T.G*_*.G. 3 plot r facet ggplot2
我有一个很大的情节,使用facet_grid().我想添加一条垂直线来表示y = 0,但仅限于某些情节.
可重复的例子 -
df <- data.frame(x = 1:100, y = rnorm(100,sd=0.5), type = rep(c('A','B'), 50))
ggplot(df) + facet_grid(type~.) +
     geom_point(data = df[df$type == 'A',], mapping = aes(x=x, y=y)) +
     geom_rect(data = df[df$type == 'B',], mapping=aes(xmin=x,ymin=0,xmax=(x+2),ymax=y)) +
     theme(panel.background=element_rect(fill="white")) 
我只想在顶部的ptot中使用该行.
只需为hline geom创建另一个数据对象,并确保包含相关的分面变量.
df <- data.frame(x = 1:100, y = rnorm(100,sd=0.5), type = rep(c('A','B'), 50))
ggplot(df) + facet_grid(type~.) +
     geom_point(data = df[df$type == 'A',], mapping = aes(x=x, y=y)) +
     geom_rect(data = df[df$type == 'B',], mapping=aes(xmin=x,ymin=0,xmax=(x+2),ymax=y)) +
     geom_hline(data = data.frame(type="A", y=0), mapping=aes(yintercept=y)) +
     theme(panel.background=element_rect(fill="white"))