如何使用ggplot2在甘特图中有条件地加粗y轴文本

How do to conditionally bold y axis text in gantt chart using ggplot2

我正在尝试使用主题根据属性 ActivitySort 有条件地设置元素文本。如果该值为 0,我想将文本加粗,否则我希望它保持简洁。

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")
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,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 <- plot + facet_grid(rows = vars(StrategyName),  scales="free")


plot <- plot + theme(axis.text.y=  element_text(face=ifelse((dataset$ActivitySort == 0),"bold","plain")))

plot

代码当前使用 ifelse 将文本加粗,但结果与预期不符。我只想将策略、黑线项目和策略状态加粗。请注意,目前只有第二个策略是粗体,期望两个策略都是粗体。

您的问题在于您将数据拆分为两个方面并将 y 轴设置为自由。这意味着它们不一样。通过 ifelse 语句,您只是传递与外部数据集相关的 9 个 TRUEFALSE 值,即您的上图获得 9 个值(其中前 4 个是FALSE 因此没有什么是粗体),而你的下图也有相同的 9 个值,因为第 5 个是 TRUEStrategy 2 是粗体。
您可以通过从 facet_grid 中删除 scales="free" 部分来轻松检查这一点。你应该得到这样的东西:

因此,您必须使用自定义函数,根据每个标签的内容将其加粗。 我修改了 .

的代码
bold_labels <- function(breaks) {
  strategy <- filter(dataset, Activity %in% breaks) %>% 
    mutate(check = str_detect(Activity, "Strategy")) %>% 
    pull(check)
  labels <- purrr::map2(
    breaks, strategy,
    ~ if (.y) bquote(bold(.(.x))) else bquote(plain(.(.x)))
  )
  parse(text = labels)
}
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") +
  scale_y_discrete(labels = bold_labels) +
  facet_grid(rows = vars(StrategyName), scales="free_y")
plot

结果: