如何在 R 中创建拟合图来直观地表示时间序列数据集?

问题描述 投票:0回答:1

我在 R 中有一个 data.frame,其中包含来自三个地点的干旱数据:

Droughts <- data.frame("site_id" = c(1,1,1,2,2,2,2,3,3),
                       "Drought_start" = c(1962,1970,1980,1960,1970,1978,1990,1965,1988),
                       "Drought_end" = c(1964,1975,1984,1964,1974,1980,1993,1980,1993))

我现在想在图表中表示这些数据,该图表显示每个地点的干旱和间歇期。类似这样的事情:

enter image description here

有谁知道如何在 R 中实现这一点?该图表不必看起来与示例完全相同,只要它直观地表示沿着这些线的数据即可

我不知道示例中的图表类型到底是什么,因此无法正确研究它的实现。

r dataframe ggplot2 plot charts
1个回答
0
投票

可能不是最漂亮的解决方案,但这将使您接近您想要的。

library(ggplot2)

Droughts$site_id <- factor(paste("Site", Droughts$site_id))

ggplot(Droughts) +
  geom_segment(aes(x = 1950, xend = 2000, y = site_id), lwd = 10, col = "#00a2ff") +
  geom_segment(aes(x = Drought_start, xend = Drought_end, y = site_id), lwd = 10, col = "#ffff00") +
  scale_x_continuous(NULL) +
  scale_y_discrete(NULL, limits = rev(levels(Droughts$site_id))) +
  theme_minimal()

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.