【问题标题】:coloured vlines on a POSIXct axis with a different dataset in ggplot2POSIXct 轴上的彩色 vlines 与 ggplot2 中的不同数据集
【发布时间】:2012-06-17 10:57:00
【问题描述】:

我想用另一个 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?

【问题讨论】:

    标签: r ggplot2 time-series


    【解决方案1】:

    由于我已经将所有这些工作都投入到这个问题中,我刚刚意识到答案......颜色必须是 aes 的一部分!我对aes的工作原理还没有正确的理解,我得再看一遍书! :-)

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-16
      • 2021-11-05
      • 2018-08-17
      • 1970-01-01
      • 2023-03-24
      • 2019-10-07
      • 2020-08-15
      • 1970-01-01
      相关资源
      最近更新 更多