In Adding individual arrows in multiple plots I described a problem which has been resolved thanks to mathematical.coffee.
My problem was how to add a single arrow to only one facet. Recently I installed a new version of R:
platform i386-pc-mingw32
version.string R version 2.14.2 (2012-02-29) ggplot2_0.9.0
Here is the code example
# data frame
xdf <- data.frame(x=rep(1:10,2)
,y=c( 2*c(1:10)+rnorm(10,0,3), 4*c(1:10)+rnorm(10,0,5))
,z=rep(c("A","B"),each=10)
)
xdf
# plot
ggplot with faceting
xp <- ggplot(xdf,aes(x=x,y=y)) +
geom_line() +
facet_grid(. ~ z)
xp
# location of the arrow: x=4, y=y+1 on the top of the first facet (A)
(f1x4 <- xdf[4,"y"]+1)
# add arrow and label
xp + geom_segment(aes(x=4,xend=4,y=f1x4+3,yend=f1x4,z='A') # <-- see the z='A'
, arrow=arrow(length=unit(0.4,"cm")
)
) +
geom_text(aes(x=4,y=f1x4+5, label="a",z='A')) # <-- see the z='A'
What should happen: the arrow should be created only on facet A. What happens: the arrow is created on both facets A and B.
Has anybody an idea how to solve this problem?