I want to add arrows in 2 plots produced with ggplot and faceting. Problem: how can I avoid a replication of the arrow in both graphs? I would like to add individual arrows to each plot. Here is an example:
library(ggplot2)
# data frame with fake data
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
# ggplot with faceting
xp <- ggplot(xdf,aes(x=x,y=y)) +
geom_line() +
facet_grid(. ~ z)
xp
# location of the arrow: x=4, y= on the top
(f1x4 <- xdf[4,"y"])+1
xp + geom_segment(aes(x=4,xend=4,y=f1x4+3,yend=f1x4)
, arrow=arrow(length=unit(0.4,"cm")
)
) +
geom_text(aes(x=4,y=f1x4+5, label="a"))
What happened: arrow is placed in both facets in the identical region. How can I choose a specific plot to place the arrows?
As far as I can tell, to add individual layers onto a facet, you need to supply a data frame with the corresponding facet value.
From the ggplot2 facet_grid page:
So just doing
xp + geom_segment(aes(x,xend,y,yend))
will draw on all facets, because the facetting variable is missing.Your facetting variable is
z
, so you can either:arrow.df
withx
,y
, andz
being 'A'z
intoaes
directly.Second option seems handier:
So you just add in a
factorvariablename=factor
to theaes
argument to plot on that particular panel (since yourz
column in the data frame forxp
has levels 'A' and 'B').