我有这3个时间序列:
a<-rnorm(357) b<-rnorm(357) c<-rnorm(357) a_ts<-ts(a, start=c(1980, 1), frequency=12) b_ts<-ts(b, start=c(1980, 1), frequency=12) c_ts<-ts(c, start=c(1980, 1), frequency=12) a_time<-time(a_ts) a_series<-ts.union(month=a_time,a=a_ts) a_series_df<-as.data.frame(a_series) a_series_df["b"] <- b_ts a_series_df["c"] <- c_ts要使用ggplot功能,我将其melted :
melted = melt(a_series_df, id.vars="month")情节顺利:
ggplot(data=melted, aes(x=month, y=2*value)) + geom_line(aes(colour = variable))但是,当我想要在下面这些间隔之间的棚图时,它会显示以下错误消息:
shade = data.frame(x1=c(1980.333 ,2009.167), x2=c(2007.333 ,2009.667), y1=c(0,3), y2=c(0,4)) ggplot(data=melted, aes(x=month, y=2*value)) + geom_line(aes(colour = variable))+ geom_rect(data=shade, mapping=aes(xmin=x1, xmax=x2, ymin=y1, ymax=y2), color='grey', alpha=0.2) Error in eval(expr, envir, enclos) : object 'month' not found我错过了什么?
I have this 3 time series:
a<-rnorm(357) b<-rnorm(357) c<-rnorm(357) a_ts<-ts(a, start=c(1980, 1), frequency=12) b_ts<-ts(b, start=c(1980, 1), frequency=12) c_ts<-ts(c, start=c(1980, 1), frequency=12) a_time<-time(a_ts) a_series<-ts.union(month=a_time,a=a_ts) a_series_df<-as.data.frame(a_series) a_series_df["b"] <- b_ts a_series_df["c"] <- c_tsTo use ggplot function i melted it:
melted = melt(a_series_df, id.vars="month")The plot goes well:
ggplot(data=melted, aes(x=month, y=2*value)) + geom_line(aes(colour = variable))But when i want a shed plot between these intervals below, it shows this error message:
shade = data.frame(x1=c(1980.333 ,2009.167), x2=c(2007.333 ,2009.667), y1=c(0,3), y2=c(0,4)) ggplot(data=melted, aes(x=month, y=2*value)) + geom_line(aes(colour = variable))+ geom_rect(data=shade, mapping=aes(xmin=x1, xmax=x2, ymin=y1, ymax=y2), color='grey', alpha=0.2) Error in eval(expr, envir, enclos) : object 'month' not foundWhat am I missing?
最满意答案
您在geom_rect图层中缺少inherit.aes = FALSE 。 它期望能够从ggplot初始化中找到所有映射变量,除非你告诉它不要继承那些美学。
这有效:
ggplot(data = melted, aes(x = month, y = 2 * value)) + geom_line(aes(colour = variable))+ geom_rect(data = shade, mapping = aes(xmin = x1, xmax = x2, ymin = y1, ymax = y2), color = 'grey', alpha = 0.2, inherit.aes = FALSE)第一个矩形不会显示,因为矩形的数据y1和y2为0 - 所以它只是一条线。 第二个矩形在那里,但它非常小。
shade # x1 x2 y1 y2 # 1 1980.333 2007.333 0 0 # 2 2009.167 2009.667 3 4设置矩形轮廓的color , fill矩形的填充颜色。
You're missing inherit.aes = FALSE in your geom_rect layer. It's expecting to be able to find all the mapped variables from ggplot initialization unless you tell it not to inherit those aesthetics.
This works:
ggplot(data = melted, aes(x = month, y = 2 * value)) + geom_line(aes(colour = variable))+ geom_rect(data = shade, mapping = aes(xmin = x1, xmax = x2, ymin = y1, ymax = y2), color = 'grey', alpha = 0.2, inherit.aes = FALSE)The first rectangle doesn't show up still because your data for the rectangle has both y1 and y2 as 0 - so it is just a line. The second rectangle is there, but it is very small.
shade # x1 x2 y1 y2 # 1 1980.333 2007.333 0 0 # 2 2009.167 2009.667 3 4Set color for the outline of the rectangle, fill for the fill color of the rectangle.
更多推荐
发布评论