首页 文章

在POSIXct轴上使用ggplot2中的不同数据集着色的vlines

提问于
浏览
1

我想用另一个data.frame中的发布日期注释我的错误图,但我希望vline的颜色与相应的原始跟踪相匹配

# first create some dummy data
set.seed(123)
N <- 100
adf <- data.frame(version=sample(c('A','B','C'), N, replace=TRUE),
              cs=as.POSIXct('2011-06-01 00:00') + rnorm(N, 20, 70)*86400)
# lets just shift things slightly, depending on version
adf$cs <- adf$cs + (as.integer(adf$version) - 1)*5e6
adf <- adf[order(adf$cs),]
library(plyr)
adf <- ddply(adf, .(version), function(bdf) { cbind(bdf, bugno=1:nrow(bdf)) } )

# now lets plot these bug curves by version
library(ggplot2)
q <- qplot(cs, bugno, data=adf, geom='line', colour=version,
  xlab='', ylab='Number of Bugs')
print(q)

# however I'd like to annotate these plots by adding the 
# dates of "release", with the colour matching that of release 
# in the plot q, so no further annotation necessary (hopefully!)
g.res <- data.frame(version=c('A','B','C'),
                releasedate=c(as.Date('2011-06-01'), as.Date('2011-10-01'),
                              as.Date('2012-01-01')))
# works... but only in blue...
q + geom_vline(data=g.res, aes(xintercept=as.POSIXct(releasedate)), col="blue")

我知道Axis breaks at noon each day of ggplot2 chartHow to get a vertical geom_vline to an x-axis of class date?

1 回答

  • 1

    因为我把所有这些工作都放到了问题中我才刚刚意识到答案......颜色必须是aes的一部分!我仍然没有正确理解aes如何工作,我将不得不再次阅读这本书! :-)

    q + geom_vline(data=g.res, aes(xintercept=as.POSIXct(releasedate), col=version) )
    

相关问题