R 在ggplot2中添加垂直线时出错

R 在ggplot2中添加垂直线时出错,r,ggplot2,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_po

所以,我有一个大型拍卖数据集(约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

firstmonth您需要将新的data.frame传递给
geom\u 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
库(ggplot2)

bb您需要将新的data.frame传递到
geom\u 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
库(ggplot2)

bb(+1)因此,基本上,如果您不传递新的
data.frame
,那么如果您使用
美学
,您的映射仅限于
x
上的点数。。你能解释一下原因吗?还有,为什么不干脆这样做:
geom_vline(xintercept=firstmonth)
而不使用
aes
?我所说的限制点数的意思是:
dfya,这是对的,但很奇怪。虽然我很喜欢ggplot,但像这样的东西让我觉得我不完全理解它是如何工作的。这是令人沮丧的。但是感谢您的帮助。(+1)因此,基本上,如果您不传递新的
data.frame
,那么如果您使用
美学
,您的映射仅限于
x
上的点数。。你能解释一下原因吗?还有,为什么不干脆这样做:
geom_vline(xintercept=firstmonth)
而不使用
aes
?我所说的限制点数的意思是:
dfya,这是对的,但很奇怪。虽然我很喜欢ggplot,但像这样的东西让我觉得我不完全理解它是如何工作的。这是令人沮丧的。但是谢谢你的帮助。