【问题标题】:How to shade an area in ggplot mixing geom_rect and geom_line?如何在 ggplot 中混合 geom_rect 和 geom_line 对一个区域进行着色?
【发布时间】:2020-06-26 08:06:17
【问题描述】:

考虑这些数据:

shade <- structure(list(from = structure(c(6940, 7670, 8766, 13879, 14610, 
15340), class = "Date"), to = structure(c(6940, 7670, 10227, 
13879, 14610, 17897), class = "Date")), class = "data.frame", row.names = c(NA, 
-6L))

我正在尝试用shade 为 ggplot 中的一个区域着色。另一部分数据是:

df <- structure(list(Date = structure(c(4383, 4748, 5113, 5479, 5844, 
6209, 6574, 6940, 7305, 7670, 8035, 8401, 8766, 9131, 9496, 9862, 
10227, 10592, 10957, 11323, 11688, 12053, 12418, 12784, 13149, 
13514, 13879, 14245, 14610, 14975, 15340, 15706, 16071, 16436, 
16801, 17167, 17532, 17897), class = "Date"), growth = c(0.0623150839421278, 
-0.0118611051533168, -0.0664501162049262, -0.087782956704693, 
-0.129496959622578, -0.18594460634209, -0.188214164298442, -0.172238123190157, 
-0.94277027404306, -0.0834575040012719, -0.0883082379839397, 
-0.101106281706148, -0.0371396056259545, -0.011174839245335, 
0.0182311778775262, 0.0549337165151567, 0.35326560836822, 0.320551702135409, 
0.295635261636084, 0.201819487872301, 0.045320365597334, 0.036634868496078, 
0.000464348739324549, -0.0302907006607054, -0.0615712296201636, 
-0.0850991148284894, -0.0819342537954828, -0.0874010742126909, 
-0.0688419496282826, -0.086814819317242, -0.0567750186957099, 
-0.0333039095763059, -0.00719293166024748, 0.0166361057943032, 
0.0707141351711211, 0.133846612601381, 0.156206411946355, 0.329862325903626
)), row.names = 23:60, class = "data.frame")

这是我尝试过的,但它忽略了 shade$fromshade$to 中具有相同值的行:

library(ggplot2)

ggplot() +
  geom_rect(data = shade, aes(xmin = from, xmax = to, ymin = -Inf, ymax = Inf)) +
  geom_line(data = df,  aes(x = Date, y = growth))

我猜geom_rectgeom_line 来自 ggplot 的组合会起作用。

【问题讨论】:

  • 您能否描述或提供您希望最终情节的样子的草图?
  • 我认为你可以做这样的事情 ggplot(df, aes(x=Date, y = growth)) + geom_area( fill = "lightblue", color="blue", linetype="dashed ", 大小=1)

标签: r ggplot2


【解决方案1】:

对于阴影区域只是垂直线的情况,只需添加 geom_vline

library(ggplot2)
library(dplyr)

ggplot() +
  geom_vline(data = dplyr::filter(shade, from == to), aes(xintercept = from)) +
  geom_rect(data = shade, aes(xmin = from, xmax = to, ymin = -Inf, ymax = Inf)) +
  geom_line(data = df,  aes(x = Date, y = growth))

reprex package (v0.3.0) 于 2020 年 6 月 26 日创建

【讨论】:

  • 这正是我想要的。非常感谢!
【解决方案2】:

带或不带图例的两个选项:

对于图例:向阴影数据框添加一个因子;

使用颜色美学和填充来管理相同的日期。

我怀疑你不能填充没有宽度的区域。

您不需要使用这种方法添加额外的几何图形:


library(ggplot2)
library(patchwork)

shade$area <- letters[1:nrow(shade)]

p1 <- ggplot() +
  geom_rect(data = shade, aes(xmin = from, xmax = to, ymin = -Inf, ymax = Inf, colour = area, fill = area)) +
  geom_line(data = df,  aes(x = Date, y = growth))+
  ggtitle("Shade each area identifiable with legend")


p2 <- ggplot() +
  geom_rect(data = shade, aes(xmin = from, xmax = to, ymin = -Inf, ymax = Inf), colour = "gray50", fill = "gray50") +
  geom_line(data = df,  aes(x = Date, y = growth))+
  ggtitle("Shade uniform colour, without legend")


p1/p2

reprex package (v0.3.0) 于 2020 年 6 月 26 日创建

【讨论】:

    猜你喜欢
    • 2016-06-21
    • 2021-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-02
    • 2012-04-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多