【问题标题】:Show flexible, data-aware Gantt Chart Segments in ggplot2在 ggplot2 中显示灵活、数据感知的甘特图段
【发布时间】:2020-12-25 18:53:49
【问题描述】:

由于某种原因,X 轴以固定间距绘制,不代表实际时间段。 黑色的任期段都具有相同的大小,尽管它们应该显示不同的任期。

我尝试了以下两个选项,但轴没有改变。:

scale_x_date(limits = c(as.Date("06.10.2017","31.12.2020")))

min <- ("06.10.2017")
max <- ("31.12.2020")
scale_x_date(limits = c(min, max))

三个黑色段的长度应根据各自任期的长度(即结束日期 - 开始日期) 我不明白如何解决这个问题。

library(ggplot2)
library(ggthemes)
library(dplyr)

data <- data.frame(name = c(
  "Hank Kowalski",
  "Bruce Kippler",
  "Gerd Jennson"),

  start = c(   
    "31.12.2020",
    "01.07.2018",
    "06.10.2017"),
  
  end = c(
    "03.11.2019",
    "03.11.2019",
    "30.06.2018")
   )

data %>%
  mutate(start=as.Date(start,'%d.%m.%Y'),
         end=as.Date(end,'%d.%m.%Y'), #'%d.%m.%Y'
         name=factor(name,levels = unique(name),ordered = T)) %>%
  mutate(start=as.character(start),
         end=as.character(end)) %>%
  ggplot(aes(x=start, xend=end, y=name, yend=name)) +
  geom_segment(size=3) +
  labs(title='Team Coaches', size= 6, x='Tenure', y='Coach') +
  scale_colour_manual(values = c(CDU = 'black', FDP = 'yellow', Grüne = 'green' , parteilos = 'grey', SPD = 'red')) +
  theme_wsj() + 
  theme(axis.title = element_text(),text = element_text(size=8),
        axis.text.x = element_text(angle=45, hjust=1)) +
  theme(axis.text.y = element_text(lineheight = 2, size = 6))

【问题讨论】:

  • 删除mutate(start=as.character(start), end=as.character(end)),通过强制转换为字符,它们不再是日期,ggplot 将反过来强制将日期转换为因子,您将在 x 轴上增加 1。 如果可行,请将问题或 VTC 作为简单错误删除?
  • 但是有了这个,X 轴不再按时间顺序排序。
  • 是的。它按日期排序。

标签: r ggplot2 gantt-chart


【解决方案1】:

这里有证据表明,如果移除对字符的强制,x 轴仍按日期排序。
我已经注释掉了theme_wsj,因为我的字体有问题。

data %>%
  mutate(start=as.Date(start,'%d.%m.%Y'),
         end=as.Date(end,'%d.%m.%Y'), #'%d.%m.%Y'
         name=factor(name,levels = unique(name),ordered = T)) %>%
  #mutate(start=as.character(start),
  #       end=as.character(end)) %>%
  ggplot(aes(x=start, xend=end, y=name, yend=name)) +
  geom_segment(size=3) +
  labs(title='Team Coaches', size= 6, x='Tenure', y='Coach') +
  scale_colour_manual(values = c(CDU = 'black', FDP = 'yellow', Grüne = 'green' , parteilos = 'grey', SPD = 'red')) +
  #theme_wsj() +
  theme(axis.title = element_text(),text = element_text(size=8),
        axis.text.x = element_text(angle=45, hjust=1)) +
  theme(axis.text.y = element_text(lineheight = 2, size = 6))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-18
    • 1970-01-01
    • 2021-06-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多