在 ggplot 和 R 中的甘特图中创建一致的条形颜色
Creating consistent bar colors in Gantt chart in ggplot and R
我对多个不同的数据集重复使用相同的 R 代码来生成甘特图。
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")
dataset <- as.data.frame(rbind(task0, task1, task2, task3, task4))
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(StrategyName, desc(ActivitySort), desc(StartSort),Activity, End)
acts <- c("Planned","Discontinued","In Progress","Completed", "Strategy")
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(date, Activity, color = Status, group=Activity)) +
geom_line(size = 5) +
scale_color_manual(values=actcols, name="Status") +
labs(x="Project year", y=NULL, title="Activity timeline")
plot + theme(axis.text.y = element_text(hjust = 0))
如果图表数据包含所有状态代码、策略、进行中、已完成、已停止和已计划,则图表颜色将按预期显示。
但是,我的图表数据不包含所有 5 个状态代码,因此手动颜色设置不正确且不一致。例如,完成状态可能不再显示为绿色。
如何 format/set 手动颜色,以便无论数据集中包含的其他状态的数量如何,已完成的总是显示为绿色?
感谢 teunbrand 的评论回答。我确实需要调整排序。
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")
dataset <- as.data.frame(rbind(task0, task1, task2, task3, task4))
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(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(date, 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))
我对多个不同的数据集重复使用相同的 R 代码来生成甘特图。
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")
dataset <- as.data.frame(rbind(task0, task1, task2, task3, task4))
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(StrategyName, desc(ActivitySort), desc(StartSort),Activity, End)
acts <- c("Planned","Discontinued","In Progress","Completed", "Strategy")
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(date, Activity, color = Status, group=Activity)) +
geom_line(size = 5) +
scale_color_manual(values=actcols, name="Status") +
labs(x="Project year", y=NULL, title="Activity timeline")
plot + theme(axis.text.y = element_text(hjust = 0))
如果图表数据包含所有状态代码、策略、进行中、已完成、已停止和已计划,则图表颜色将按预期显示。
但是,我的图表数据不包含所有 5 个状态代码,因此手动颜色设置不正确且不一致。例如,完成状态可能不再显示为绿色。
如何 format/set 手动颜色,以便无论数据集中包含的其他状态的数量如何,已完成的总是显示为绿色?
感谢 teunbrand 的评论回答。我确实需要调整排序。
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")
dataset <- as.data.frame(rbind(task0, task1, task2, task3, task4))
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(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(date, 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))