# Create the dataset
x <- c(1, 2, 3, 4, 5)
y <- c(10, 30, 20, 40, 35)
df <- data.frame(x, y)
# Create a basic plot
p <- ggplot(df, aes(x, y)) +
geom_point() +
geom_line()
# Add an annotation with a rectangle shape
p + annotate("rect", xmin = 2, xmax = 4, ymin = 15, ymax = 40,
fill = "lightgreen", color = "blue", alpha = 0.5) +
# Add an annotation with an arrow shape using geom_segment
geom_segment(x = 2, xend = 4, y = 15, yend = 40,
arrow = arrow(length = unit(0.3, "inches")),
color = "red")