能否在同一ggplot标题/说明中使用marquee包的两个自定义span?
解决ggplot标题/说明文字中使用marquee包插入多自定义样式的问题
要在单个标题或说明文字中使用多个不同的自定义span样式,核心是在element_marquee()里用**styles参数(复数)**传入所有定义好的样式列表,而非仅指定单个style。
修改后的完整代码
library(ggplot2) library(ragg) library(marquee) file <- knitr::fig_path('.png') positive_style <- modify_style( classic_style(), "positive_style", color = "black", background = "#FFFFF7", border = "black", border_size = trbl(em(0.1)), padding = trbl(em(0.15)) ) negative_style <- modify_style( classic_style(), "negative_style", color = "black", background = "#e4e4e4", border = "black", border_size = trbl(em(0.1)), padding = trbl(em(0.15)) ) the_title <- "Weekly proportion of cocaine positive {.positive_style +} UDS testing results" the_caption <- "Positive {.positive_style +} versus negative {.negative_style -} UDS test results." agg_png(file, width = 1000, height = 500, res = 144) ggplot() + theme_bw() + labs( title = the_title, caption = the_caption ) + theme( panel.grid.major.x = element_line(color = "gray", linewidth = 0.1), panel.grid.minor.x = element_blank(), panel.grid.minor.y = element_blank(), legend.position = "none", # 关键修改:用styles传入对应样式列表 plot.title = element_marquee( styles = list(positive_style) ), plot.caption = element_marquee( styles = list(positive_style, negative_style) ) ) invisible(dev.off()) knitr::include_graphics(file)
关键修改说明
- 对于仅需单一样式的标题,用
styles = list(positive_style)替代原有的style = positive_style - 对于需要两种样式的说明文字,将两个样式都放入列表
styles = list(positive_style, negative_style),marquee就能正确解析文本中对应的{.positive_style}和{.negative_style}标记,渲染出两个不同的颜色样本。
内容的提问来源于stack exchange,提问作者itsMeInMiami
相关产品推荐
相关产品推荐

