ggplot2 主题元素中的 Markdown。合并两列以制作一个具有不同字体的刻度标签
ggplot2 Markdown in theme elements. Combining two columns to make one tick label with different fonts
我正在尝试将此解决方案 https://github.com/wilkelab/ggtext 应用于我的数据,以根据两列的组合创建刻度标签,并为每列应用不同的字体。我想我已经根据我的场景调整了示例代码,但是在 mutate 阶段之后出现了这个错误:
Error in check_breaks_labels(breaks, labels) : object 'name' not found
感谢任何指点。
我正在寻找类似下面的内容。
这是我试过的代码:
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")
正如@Duck 已经指出的,您的代码中存在一些小问题。不幸的是,这些无法解决 ggtext
问题
当你想在 ggtext
中使用 markdown 元素时,你必须通过设置例如 element_markdown
使用 element_markdown
axis.text.y = element_markdown()
里面 theme()
.
要使样式正常工作,您必须通过 HTML 实体 .
转义 Comname 中的点 .
。 (原因是数字后跟 .
标记降价中有序列表的开始。)
进行这些更改后,您不再需要 scale_y_discrete
。 ggplot2
将默认使用 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, "\.", "."),
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())
我正在尝试将此解决方案 https://github.com/wilkelab/ggtext 应用于我的数据,以根据两列的组合创建刻度标签,并为每列应用不同的字体。我想我已经根据我的场景调整了示例代码,但是在 mutate 阶段之后出现了这个错误:
Error in check_breaks_labels(breaks, labels) : object 'name' not found
感谢任何指点。
我正在寻找类似下面的内容。
这是我试过的代码:
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")
正如@Duck 已经指出的,您的代码中存在一些小问题。不幸的是,这些无法解决 ggtext
问题
当你想在
ggtext
中使用 markdown 元素时,你必须通过设置例如element_markdown
使用element_markdown
axis.text.y = element_markdown()
里面theme()
.要使样式正常工作,您必须通过 HTML 实体
.
转义 Comname 中的点.
。 (原因是数字后跟.
标记降价中有序列表的开始。)进行这些更改后,您不再需要
scale_y_discrete
。ggplot2
将默认使用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, "\.", "."),
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())