R语言 如何安装图标以添加到夸托文件中的ggplot2标题?

ljsrvy3e  于 2023-05-26  发布在  其他
关注(0)|答案(2)|浏览(135)

我在尝试在夸托文件中实现以下解决方案(来自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\"'>&#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()
  )

正如答案所指出的,我需要通过调用字体真棒字体

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",这让我相信调用甚至没有指向正确的文件。

iqxoj9l9

iqxoj9l91#

我相信字体家族应该是Font Awesome 6 Brands(我只从桌面安装了otf字体)。如果您能够切换到AGG图形设备(必须先安装ragg软件包),字体处理和访问将简化很多。
Knitr也应该为夸托使用ragg_png,这可以通过YAML块中的knitr:opts_chunk进行配置:

knitr:
  opts_chunk:
    dev: ragg_png

要访问某些字体变体(例如使用FA Solid而不是FA Regular),我们可以先使用register_variant()
使用Font Aweseome的ggtext::geom_richtext(),注册fa-solid字体家族以访问FA Solid图标,并在ggplot标题中使用相同的fa-solid

# reprex must use ragg_png, otherwise FA fonts are not found
 
#+ setup, include=FALSE
knitr::opts_chunk$set(dev = "ragg_png")

#+ main
library(systemfonts)
library(dplyr)
library(ggplot2)
library(ggtext)  

reset_font_cache()

# list Font Awsome fonts
system_fonts() %>% 
  filter(grepl("Awesome", family )) %>%
  glimpse()
#> Rows: 3
#> Columns: 9
#> $ path      <chr> "C:\\Users\\marguslt\\AppData\\Local\\Microsoft\\Windows\\Fo…
#> $ index     <int> 0, 0, 0
#> $ name      <chr> "FontAwesome6Brands-Regular", "FontAwesome6Free-Regular", "F…
#> $ family    <chr> "Font Awesome 6 Brands", "Font Awesome 6 Free", "Font Awesom…
#> $ style     <chr> "Regular", "Regular", "Solid"
#> $ weight    <ord> normal, normal, heavy
#> $ width     <ord> normal, normal, normal
#> $ italic    <lgl> FALSE, FALSE, FALSE
#> $ monospace <lgl> FALSE, FALSE, FALSE

system_fonts() %>% 
  filter(grepl("Awesome", family )) %>%
  mutate(path = basename(path)) %>% 
  select(-c(index, family))
#> # A tibble: 3 × 7
#>   path                                 name  style weight width italic monospace
#>   <chr>                                <chr> <chr> <ord>  <ord> <lgl>  <lgl>    
#> 1 Font Awesome 6 Brands-Regular-400.o… Font… Regu… normal norm… FALSE  FALSE    
#> 2 Font Awesome 6 Free-Regular-400.otf  Font… Regu… normal norm… FALSE  FALSE    
#> 3 Font Awesome 6 Free-Solid-900.otf    Font… Solid heavy  norm… FALSE  FALSE

# register new family to access "Font Awesome 6 Free-Solid-900.otf"
register_variant(
  name = "fa-solid", 
  family = "Font Awesome 6 Free", 
  weight = "heavy"
)

ggplot() + 
  geom_richtext(aes(x = 0, y = 1, 
                    label = "rich <span style='font-size:60pt; 
                    font-family: \"Font Awesome 6 Brands\"'>github</span>
                    text<br>Font Awesome 6 Brands")) +
  geom_richtext(aes(x = 1, y = 1, 
                    label = "rich <span style='font-size:60pt; 
                    font-family: \"Font Awesome 6 Free\"'>chart-bar</span>
                    text<br>Font Awesome 6 Free <br>Regular")) +
  geom_richtext(aes(x = 2, y = 1, 
                    label = "rich <span style='font-size:60pt; 
                    font-family: \"fa-solid\"'>chart-simple</span>
                    text<br>Font Awesome 6 Free <br>Solid")) +
  # coord_fixed(ratio = 2) +
  scale_x_discrete(expand = expansion(mult = .3), name = "gear") +
  scale_y_discrete(expand = expansion(mult = .2), limits = c(0,2), name = "bars") +
  coord_fixed(ratio = 2) +
  ggtitle("globe city compass") +
  theme_minimal() +
  theme(title = element_text(family = "fa-solid", size = 20))
#> Warning: Continuous limits supplied to discrete scale.
#> ℹ Did you mean `limits = factor(...)` or `scale_*_continuous()`?

创建于2023-05-24使用reprex v2.0.2

46qrfjad

46qrfjad2#

基于@margusl提供的示例,我能够将文本与图标混合并匹配,以生成我想要的标题。我想我会把这些加到提供的优秀答案中。

library(glue)
this_text <- "variables"

ggplot() +
    labs(
    title = "<span style='font-size:16pt; color: #065df4; font-family: \"fa-solid\"'>chart-line</span> 
    Mix text and icons for your plot title  <span style='font-size:16pt; color: #919191; font-family: \"fa-solid\"'>chart-simple</span>",
    subtitle = glue::glue("<span style='font-size:16pt; color: #FFFF00; font-family: \"fa-solid\"'>lightbulb</span>",
                          " You can also use ",
                          this_text,
                          " to add dynamic text with glue::glue. ",
                          "<span style='font-size:16pt; color: #008000; font-family: \"Font Awesome 6 Free\"'>thumbs-up</span>")
  ) +
  theme(plot.title = element_textbox_simple(),
        plot.subtitle = element_textbox_simple())

相关问题