使用ggplot和geom

编程入门 行业动态 更新时间:2024-10-27 12:31:40
使用ggplot和geom_rect(Using ggplot and geom_rect)

我有这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_ts

To 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 found

What 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 4

Set color for the outline of the rectangle, fill for the fill color of the rectangle.

更多推荐

本文发布于:2023-08-01 02:23:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1351768.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:ggplot   geom

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!