我如何在 timevis 中自动换行?

How do I word wrap in timevis?

我在 R 中用 timevis 制作了甘特图。我的甘特图中的一些条形图有很长的文字,所以大约第 10 个字之后的任何内容都被截断了。有没有办法把单词换到第二行或第三行?

gantt <- structure(list(group = c("How FAN1 stabilises repeats", "How FAN1 stabilises repeats"
), content = c("CRISPR nuclease and SPYF", "Effect of FAN1 SPAA and MIM mutant on MLH1 binding, ICL repair and CAG instability"
), start = structure(c(1662516351.36, 1662516351.36), tzone = "UTC", class = c("POSIXct", 
                                                                               "POSIXt")), end = structure(c(1677242989.44, 1677242989.44), tzone = "UTC", class = c("POSIXct", 
                                                                                                                                                                     "POSIXt")), style = c("color: white; background-color: maroon;font-size:9px;word-break: break-all;", 
                                                                                                                                                                                           "color: white; background-color: maroon;font-size:9px;word-break: break-all;"
                                                                                                                                                                     ), type = c("range", "range")), row.names = c(NA, -2L), class = c("tbl_df", 
                                                                                                                                                                                                                                       "tbl", "data.frame"))    


groups <- data.frame(id = unique(gantt$group),
                     content = unique(gantt$group),
                     style = "font-weight: bold; font-size:10px")

timevis(data = gantt, groups = groups,
        options = list(editable = TRUE, height = "600px"))

您可以使用 stringr::str_wrap 将文本很好地环绕到所需的最大宽度。它通过插入在此上下文中不起作用的换行符来工作,因此您需要将它们替换为 <br>:

library(tidyverse)

gantt %>%
  mutate(content = gsub("\n", "<br>", str_wrap(content, 30))) %>%
  timevis(groups = groups, options = list(editable = TRUE, height = "600px"))