【问题标题】:Highlight/multicolor substring of axis tick label in ggplotggplot中轴刻度标签的突出显示/多色子字符串
【发布时间】:2020-09-05 06:07:48
【问题描述】:

我很好奇如何在 x 轴上突出显示或多色文本,这是输出示例,我如何在单词 'TGFB1-EGFR' 中标记 'TGFB1' 的红色,这是对生物学中的配体和受体。如果您有一些想法或知道其他可以实现此目的的软件包,请告诉我,提前谢谢。

最好的, 魏

【问题讨论】:

    标签: ggplot2 colors label substring highlight


    【解决方案1】:

    我发现ggtext github 包这样做非常方便。它允许文本的 html 和 markdown 样式。这是一个使用 Iris 数据集的示例,您可以将其应用于您自己的数据。

    简而言之,您将标签包装在 html 颜色编码中,并将主题元素设置为 markdown 元素。

    library(ggtext) # remotes::install_github("wilkelab/ggtext")
    library(ggplot2)
    
    ggplot(iris, aes(Species, Sepal.Length)) +
      geom_boxplot() +
      scale_x_discrete(
        labels = function(x) {
          paste0("<i style='color:#FF0000'>Iris </i>",
                 "<i style='color:#0000FF'>", x ,"</i>")
        }
      ) +
      theme(axis.text.x = element_markdown())
    

    reprex package (v0.3.0) 于 2020 年 5 月 19 日创建

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-08
      • 1970-01-01
      • 2012-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-10
      相关资源
      最近更新 更多