How to draw an arrow in the middle of the segment?

This example places the arrow at the end of the line. How can I place the arrow in the middle? Thank you!

b <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point()

df <- data.frame(x1 = 2.62, x2 = 3.57, y1 = 21.0, y2 = 15.0)
b +
  geom_curve(aes(x = x1, y = y1, xend = x2, yend = y2, colour = "curve"), 
             data = df) +
  geom_segment(aes(x = x1, y = y1, xend = x2, yend = y2, colour = "segment"),
               arrow = arrow(),
               data = df)

Edit: Can I remove the arrow in the curve legend key on the right as well?

You could split the line into two segments and draw the arrow only for the one that ends at the midpoint. Adding show.legend=FALSE to the arrow-drawing call to geom_segment removes the arrow from the legend:

b +
  geom_curve(aes(x = x1, y = y1, xend = x2, yend = y2, colour = "curve"), 
             data = df) +
  geom_segment(aes(x = (x1+x2)/2, y = (y1+y2)/2, xend = x2, yend = y2, colour = "segment"),
               data = df) +
  geom_segment(aes(x = x1, y = y1, xend = (x1+x2)/2 , yend = (y1+y2)/2, colour = "segment"),
               arrow = arrow(), 
               data = df, show.legend=FALSE)

Rplot132

4 Likes

That did it. Thank you much!