我有这些数据框
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))
到目前为止一切顺利。现在我想为periods
的每个观察添加一个功能区,功能区应该在两个分别设置为first
和last
的x截距之间绘制,就像
geom_ribbon(data=periods, aes(xmin=first, xmax=last))
哪个不起作用。
我想要获得的是为periods
中每个时期的相应区域着色。所以在100-300期间,结果看起来应该是