英文:
ggplot2: Date range bar graph
问题
我正在尝试使用ggplot2(R)构建一个日期范围条形图,灵感来自于:
我已经关注了一个主题,但我完全无法使用日期复制结果。
如果我理解正确,对于每个“id”,条的长度由数据库中最小和最大的“value”确定。
这是我的数据的最小工作示例:
示例数据框
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")))
从宽格式转换为长格式
DFGather <- DF %>% tidyr::gather(key="Time", value="Value", 2:3)
生成图表
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())
我还尝试过将POSIXct日期转换为整数,但没有改变输出:
DFGather$Value <- as.integer(format(DFGather$Value,"%Y%m%d"))
感谢支持,
C.
英文:
I am trying to build a date range bar graph usins ggplot2 (R) in the spirit of:
I have followed a thread but I am completely unable to reproduce the results with dates.
If I understood it correctly, for each "id", the bar length is determined by the smallest and largest "value" in the database.
Here is a minimally working example of my data:
# 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())
I have also tried converting POSIXct dates to integers but it didn't change my output:
DFGather$Value <- as.integer(format(DFGather$Value,"%Y%m%d"))
Thanks for the support,
C.
答案1
得分: 1
使用geom_segment
的快速而粗糙的方法。
ggplot2::ggplot(DF, ggplot2::aes(x = CreationTime, xend = LastActivity, y = Name, yend = Name, colour = Status)) +
ggplot2::geom_segment(linewidth = 15) +
ggplot2::coord_cartesian(xlim = c(min(DFGather$Value),max(DFGather$Value))) +
ggplot2::scale_x_datetime(date_breaks = "3 months", labels = scales::label_date_short())
创建于2023-06-01,使用reprex v2.0.2
英文:
A quick and dirty approach using geom_segment
.
ggplot2::ggplot(DF, ggplot2::aes(x = CreationTime, xend = LastActivity, y = Name, yend = Name, colour = Status)) +
ggplot2::geom_segment(linewidth = 15) +
ggplot2::coord_cartesian(xlim = c(min(DFGather$Value),max(DFGather$Value))) +
ggplot2::scale_x_datetime(date_breaks = "3 months", labels = scales::label_date_short())
<!-- -->
<sup>Created on 2023-06-01 with reprex v2.0.2</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论