R:在 ggplot 图中添加每个月的背景阴影
我正在努力在 ggplot 图中添加每个月的背景阴影。我的代码工作正常,但我想更自动地完成此任务。我的代码示例:
library(ggplot2)
set.seed(1234)
n_month <- 26
df <- data.frame(
month = seq(as.Date("2015-01-01"), length.out = n_month, by = "month"),
sum = rnorm(n_month, mean = 200000, sd = 50000)
)
df$year_month <- format(df$month, "%Y-%m-%d")
df$year_month <- as.Date(df$year_month)
#Here 我必须配置每个月以在 ggplot 图中的 geom_rect 中使用。我想自动执行此任务
jan1 = as.Date("2015-01-01")
feb1 = as.Date("2015-02-01")
mar1 = as.Date("2015-03-01")
apr1 = as.Date("2015-04-01")
may1 = as.Date("2015-05-01")
jun1 = as.Date("2015-06-01")
jul1 = as.Date("2015-07-01")
ago1 = as.Date("2015-08-01")
sep1 = as.Date("2015-09-01")
oct1 = as.Date("2015-10-01")
nov1 = as.Date("2015-11-01")
dec1 = as.Date("2015-12-01")
jan2 = as.Date("2016-01-01")
feb2 = as.Date("2016-02-01")
mar2 = as.Date("2016-03-01")
apr2 = as.Date("2016-04-01")
may2 = as.Date("2016-05-01")
jun2 = as.Date("2016-06-01")
jul2 = as.Date("2016-07-01")
ago2 = as.Date("2016-08-01")
sep2 = as.Date("2016-09-01")
oct2 = as.Date("2016-10-01")
nov2 = as.Date("2016-11-01")
dec2 = as.Date("2016-12-01")
jan3 = as.Date("2017-01-01")
feb3 = as.Date("2017-02-01")
#Using geom_rect 我配置每个背景。我在示例中绘制了几个月的图表,但我的目标是自动添加直到 2 月 3 日的背景。
ggplot(df, aes(x = year_month, y = sum, group = 1)) +
geom_rect(mapping=aes( xmax=jan1, ymin=-Inf, ymax=Inf),
xmin=-Inf ,fill='lightblue', alpha=0.01, color=NA)+
geom_rect(mapping=aes(xmin=jan1, xmax=feb1, ymin=-Inf,
ymax=Inf), fill='cornflowerblue', alpha=0.01, color=NA)+
geom_rect(mapping=aes(xmin=feb1, xmax=mar1, ymin=-Inf,
ymax=Inf), fill='lightblue', alpha=0.01, color=NA)+
geom_rect(mapping=aes(xmin=mar1, xmax=apr1, ymin=-Inf,
ymax=Inf), fill='cornflowerblue', alpha=0.01, color=NA)+
geom_rect(mapping=aes(xmin=apr1, xmax=may1, ymin=-Inf,
ymax=Inf), fill='lightblue', alpha=0.01, color=NA)+
geom_rect(mapping=aes(xmin=may1, xmax=jun1, ymin=-Inf,
ymax=Inf), fill='cornflowerblue', alpha=0.01, color=NA)+
geom_rect(mapping=aes(xmin=jun1, xmax=jul1, ymin=-Inf,
ymax=Inf), fill='lightblue', alpha=0.01, color=NA)+
geom_rect(mapping=aes(xmin=jul1, xmax=ago1, ymin=-Inf,
ymax=Inf), fill='cornflowerblue', alpha=0.01, color=NA)+
geom_rect(mapping=aes(xmin=ago1, xmax=sep1, ymin=-Inf,
ymax=Inf), fill='lightblue', alpha=0.01, color=NA)+
geom_rect(mapping=aes(xmin=sep1, xmax=oct1, ymin=-Inf,
ymax=Inf), fill='cornflowerblue', alpha=0.01, color=NA)+
geom_rect(mapping=aes(xmin=oct1, xmax=nov1, ymin=-Inf,
ymax=Inf), fill='lightblue', alpha=0.01, color=NA)+
geom_rect(mapping=aes(xmin=nov1, xmax=dec1, ymin=-Inf,
ymax=Inf), fill='cornflowerblue', alpha=0.01, color=NA)+
geom_rect(mapping=aes(xmin=dec1, xmax=jan2, ymin=-Inf,
ymax=Inf), fill='lightblue', alpha=0.01, color=NA)+
geom_rect(mapping=aes(xmin=jan2, xmax=feb2, ymin=-Inf,
ymax=Inf), fill='cornflowerblue', alpha=0.01, color=NA)+
labs(fill = "year") +
geom_point() +
geom_line() +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5))
I am working adding brackground shading for each month in a ggplot graph. My code work fine but I would like to do this task more automatically. example of my code:
library(ggplot2)
set.seed(1234)
n_month <- 26
df <- data.frame(
month = seq(as.Date("2015-01-01"), length.out = n_month, by = "month"),
sum = rnorm(n_month, mean = 200000, sd = 50000)
)
df$year_month <- format(df$month, "%Y-%m-%d")
df$year_month <- as.Date(df$year_month)
#Here I have to configure each month to use in geom_rect in the ggplot graph. I would like automate this task
jan1 = as.Date("2015-01-01")
feb1 = as.Date("2015-02-01")
mar1 = as.Date("2015-03-01")
apr1 = as.Date("2015-04-01")
may1 = as.Date("2015-05-01")
jun1 = as.Date("2015-06-01")
jul1 = as.Date("2015-07-01")
ago1 = as.Date("2015-08-01")
sep1 = as.Date("2015-09-01")
oct1 = as.Date("2015-10-01")
nov1 = as.Date("2015-11-01")
dec1 = as.Date("2015-12-01")
jan2 = as.Date("2016-01-01")
feb2 = as.Date("2016-02-01")
mar2 = as.Date("2016-03-01")
apr2 = as.Date("2016-04-01")
may2 = as.Date("2016-05-01")
jun2 = as.Date("2016-06-01")
jul2 = as.Date("2016-07-01")
ago2 = as.Date("2016-08-01")
sep2 = as.Date("2016-09-01")
oct2 = as.Date("2016-10-01")
nov2 = as.Date("2016-11-01")
dec2 = as.Date("2016-12-01")
jan3 = as.Date("2017-01-01")
feb3 = as.Date("2017-02-01")
#Using geom_rect I configure each background. I graph some months in my example but my goal its add brackground until feb3 automatically.
ggplot(df, aes(x = year_month, y = sum, group = 1)) +
geom_rect(mapping=aes( xmax=jan1, ymin=-Inf, ymax=Inf),
xmin=-Inf ,fill='lightblue', alpha=0.01, color=NA)+
geom_rect(mapping=aes(xmin=jan1, xmax=feb1, ymin=-Inf,
ymax=Inf), fill='cornflowerblue', alpha=0.01, color=NA)+
geom_rect(mapping=aes(xmin=feb1, xmax=mar1, ymin=-Inf,
ymax=Inf), fill='lightblue', alpha=0.01, color=NA)+
geom_rect(mapping=aes(xmin=mar1, xmax=apr1, ymin=-Inf,
ymax=Inf), fill='cornflowerblue', alpha=0.01, color=NA)+
geom_rect(mapping=aes(xmin=apr1, xmax=may1, ymin=-Inf,
ymax=Inf), fill='lightblue', alpha=0.01, color=NA)+
geom_rect(mapping=aes(xmin=may1, xmax=jun1, ymin=-Inf,
ymax=Inf), fill='cornflowerblue', alpha=0.01, color=NA)+
geom_rect(mapping=aes(xmin=jun1, xmax=jul1, ymin=-Inf,
ymax=Inf), fill='lightblue', alpha=0.01, color=NA)+
geom_rect(mapping=aes(xmin=jul1, xmax=ago1, ymin=-Inf,
ymax=Inf), fill='cornflowerblue', alpha=0.01, color=NA)+
geom_rect(mapping=aes(xmin=ago1, xmax=sep1, ymin=-Inf,
ymax=Inf), fill='lightblue', alpha=0.01, color=NA)+
geom_rect(mapping=aes(xmin=sep1, xmax=oct1, ymin=-Inf,
ymax=Inf), fill='cornflowerblue', alpha=0.01, color=NA)+
geom_rect(mapping=aes(xmin=oct1, xmax=nov1, ymin=-Inf,
ymax=Inf), fill='lightblue', alpha=0.01, color=NA)+
geom_rect(mapping=aes(xmin=nov1, xmax=dec1, ymin=-Inf,
ymax=Inf), fill='cornflowerblue', alpha=0.01, color=NA)+
geom_rect(mapping=aes(xmin=dec1, xmax=jan2, ymin=-Inf,
ymax=Inf), fill='lightblue', alpha=0.01, color=NA)+
geom_rect(mapping=aes(xmin=jan2, xmax=feb2, ymin=-Inf,
ymax=Inf), fill='cornflowerblue', alpha=0.01, color=NA)+
labs(fill = "year") +
geom_point() +
geom_line() +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不会创建所有这些年月变量,而是为背景创建一个小的日期数据框:
这样您的绘图调用也短得多,只需要一个
geom_rect
< /a>
Instead of creating all those year-month variables, I would create a little data frame of dates for the backgrounds:
So that your plot call is much shorter too, requiring only a single
geom_rect