I am trying to build a date range bar graph usins ggplot2 (R) in the spirit of:
我跟踪了thread,但我完全无法重现带有日期的结果. 如果我理解正确的话,对于每个"id",条长是由数据库中最小和最大的"值"决定的.
以下是我的数据的一个最基本的工作示例:
# Example dataframe
DF <- data.frame(Name = as.factor(c("1.Project1", "2.Project2", "3.Project3", "4.Project4")),
CreationTime = as.POSIXct(c("2019-12-10 13:22:20", "2019-12-17 12:25:48", "2020-01-02 13:02:57", "2020-01-14 08:37:10")),
LastActivity = as.POSIXct(c("2019-12-17 10:42:17 ", "2020-01-02 13:27:10", "2021-02-11 11:32:45", "2023-05-03 07:41:38")),
Status = as.factor(c("Prod", "Prod", "Dev", "Complete")))
# From wide to long
DFGather <- DF %>% tidyr::gather(key="Time", value="Value", 2:3)
# Generate plot
ggplot2::ggplot(DFGather, aes(x = Value, y = Name, fill = Status)) +
ggplot2::geom_col() +
ggplot2::coord_cartesian(xlim = c(min(DFGather$Value),max(DFGather$Value))) +
ggplot2::scale_x_datetime(date_breaks = "3 months", labels = scales::label_date_short())
我还try 将POSIXct日期转换为整数,但没有更改我的输出:
DFGather$Value <- as.integer(format(DFGather$Value,"%Y%m%d"))
谢谢你的支持,
C.