ggplot2:日期范围条形图

huangapple go评论88阅读模式
英文:

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:
ggplot2:日期范围条形图

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 &lt;- data.frame(Name = as.factor(c(&quot;1.Project1&quot;, &quot;2.Project2&quot;, &quot;3.Project3&quot;, &quot;4.Project4&quot;)),
                 CreationTime = as.POSIXct(c(&quot;2019-12-10 13:22:20&quot;, &quot;2019-12-17 12:25:48&quot;, &quot;2020-01-02 13:02:57&quot;, &quot;2020-01-14 08:37:10&quot;)),
                 LastActivity = as.POSIXct(c(&quot;2019-12-17 10:42:17 &quot;, &quot;2020-01-02 13:27:10&quot;, &quot;2021-02-11 11:32:45&quot;, &quot;2023-05-03 07:41:38&quot;)),
                 Status = as.factor(c(&quot;Prod&quot;, &quot;Prod&quot;, &quot;Dev&quot;, &quot;Complete&quot;)))

# From wide to long
DFGather &lt;- DF %&gt;% tidyr::gather(key=&quot;Time&quot;, value=&quot;Value&quot;, 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 = &quot;3 months&quot;, labels = scales::label_date_short())

I have also tried converting POSIXct dates to integers but it didn't change my output:

DFGather$Value &lt;- as.integer(format(DFGather$Value,&quot;%Y%m%d&quot;))

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())

ggplot2:日期范围条形图

创建于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 = &quot;3 months&quot;, labels = scales::label_date_short())

ggplot2:日期范围条形图<!-- -->

<sup>Created on 2023-06-01 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年6月1日 19:56:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76381615.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定