如何将图标添加到 ggplot 标题和标题?

How to add icons to ggplot captions and titles?

现在我正在尝试将图标放入 ggplot 可视化中。为此,我在网上查看了几个教程,当我 运行 下面是 Claus Wilke 的代码示例时,

library(ggtext)
library(ggplot2)

ggplot(mtcars) +
  aes(mpg, disp) +
  geom_point() +
  labs(title = "This font is awesome:
       <span style='font-family: \"Font Awesome 6 Free Solid\"'>&#62766; &#62650; &#62577;</span>") +
  theme(plot.title = element_markdown())

一切似乎都很好,您可以在以下结果中看到

然而,为了模仿我的另一个可视化代码,比方说上周 TidyTuesday 挑战 Babynames 数据,我使用以下代码,

library(ggplot2)
library(ggtext)
library(tidyverse)
library(showtext)
library(ggrepel)


font_add_google("Martel", family = "title")
font_add_google("Libre Caslon Display", family = "subtitle")
font_add_google("Space Mono", family = "axis")
font_add_google("Spartan", family = "caption")

showtext_auto()

tuesdata <- tidytuesdayR::tt_load('2022-03-22')
babynames <- tuesdata$babynames


babynames %>%
  group_by(year) %>%
  top_n(1, n) %>%
  ggplot(aes(x = year, y = prop)) +
  geom_step(size = 0.5, show.legend = FALSE, direction = "hv") +
  theme_bw() +
  labs(x = "Year", 
       y = "Proportion",
       title = "How Unique are Names in the United States?",
       subtitle = "This visualization illustrates the proportion of most given baby names in that year between 1880 and 2017",
       caption = "Source: {babynames} package | Plot: <span style='font-family: \"Font Awesome 6 Brands\"'>&#xf09b;</span> muhammetozkrca | TidyTuesday-Week 12") +
  background_grid(major = 'none', minor = "none") +
  theme(
    plot.title = element_text(hjust = 0.5, family = "title", size = 20),
    plot.subtitle = element_markdown(hjust = 0.5, family = "subtitle", size = 14), # note that to color numbers differently, I used element_markdown not element_text function
    plot.caption = element_markdown(hjust = 0.5, size = 7),
    legend.position = c(0.9, 0.6),
    legend.justification = "center",
    legend.title = element_text(family = "caption", hjust = 1, vjust = 0.7),
    legend.title.align = 0.5,
    axis.title.x = element_text(family = "axis"),
    axis.title.y = element_text(family = "axis"),
    panel.border = element_blank(),
    axis.ticks = element_blank())

我收到了这个错误

Error in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y,  : 
  polygon edge not found

有时候,我也会收到这个错误

In grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y,  :
  font family 'Font Awesome 6 Free Solid' not found, will use 'wqy-microhei' instead

综上所述,我不太明白这是怎么回事。当我搜索类似的查询时,我看到问题主要是由于用户的电脑上没有字体文件。然而,如果这对我来说是个问题,我认为我无法重现 Wilke 的例子。因此,如果您能帮助我解决这个问题,我将不胜感激。提前谢谢你。

问题是您使用 showtext 进行文本渲染。这样做需要通过 font_add 添加本地字体,以便它们可以在绘图中使用:

library(ggplot2)
library(ggtext)
library(showtext)
#> Loading required package: sysfonts
#> Loading required package: showtextdb
library(ggrepel)
library(cowplot)

font_add_google("Martel", family = "title")
font_add_google("Libre Caslon Display", family = "subtitle")
font_add_google("Space Mono", family = "axis")
font_add_google("Spartan", family = "caption")

fa_path <- systemfonts::font_info(family = "Font Awesome 6 Brands Regular")[["path"]]
font_add(family = "fa-brands", regular = fa_path)

showtext_auto()

ggplot(babynames, aes(x = year, y = prop)) +
  geom_step(size = 0.5, show.legend = FALSE, direction = "hv") +
  theme_bw() +
  labs(
    x = "Year",
    y = "Proportion",
    title = "How Unique are Names in the United States?",
    subtitle = "This visualization illustrates the proportion of most given baby names in that year between 1880 and 2017",
    caption = "Source: {babynames} package | Plot: <span style='font-family: \"fa-brands\"'>&#xf09b;</span> muhammetozkrca | TidyTuesday-Week 12"
  ) +
  background_grid(major = "none", minor = "none") +
  theme(
    plot.title = element_text(hjust = 0.5, family = "title", size = 20),
    plot.subtitle = element_markdown(hjust = 0.5, family = "subtitle", size = 14),
    plot.caption = element_markdown(hjust = 0.5, size = 14),
    legend.position = c(0.9, 0.6),
    legend.justification = "center",
    legend.title = element_text(family = "caption", hjust = 1, vjust = 0.7),
    legend.title.align = 0.5,
    axis.title.x = element_text(family = "axis"),
    axis.title.y = element_text(family = "axis"),
    panel.border = element_blank(),
    axis.ticks = element_blank()
  )

数据

babynames <- structure(list(
  X = c(
    875979L, 895453L, 915066L, 934495L, 954004L,
    973836L, 994240L, 1015321L, 1037346L, 1060379L, 1084841L, 1109784L,
    1135041L, 1160654L, 1186574L, 1212571L, 1238789L, 1265481L, 1292890L,
    1321132L, 1350394L, 1380480L, 1410860L, 1441772L, 1473351L, 1505753L,
    1539167L, 1573759L, 1608606L, 1623219L, 1657921L, 1691988L, 1725891L,
    1759623L, 1792892L, 1826120L, 1859218L, 1892197L
  ), year = 1980:2017,
  sex = c(
    "M", "M", "M", "M", "M", "M", "M", "M", "M", "M",
    "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M",
    "M", "M", "M", "M", "M", "M", "M", "F", "F", "F", "F", "F",
    "F", "F", "F", "F"
  ), name = c(
    "Michael", "Michael", "Michael",
    "Michael", "Michael", "Michael", "Michael", "Michael", "Michael",
    "Michael", "Michael", "Michael", "Michael", "Michael", "Michael",
    "Michael", "Michael", "Michael", "Michael", "Jacob", "Jacob",
    "Jacob", "Jacob", "Jacob", "Jacob", "Jacob", "Jacob", "Jacob",
    "Jacob", "Isabella", "Isabella", "Sophia", "Sophia", "Sophia",
    "Emma", "Emma", "Emma", "Emma"
  ), n = c(
    68693L, 68765L, 68228L,
    67995L, 67736L, 64906L, 64205L, 63647L, 64133L, 65382L, 65282L,
    60785L, 54386L, 49550L, 44470L, 41402L, 38365L, 37548L, 36614L,
    35361L, 34471L, 32541L, 30568L, 29630L, 27879L, 25830L, 24841L,
    24273L, 22591L, 22298L, 22905L, 21837L, 22304L, 21213L, 20924L,
    20435L, 19471L, 19738L
  ), prop = c(
    0.03703079, 0.03692247,
    0.03615445, 0.0364911, 0.03610228, 0.03373805, 0.03342343,
    0.03264834, 0.03204521, 0.03120182, 0.03034735, 0.0286831,
    0.02591513, 0.02399562, 0.02182099, 0.02058782, 0.01915021,
    0.01879903, 0.01806194, 0.01734789, 0.01651392, 0.01573989,
    0.0147991, 0.01410917, 0.0131991, 0.01214938, 0.01133859,
    0.01096627, 0.0103702, 0.01102237, 0.01169646, 0.011285,
    0.01151924, 0.01102629, 0.01072117, 0.01050471, 0.01009677,
    0.0105275
  )
), class = c("grouped_df", "tbl_df", "tbl", "data.frame"), row.names = c(NA, -38L), groups = structure(list(
  year = 1980:2017,
  .rows = structure(list(
    1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L,
    10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L,
    21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L,
    32L, 33L, 34L, 35L, 36L, 37L, 38L
  ), ptype = integer(0), class = c(
    "vctrs_list_of",
    "vctrs_vctr", "list"
  ))
), row.names = c(NA, -38L), class = c(
  "tbl_df",
  "tbl", "data.frame"
), .drop = TRUE))