I am trying to build a date range bar graph usins ggplot2 (R) in the spirit of: enter image description here

我跟踪了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.

推荐答案

一种使用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

R相关问答推荐

手工PCA上的载体与输出双图不匹配

检测(并替换)字符串中的数学符号

为什么st_join(ob1,ob2,left = True)返回具有比ob1更多功能的sf对象?

如何自定义Shapviz图?

如何通过Docker部署我的shiny 应用程序(多个文件)

计算两列中满足特定条件连续行之间的平均值

如何将网站图像添加到带有极坐标的面包裹条形图?

将选定的索引范围与阈值进行比较

悬崖三角洲超大型群数计算导致整数溢出

当每个变量值只能 Select 一次时,如何从数据框中 Select 两个变量的组合?

在R中使用列表(作为tibble列)进行向量化?

R中时间间隔的大向量与参考时间间隔的相交

减少雨云面之间的间距并绘制所有统计数据点

在ggplot2图表中通过端点连接点

抽样变换-REXP与RWEIBUR

隐藏基于 case 总数的值

策略表单连接两个非常大的箭头数据集,而不会 destruct 内存使用

使用显式二元谓词子集化sfc对象时出错

在具有条件的循环中添加行

在分面的ggplot2条形图中对条形图进行排序,并省略每组未使用的系数级别