How to draw an arrow in the middle of a curve

In a previous topic it was discussed how to draw an arrow in the middle of a segment
https://forum.posit.co/t/how-to-draw-an-arrow-in-the-middle-of-the-segment/7305

I would like to be able to do the same for a curve. The example below extends the previous one with the same approach for the geom_curve, however the result is not satisfactory.
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)

b1 <- b  + 
  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)
			   
b1 +  geom_curve(aes(x = (x1+x2)/2, y = (y1+y2)/2, xend = x2, yend = y2, colour = "curve"),data = df) +
	geom_curve(aes(x=x1 , y = y1 , xend = (x1+x2)/2 , yend = (y1+y2)/2,   colour = "curve"), arrow = arrow() , data = df) 

Probably takes some trigonometry knowledge to come up with other/preferred arrangements.
Heres a simple example

b1 +   geom_curve(aes(x=x1 , y = y1 , xend = (x1+x2)/2, yend =y2,   colour = "curve"),angle=90,arrow = arrow() , data = df) +
geom_curve(aes(x = (x1+x2)/2, y =y2, xend = x2, yend = y2, colour = "curve"),angle=0,data = df) 

image

Thanks! I've tried something similar but it is difficult to make it work in every situation. I believe there is no easy solution for this as it requires some computation of the best location to end the arc.

For example

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

df <- data.frame(x1 = 2.62, x2 = 3.57, y1 = 21.0, y2 = 23.0)

b1 +   geom_curve(aes(x=x1 , y = y1 , xend = (x1+x2)/2, yend =y2,   colour = "curve"),angle=90,arrow = arrow() , data = df) +
geom_curve(aes(x = (x1+x2)/2, y =y2, xend = x2, yend = y2, colour = "curve"),angle=0,data = df) 

or

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

df <- data.frame(x1 = 2.62, x2 = 3.57, y1 = 21.0, y2 = 21.0)

b1 +   geom_curve(aes(x=x1 , y = y1 , xend = (x1+x2)/2, yend =y2,   colour = "curve"),angle=90,arrow = arrow() , data = df) +
geom_curve(aes(x = (x1+x2)/2, y =y2, xend = x2, yend = y2, colour = "curve"),angle=0,data = df) 

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.