如何在quarto文件中安装图标以添加到ggplot2标题中?

huangapple go评论71阅读模式
英文:

How do I install icons to add to ggplot2 title in a quarto file?

问题

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

I am having issues trying to implement the following solution (from How to add icons to ggplot captions and titles?) in a quarto file.

library(ggplot2)
library(ggtext)
library(showtext)
#&gt; Loading required package: sysfonts
#&gt; Loading required package: showtextdb
library(ggrepel)
library(cowplot)

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

fa_path &lt;- systemfonts::font_info(family = &quot;Font Awesome 6 Brands Regular&quot;)[[&quot;path&quot;]]
font_add(family = &quot;fa-brands&quot;, regular = fa_path)

showtext_auto()

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

As the answer points out, I need to call the font awesome fonts via

fa_path &lt;- systemfonts::font_info(family = &quot;Font Awesome 6 Brands Regular&quot;)[[&quot;path&quot;]]
font_add(family = &quot;fa-brands&quot;, regular = fa_path)

I can't get this to work. Through other post suggestions I've tried installing the Font Awesome 6 Brands-Regular-400.otf (from the desktop verstion) as well as fa-brands-400.ttf (from the web version). I've run them separately as they are named the same.

Trying to trouble shoot I've noticed that

fa_path &lt;- systemfonts::font_info(family = &quot;fa-brands-400&quot;)[[&quot;path&quot;]]
fa_path &lt;- systemfonts::font_info(family = &quot;Font Awesome 6 Brands Regular&quot;)[[&quot;path&quot;]]

both return &quot;C:\\WINDOWS\\Fonts\\arial.ttf&quot;, which leads me to believe the calls aren't even pointing to the correct files.

答案1

得分: 1

I believe the font family should be Font Awesome 6 Brands (I have only installed otf fonts from Desktop). If you are able to switch to AGG graphics device (must install ragg package first), font handling and access simplifies quite a lot.

Knitr should use ragg_png for Quarto too, this can be configured through knitr:opts_chunkin YAML block:

knitr:
  opts_chunk:
    dev: ragg_png

To access certain font variants (e.g. use FA Solid instead of FA Regular), we can first use register_variant().

ggtext::geom_richtext() with Font Awesome, registering fa-solid font family to access FA Solid icons and using the same fa-solid in ggplot titles:

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

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

reset_font_cache()

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

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

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

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

如何在quarto文件中安装图标以添加到ggplot2标题中?<!-- -->

<sup>Created on 2023-05-24 with reprex v2.0.2</sup>

英文:

I believe the font family should be Font Awesome 6 Brands (I have only installed otf fonts from Desktop). If you are able to switch to AGG graphics device (must install ragg package first), font handling and access simplifies quite a lot.

Knitr should use ragg_png for Quarto too, this can be configured through knitr:opts_chunkin YAML block:

knitr:
  opts_chunk:
    dev: ragg_png

To access certain font variants (e.g. use FA Solid instead of FA Regular), we can first use register_variant().

ggtext::geom_richtext() with Font Aweseome, registering fa-solid font family to access FA Solid icons and using the same fa-solid in ggplot titles:

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

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

reset_font_cache()

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

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

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

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

如何在quarto文件中安装图标以添加到ggplot2标题中?<!-- -->

<sup>Created on 2023-05-24 with reprex v2.0.2</sup>

答案2

得分: 1

基于@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())

如何在quarto文件中安装图标以添加到ggplot2标题中?

英文:

Based on the examples provided by @margusl I was able to mix and match text with icons to produce the title I was after. Thought I would add these to the excellent answer provided.

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

如何在quarto文件中安装图标以添加到ggplot2标题中?

huangapple
  • 本文由 发表于 2023年5月22日 04:30:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76301813.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定