R:在 ggplot 图中添加每个月的背景阴影

发布于 2025-01-11 14:29:50 字数 3233 浏览 0 评论 0原文

我正在努力在 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))

graph

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))

graph

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

超可爱的懒熊 2025-01-18 14:29:50

我不会创建所有这些年月变量,而是为背景创建一个小的日期数据框:

rect_df <- data.frame(start = seq(as.Date("2015-01-01"), 
                                  length.out = n_month, by = "month"),
                      end = seq(as.Date("2015-02-01"), 
                                  length.out = n_month, by = "month"),
                      fill = c("lightblue", "cornflowerblue"))

这样您的绘图调用也短得多,只需要一个 geom_rect

ggplot(df, aes(x = year_month, y = sum, group = 1)) + 
  geom_rect(data = rect_df,
            aes(xmin = start, xmax = end, fill = fill, ymin = -Inf, ymax = Inf),
            alpha = 0.3, color = NA, inherit.aes = FALSE) +
  scale_fill_identity() +
  labs(fill = "year") +
  geom_point() +
  geom_line() +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5))

在此处输入图像描述< /a>

Instead of creating all those year-month variables, I would create a little data frame of dates for the backgrounds:

rect_df <- data.frame(start = seq(as.Date("2015-01-01"), 
                                  length.out = n_month, by = "month"),
                      end = seq(as.Date("2015-02-01"), 
                                  length.out = n_month, by = "month"),
                      fill = c("lightblue", "cornflowerblue"))

So that your plot call is much shorter too, requiring only a single geom_rect

ggplot(df, aes(x = year_month, y = sum, group = 1)) + 
  geom_rect(data = rect_df,
            aes(xmin = start, xmax = end, fill = fill, ymin = -Inf, ymax = Inf),
            alpha = 0.3, color = NA, inherit.aes = FALSE) +
  scale_fill_identity() +
  labs(fill = "year") +
  geom_point() +
  geom_line() +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5))

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文