在ggplot2中添加垂直线时出错

时间:2013-03-01 19:30:21

标签: r ggplot2

因此,我从拍卖中获得了大量数据集(大约40万个观测值)。我正在尝试使用ggplot按天绘制拍卖价格,同时用颜色和月份表示垂直线。

我有一个POSIXlt向量来保存我的日期,这是我正在使用的:

firstmonth<- c(1,32,60,91,121,152,182,213,244,274,305,335)

require(ggplot2)
p <- ggplot(bb, aes(saledate$yday, SalePrice))
p <- p + geom_point(aes(color = factor(saledate$year)), alpha = I(1/30)) #This plot works
p + geom_vline(aes(xintercept = firstmonth))
Error in data.frame(xintercept = c(1, 32, 60, 91, 121, 152, 182, 213,  : 
  arguments imply differing number of rows: 12, 401125

这个错误是什么?为什么我不能得到垂直线?

1 个答案:

答案 0 :(得分:6)

您需要将新的data.frame传递给geom_vline

library(ggplot2)

bb <- data.frame(SalePrice=rnorm(1000))
saledate <- data.frame(yday=1:1000,year=rep(1:10,each=100))

firstmonth<- c(1,32,60,91,121,152,182,213,244,274,305,335)

p <- ggplot(bb, aes(saledate$yday, SalePrice))
p <- p + geom_point(aes(color = factor(saledate$year))) #This plot works
p + geom_vline(aes(xintercept = firstmonth))
#error
df2 <- data.frame(firstmonth)
p + geom_vline(data=df2,aes(xintercept = firstmonth))
#works