我在尝试在夸托文件中实现以下解决方案(来自How to add icons to ggplot captions and titles?)时遇到问题。
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\"'></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()
)
正如答案所指出的,我需要通过调用字体真棒字体
fa_path <- systemfonts::font_info(family = "Font Awesome 6 Brands Regular")[["path"]]
font_add(family = "fa-brands", regular = fa_path)
我不能让这个工作。通过其他帖子的建议,我已经尝试安装Font Awesome 6 Brands-Regular-400.otf
(从桌面版本)以及fa-brands-400.ttf
(从Web版本)。我把它们分开,因为它们的名字是一样的。
试着解决问题我注意到了
fa_path <- systemfonts::font_info(family = "fa-brands-400")[["path"]]
fa_path <- systemfonts::font_info(family = "Font Awesome 6 Brands Regular")[["path"]]
都返回"C:\\WINDOWS\\Fonts\\arial.ttf"
,这让我相信调用甚至没有指向正确的文件。
2条答案
按热度按时间iqxoj9l91#
我相信字体家族应该是
Font Awesome 6 Brands
(我只从桌面安装了otf字体)。如果您能够切换到AGG图形设备(必须先安装ragg
软件包),字体处理和访问将简化很多。Knitr也应该为夸托使用
ragg_png
,这可以通过YAML块中的knitr:opts_chunk
进行配置:要访问某些字体变体(例如使用FA Solid而不是FA Regular),我们可以先使用
register_variant()
。使用Font Aweseome的
ggtext::geom_richtext()
,注册fa-solid
字体家族以访问FA Solid图标,并在ggplot标题中使用相同的fa-solid
:创建于2023-05-24使用reprex v2.0.2
46qrfjad2#
基于@margusl提供的示例,我能够将文本与图标混合并匹配,以生成我想要的标题。我想我会把这些加到提供的优秀答案中。