myfunc <- function(x) sin(x)*100+5*x
(df <- data.frame(x=150:300/10,
y=myfunc(150:300/10)))
min2 <- optimise(myfunc,c(20,25))
ggplot(df) +
aes(x=x,
y=y) +
geom_point() +
annotate("segment",
x = min2$minimum - 2,
xend = min2$minimum,
y = min2$objective-1,
yend = min2$objective,
colour = "red",
size=1,
arrow=arrow()) +
geom_text(data=data.frame(x=min2$minimum - 2.5,
y=min2$objective - 5),
label = "minimum")