【发布时间】: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$from 和 shade$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_rect 和 geom_line 来自 ggplot 的组合会起作用。
【问题讨论】:
-
您能否描述或提供您希望最终情节的样子的草图?
-
我认为你可以做这样的事情 ggplot(df, aes(x=Date, y = growth)) + geom_area( fill = "lightblue", color="blue", linetype="dashed ", 大小=1)