gpt4 book ai didi

r - 如何使用带有 ggplot2 的甘特图 R 的 facet_grid

转载 作者:行者123 更新时间:2023-12-04 15:42:23 24 4
gpt4 key购买 nike

我有一个基本的甘特图,代码如下

require("tidyverse")

task0 <- c('Strategy 1', 'Strategy 1', '2017-04-01', '2020-04-01',0, "Strategy")
task1 <- c('Strategy 1', 'Collect data', '2017-04-01', '2018-04-01',1, "In Progress")
task2 <- c('Strategy 1', 'Clean data', '2018-04-01', '2018-06-01', 1, "Completed")
task3 <- c('Strategy 1', 'Analyse data', '2018-06-01', '2019-04-01',1, "Discontinued")
task4 <- c('Strategy 1', 'Write report', '2019-04-01', '2020-04-01', 1, "Planned")
task10 <- c('Strategy 2', 'Strategy 2', '2017-04-01', '2020-04-01',0, "Strategy")
task11 <- c('Strategy 2', 'Collect data again', '2017-04-01', '2018-04-01',1, "In Progress")
task12 <- c('Strategy 2', 'Clean data again', '2018-04-01', '2018-06-01', 1, "Completed")
task13 <- c('Strategy 2', 'Analyse data again', '2018-06-01', '2019-04-01',1, "Discontinued")
task14 <- c('Strategy 2', 'Write report again', '2019-04-01', '2020-04-01', 1, "Planned")

dataset <- as.data.frame(rbind(task0, task1, task2, task3, task4,task10, task11, task12, task13, task14))
names(dataset) <- c('StrategyName', 'Activity', 'Start', 'End', 'ActivitySort', "Status")


dataset <- as_tibble(dataset)
dataset <- dataset %>% mutate(StartSort = as.Date(Start, "%Y-%m-%d" ))
dataset <- dataset %>% arrange(desc(StrategyName), desc(ActivitySort), desc(StartSort),Activity, End)

acts <- c("Strategy", "Completed","In Progress", "Discontinued","Planned")
actcols <- c("#000000","#548235", "#2E75B6", "#BF9000", "#7030A0")
els <-unique(dataset$Activity)

g.gantt <- gather(dataset, "state", "date", 3:4) %>% mutate(date = as.Date(date, "%Y-%m-%d" ), Status=factor(Status, acts[length(acts):1]), Activity=factor(Activity, els))

plot <- ggplot(g.gantt, aes(x = date, y = Activity, color = Status, group=Activity)) +
geom_line(size = 5) +
scale_color_manual(values=actcols, name="Status", breaks = acts, limits = acts) +
labs(x="Project year", y=NULL, title="Activity timeline")

plot + theme(axis.text.y = element_text(hjust = 0))

生成以下图表:

enter image description here

我想通过使用 facet_grid 并添加以下代码使图表更具可读性

+ facet_grid(rows = vars(StrategyName))

不幸的是,这会产生以下图表,其中每个策略都重复了每个事件。

enter image description here

如何分面并删除每个分面中的空白/重复行?

最佳答案

下面的代码似乎解决了重复问题。

+ facet_grid(rows = vars(StrategyName),  scales="free")

关于r - 如何使用带有 ggplot2 的甘特图 R 的 facet_grid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57365600/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com