小编Hug*_*anp的帖子

在 x 轴上绘制时间序列时,对 ggplot 中的一个方面使用 annotate("rect")

我在构面中绘制不同的时间序列,并且我想使用 annotate() 为其中一个构面创建不同的背景颜色。一个方面代表 2018 年的最后 15 周(第 38 - 52 周),而另一个方面代表 2019 年的前 15 周(第 1 - 15 周)。我想在 2019 年仅更改第 5-8 周的背景颜色。但是,当我尝试这样做时,R 会将 2018 年的 x 轴范围从第 38-52 周更改为第 1-52 周。

我试图在 2019 年的图中仅创建一个第 5-8 周的矩形,如下所示:

annotate("rect", xmin = 5, xmax = 8, min = 0, ymax = Inf, alpha = 0.3, fill="grey") +
Run Code Online (Sandbox Code Playgroud)

我正在使用的代码是:

library(ggthemes)
week <- c(38:52, 1:15)
minutes <- sample(160, 30, replace=T)
year <- c(rep(2018, 15), rep(2019,15))
dat <- data.frame(year, week, minutes)

ggplot(dat, aes(week, minutes, group=year)) …
Run Code Online (Sandbox Code Playgroud)

r annotate facet ggplot2

4
推荐指数
1
解决办法
875
查看次数

标签 统计

annotate ×1

facet ×1

ggplot2 ×1

r ×1