ggplot2 Markdown 在主题元素中。组合两列以制作一个具有不同字体的刻度标签

Posted

技术标签:

【中文标题】ggplot2 Markdown 在主题元素中。组合两列以制作一个具有不同字体的刻度标签【英文标题】:ggplot2 Markdown in theme elements. Combining two columns to make one tick label with different fonts 【发布时间】:2020-12-26 20:16:07 【问题描述】:

我正在尝试将此解决方案 https://github.com/wilkelab/ggtext 应用于我的数据,以从两列的组合创建刻度标签,并为每列应用不同的字体。我想我已经根据我的场景调整了示例代码,但是在 mutate 阶段之后出现了这个错误:

check_breaks_labels(breaks, labels) 中的错误:找不到对象“名称”

任何指针表示赞赏。

我正在寻找类似下面的东西。

这是我尝试过的代码:

df$Colour <- as.factor(df$Colour)
df$Number <- as.factor(df$Number)
df$Family <- factor(df$Family, levels = c("Ardeidae"))

df %>% mutate(
  name = glue("<i style=Comname (Sciname</i>)"))%>%

ggplot(df[df$Value != 0,], aes(x=Sample, y=Number)) +
  geom_point(aes(size=Value, alpha = 0.9)) +
  scale_y_discrete(labels= name) +
  facet_grid(Family ~ Year, scales = "free", space = "free") 

还有一些示例数据。

df <- structure(list(Year = c("1984 - 1989", "2017 - 2020", "1984 - 1989", 
"2017 - 2020", "1984 - 1989", "2017 - 2020", "1984 - 1989", "2017 - 2020", 
"1984 - 1989", "2017 - 2020", "1984 - 1989", "2017 - 2020", "1984 - 1989", 
"2017 - 2020", "1984 - 1989", "2017 - 2020", "1984 - 1989", "2017 - 2020", 
"1984 - 1989", "2017 - 2020", "1984 - 1989", "2017 - 2020", "1984 - 1989", 
"2017 - 2020"), Sample = c("Developed_zone_1992", "Developed_zone_2020", 
"Paddock_zone_1992", "Paddock_zone_2020", "Sanctuary_zone_1992", 
"Sanctuary_zone_2020", "Developed_zone_1992", "Developed_zone_2020", 
"Paddock_zone_1992", "Paddock_zone_2020", "Sanctuary_zone_1992", 
"Sanctuary_zone_2020", "Developed_zone_1992", "Developed_zone_2020", 
"Paddock_zone_1992", "Paddock_zone_2020", "Sanctuary_zone_1992", 
"Sanctuary_zone_2020", "Developed_zone_1992", "Developed_zone_2020", 
"Paddock_zone_1992", "Paddock_zone_2020", "Sanctuary_zone_1992", 
"Sanctuary_zone_2020"), Colour = structure(c(1L, 1L, 2L, 2L, 
3L, 3L, 1L, 1L, 2L, 2L, 3L, 3L, 1L, 1L, 2L, 2L, 3L, 3L, 1L, 1L, 
2L, 2L, 3L, 3L), .Label = c("1", "2", "3"), class = "factor"), 
    Value = c(0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 2L, 0L, 1L, 
    2L, 0L, 0L, 1L, 0L, 0L, 10L, 1L, 5L, 5L, 0L, 5L), Family = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "Ardeidae", class = "factor"), 
    Comname = c("47. Little egret", "47. Little egret", "47. Little egret", 
    "47. Little egret", "47. Little egret", "47. Little egret", 
    "46. Western great egret", "46. Western great egret", "46. Western great egret", 
    "46. Western great egret", "46. Western great egret", "46. Western great egret", 
    "45. Purple heron", "45. Purple heron", "45. Purple heron", 
    "45. Purple heron", "45. Purple heron", "45. Purple heron", 
    "44. Grey heron", "44. Grey heron", "44. Grey heron", "44. Grey heron", 
    "44. Grey heron", "44. Grey heron"), Sciname = c("Egretta garzetta", 
    "Egretta garzetta", "Egretta garzetta", "Egretta garzetta", 
    "Egretta garzetta", "Egretta garzetta", "Ardea alba", "Ardea alba", 
    "Ardea alba", "Ardea alba", "Ardea alba", "Ardea alba", "Ardea purpurea", 
    "Ardea purpurea", "Ardea purpurea", "Ardea purpurea", "Ardea purpurea", 
    "Ardea purpurea", "Ardea cinerea", "Ardea cinerea", "Ardea cinerea", 
    "Ardea cinerea", "Ardea cinerea", "Ardea cinerea"), Number = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 
    3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c("96", "97", "98", 
    "99"), class = "factor")), row.names = c(NA, -24L), class = "data.frame")

【问题讨论】:

你能先试试这个吗:#Data df1 &lt;- df %&gt;% mutate(name = glue("&lt;i style=Comname (Sciname&lt;/i&gt;)"))%&gt;% filter(Value!=0) 然后ggplot(df1,aes(x=Sample, y=Number)) +geom_point(aes(size=Value, alpha = 0.9)) +scale_y_discrete(labels= df1$name) 【参考方案1】:

正如@Duck 已经指出的那样,您的代码中存在一些小问题。不幸的是,这些并不能解决ggtext 问题

    当您想使用ggtext 时,您必须通过设置例如使用element_markdownaxis.text.y = element_markdown()theme()

    要使样式生效,您必须通过 html 实体 &amp;#46; 转义 Comname 中的点 .。 (原因是一个数字后跟一个. 标志着markdown 中有序列表的开始。)

    进行这些更改后,您不再需要 scale_y_discreteggplot2 默认使用 name 标记刻度

df$Colour <- as.factor(df$Colour)
df$Number <- as.factor(df$Number)
df$Family <- factor(df$Family, levels = c("Ardeidae"))

library(dplyr)
library(glue)
library(ggplot2)
library(ggtext)

df1 <- df %>%
  mutate(
    Comname = stringr::str_replace(Comname, "\\.", "&#46;"),
    name = glue::glue("Comname <i>Sciname</i>")
  ) %>%
  filter(Value != 0) 

df1 %>% 
  ggplot(aes(x = Sample, y = name)) +
  geom_point(aes(size = Value, alpha = 0.9)) +
  facet_grid(Family ~ Year, scales = "free", space = "free") +
  theme(axis.text.y = element_markdown())

【讨论】:

太棒了,谢谢!我现在唯一的问题是排序不太正确(我在这方面遇到了很多麻烦)。我在问题中添加了一张新图片。你能帮忙吗? 嗨@LisaB。根据新图像,您希望类别按降序排列,对吗?这可以实现,例如通过使用forcats::fct_rev。试试ggplot(aes(x = Sample, y = forcats::fct_rev(name))) + ... 谢谢@stefan。除了顺序为 100,101,102,99 的一组之外,这在任何地方都有效。这是我之前遇到的问题,我必须创建 Numbers 列以使它们全部按正确的顺序排列(或者将名称分配为具有级别的因子,这非常耗时)。有什么想法吗? (: 永远忘记“1xx”。在这种情况下ggplot(aes(x = Sample, y = forcats::fct_reorder(name, desc(as.numeric(stringr::str_extract(Comname, "^\\d+"))))))。1. str_extract 提取字符串开头的数字,2. 转换为数字,3. 可以在里面使用fct_reorder 以降序排列标签。 完美。非常感谢。

以上是关于ggplot2 Markdown 在主题元素中。组合两列以制作一个具有不同字体的刻度标签的主要内容,如果未能解决你的问题,请参考以下文章

R语言可视化及作图10--ggplot2的theme函数

1.ggplot2——简介

R语言ggplot2可视化:使用ggthemr包更改ggplot2主题(theme)以及布局使用ggplot2包的默认的各种主题(theme)来调整图形的颜色使用patchwork组合不同主题结果

ggplot2作图详解7(完):主题(theme)设置

如何使用 R markdown 将 ggplot2 图形正确渲染成单词?

如何在R中的ggplot2中绘制组均值的平均值?