【发布时间】:2012-02-27 22:01:05
【问题描述】:
我想在 ggplot 和 faceting 生成的 2 个图中添加箭头。问题:如何避免两个图中的箭头重复?我想为每个情节添加单独的箭头。 这是一个例子:
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"))
发生了什么: 箭头放置在同一区域的两个方面。如何选择特定的地块来放置箭头?
【问题讨论】: