图表显示 r 中从和到日期范围的数据

Graph showing data with from and to date ranges in r

我有以下购买和投资期限的数据。

investment <- as.character(c("First", "Second", "Third"))
from <- as.Date(c("2019-11-01", "2020-02-25", "2020-06-10"))
to <- as.Date(c("2020-08-31","2021-02-24", "2020-08-09"))
amount <- as.numeric(c(20000,15000,17000))
df <- data.frame(investment, from, to, amount)

我想要 X 轴 上带有日期的水平条,最小值是数据集中的最早日期,即“2019-11-01”,最大值是最后一个日期“2021-” 02-24”。 Y 轴 上的投资,即第一、第二和第三。

每项投资的日期之间应出现横条(从和到),在 X 轴上显示涵盖不同时期的不同投资。

鼠标悬停应显示金额作为标签。 X 轴或 Y 轴上的金额都没有相关性。

您可以使用来自 ggplot2

geom_linerange() 执行此操作
library(ggplot2)
ggplot(df) + 
  geom_linerange(aes(y = investment, xmin = from, xmax = to ), size = 18,
                 color = "orange") +
  xlab("date")