在R中尝试创建甘特图。

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

In r trying to create a Gant chart

问题

我正在尝试使用ggplot2创建甘特图。

library(ggplot2)

# 创建一个包含日期时间值的示例数据框
phase_summary <- data.frame(
  Phase = c("Phase 1", "Phase 2", "Phase 3", "Phase 4"),
  Starts = as.POSIXct(c("2021-01-01 09:00:00", "2021-02-01 12:30:00", "2021-03-01 10:15:00", "2021-04-01 08:45:00")),
  Finishes = as.POSIXct(c("2021-01-31 16:00:00", "2021-02-28 17:30:00", "2021-03-31 14:45:00", "2021-04-30 13:15:00"))
)

# 创建甘特图
ggplot(data = phase_summary, aes(x = Phase, y = 1, yend = 1, xmin = Starts, xmax = Finishes)) +
  geom_linerange(size = 5, color = "steelblue") +
  scale_y_continuous(breaks = 1, labels = "") +
  scale_x_datetime(date_labels = "%b %d\n%Y", expand = c(0.1, 0)) +
  labs(title = "Project Timeline",
       x = "Phases",
       y = "") +
  theme_bw()

我收到以下错误:

错误: 无效输入: time_trans 仅适用于POSIXct类对象。

这让我感到困惑,因为Starts和Finises都是POSIXct类型。

英文:

I'm trying to use ggplot2 to create a Gant chart.

    library(ggplot2)
    
    # Create an example dataframe with date-time values
    phase_summary &lt;- data.frame(
      Phase = c(&quot;Phase 1&quot;, &quot;Phase 2&quot;, &quot;Phase 3&quot;, &quot;Phase 4&quot;),
      Starts = as.POSIXct(c(&quot;2021-01-01 09:00:00&quot;, &quot;2021-02-01 12:30:00&quot;, &quot;2021-03-01 10:15:00&quot;, &quot;2021-04-01 08:45:00&quot;)),
      Finishes = as.POSIXct(c(&quot;2021-01-31 16:00:00&quot;, &quot;2021-02-28 17:30:00&quot;, &quot;2021-03-31 14:45:00&quot;, &quot;2021-04-30 13:15:00&quot;))
    )
    
    # Create a Gantt chart
    ggplot(data = phase_summary, aes(x = Phase, y = 1, yend = 1, xmin = Starts, xmax = Finishes)) +
      geom_linerange(size = 5, color = &quot;steelblue&quot;) +
      scale_y_continuous(breaks = 1, labels = &quot;&quot;) +
      scale_x_datetime(date_labels = &quot;%b %d\n%Y&quot;, expand = c(0.1, 0)) +
      labs(title = &quot;Project Timeline&quot;,
           x = &quot;Phases&quot;,
           y = &quot;&quot;) +
      theme_bw()

I'm getting the following error:

> Error: Invalid input: time_trans works with objects of class POSIXct
> only.

This is confusing me as Starts and Finises are POSIXct

答案1

得分: 2

这是我的建议:

# 创建一个带有日期时间值的示例数据框
phase_summary <- data.frame(
  Phase = c("Phase 1", "Phase 2", "Phase 3", "Phase 4"),
  Starts = as.POSIXct(c("2021-01-01 09:00:00", "2021-02-01 12:30:00", "2021-03-01 10:15:00", "2021-04-01 08:45:00")),
  Finishes = as.POSIXct(c("2021-01-31 16:00:00", "2021-02-28 17:30:00", "2021-03-31 14:45:00", "2021-04-30 13:15:00"))
)

# 创建甘特图
ggplot(data = phase_summary, aes(x = Starts, xend = Finishes, y = Phase, yend = Phase, color = Phase)) +
  scale_x_datetime(date_labels = "%b %d\n%Y", expand = c(0.1, 0)) +
  labs(title = "项目时间线",
       x = "时间",
       y = "阶段") +
  theme_bw() + geom_segment(size = 8)

在R中尝试创建甘特图。

英文:

Here is my suggestion:

# Create an example dataframe with date-time values
phase_summary &lt;- data.frame(
  Phase = c(&quot;Phase 1&quot;, &quot;Phase 2&quot;, &quot;Phase 3&quot;, &quot;Phase 4&quot;),
  Starts = as.POSIXct(c(&quot;2021-01-01 09:00:00&quot;, &quot;2021-02-01 12:30:00&quot;, &quot;2021-03-01 10:15:00&quot;, &quot;2021-04-01 08:45:00&quot;)),
  Finishes = as.POSIXct(c(&quot;2021-01-31 16:00:00&quot;, &quot;2021-02-28 17:30:00&quot;, &quot;2021-03-31 14:45:00&quot;, &quot;2021-04-30 13:15:00&quot;))
)

# Create a Gantt chart
ggplot(data = phase_summary, aes(x = Starts, xend=Finishes,y = Phase, yend = Phase, color=Phase)) +
  scale_x_datetime(date_labels = &quot;%b %d\n%Y&quot;, expand = c(0.1, 0))+
  labs(title = &quot;Project Timeline&quot;,
       x = &quot;Time&quot;,
       y = &quot;Phase&quot;) +
  theme_bw() + geom_segment(size=8) 

在R中尝试创建甘特图。

答案2

得分: 1

你的问题是你已经将 x = Phase 赋值给了 x。虽然 StartFinishPOSIXct 类型,但图表试图将一个字符变量映射到日期时间轴上,并失败了。

要获得你所期望的样式,你可以这样做:

ggplot(data = within(phase_summary, Phase <- factor(Phase, rev(Phase))),
       aes(y = Phase, xmin = Starts, xmax = Finishes, group = Phase)) +
  geom_linerange(size = 5, color = "steelblue") +
  geom_text(aes(y = "Phase 4", label = Phase, 
                x = Starts + (Finishes - Starts)/2), nudge_y = -0.5) +
  scale_x_datetime(date_labels = "%b %d\n%Y", expand = c(0.1, 0),
                   breaks = seq(as.POSIXct("2021-01-01"), 
                                by = "month", len = 12), name = "Phases") +
  labs(title = "Project Timeline") +
  theme_bw() +
  theme(axis.ticks.y = element_blank(),
        axis.text.y = element_blank(),
        axis.title.y = element_blank())

在R中尝试创建甘特图。



<details>
<summary>英文:</summary>

Your problem is that you have assigned `x = Phase`. Although `Start` and `Finish` are `POSIXct`, the plot is attempting to map a character variable to a date-time axis and failing.

To get the kind of style you are looking for, you can do:

ggplot(data = within(phase_summary, Phase <- factor(Phase, rev(Phase))),
aes(y = Phase, xmin = Starts, xmax = Finishes, group = Phase)) +
geom_linerange(size = 5, color = "steelblue") +
geom_text(aes(y = "Phase 4", label = Phase,
x = Starts + (Finishes - Starts)/2), nudge_y = -0.5) +
scale_x_datetime(date_labels = "%b %d\n%Y", expand = c(0.1, 0),
breaks = seq(as.POSIXct("2021-01-01"),
by = "month", len = 12), name = "Phases") +
labs(title = "Project Timeline") +
theme_bw() +
theme(axis.ticks.y = element_blank(),
axis.text.y = element_blank(),
axis.title.y = element_blank())

[![enter image description here][1]][1]


  [1]: https://i.stack.imgur.com/mnlJS.png

</details>



huangapple
  • 本文由 发表于 2023年2月19日 22:37:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75500877.html
匿名

发表评论

匿名网友

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

确定