ggplot的阴影背景根据月份 [英] Shade background of ggplot according to month
本文介绍了ggplot的阴影背景根据月份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
此代码
df <- data.frame(day <- 0:365, value = 1)
library(ggplot2)
ggplot(df, aes(day, value)) + geom_blank() + scale_x_continuous(breaks = seq(0, 365, 10)) + theme_bw()
生成这个情节:
使用R代码,我想遮住 day
根据哪个月份的情节。我想让情节看起来像下面(情节背景是Photoshopped)。我会特别感兴趣的一个解决方案,使用 lubridate
。
Using R code, I want to shade the background of the plot according to which day
falls in which month. I want the plot to look like below (the plot background is Photoshopped). I would be particularly interested in a solution that uses lubridate
.
推荐答案
这里是解决方案的一部分使用 geom_raster
。
Here's part of the solution using geom_raster
.
library(lubridate)
library(ggplot2)
x <- seq(from = as.Date("2000/1/1"), to = as.Date("2000/12/31"), "day")
xy <- data.frame(datum = x, day = day(x), month = month(x), seq.day = 1:length(x),
month.ind = rep(rep(0:1, 6), times = rle(xy$month)$lengths))
ggplot(xy, aes(x = seq.day, y = 1, fill = as.factor(month.ind))) +
theme_bw() +
scale_fill_manual(values = c("black", "white")) +
scale_x_continuous(breaks = seq(0, 365, 10)) +
geom_raster(alpha = 0.3, show_guide = FALSE)
这篇关于ggplot的阴影背景根据月份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文