当x-asis为日期且y轴为因子时,绘制与x截距平行的geom_ribbon

时间:2015-09-04 00:51:26

标签: r ggplot2

我有这些数据框

set.seed(28100)
myDF <- data.frame(name = factor(sample(c("John","Paul","Ringo","George"),100,replace=TRUE)),
                   date = sample(1:1000,100,replace=TRUE))

periods <- data.frame(first = c(100,500,700),
                      last = c(300,600,900))

我想为连接所有观察的每个名字绘制一条水平线。像

require(ggplot2)
ggplot() + geom_point(data=myDF, aes(x = date, y = name)) + 
  geom_line(data=myDF, aes(x = date, y = name))

enter image description here

到目前为止一切顺利。现在我想为periods的每个观察添加一个功能区,功能区应该在两个分别设置为firstlast的x截距之间绘制,就像

geom_ribbon(data=periods, aes(xmin=first, xmax=last))

哪个不起作用。

我想要获得的是为periods中每个时期的相应区域着色。所以在100-300期间,结果看起来应该是

enter image description here

1 个答案:

答案 0 :(得分:1)

基于aosmith的评论,看起来你可以用geom_rect获得你想要的效果:

enter image description here

为了获得geom_ribbon通常具有的透明度(我假设是您考虑的原因),请将alpha的{​​{1}}属性设置为小于1的某个数字。

geom_rect