I am trying to draw a line with R's ggplot that starts on one facet and ends on another.
I believe this question was not asked yet (at least I could not find it) but I have found some example code that achieves exactly this: http://rstudio-pubs-static.s3.amazonaws.com/410976_f8eb6b218bfa42038a8b7bc9a6f9a193.html
However, documentation is weak and I did not manage to untangle the code. Can someone please provide an easily understandable version that illustrates the trick?
Here's some code as an example:
library(ggplot2)
df <- data.frame(x = 1:6, y = 1:6, facet = c(rep('A', times = 3), rep('B', times = 3)))
gg <- ggplot(data = df, mapping = aes(x = x, y = y)) + facet_grid(~ facet) +
geom_line()
gg
line <- data.frame(x = 3, y = 3,
xend = 4, yend = 4,
facet = 'A')
gg_line <- gg + geom_segment(data = line, mapping = aes(x = x, y = y,
xend = xend, yend = yend),
inherit.aes = FALSE, color = 'red')
gg_line
Obviously, in gg_line
, the red geom_segment
reaches the respective coordinates in facet A.
However, I would like the end points to refer to the coordinates in facet B.
Any nudges to a working solution are highly appreciated!