英文:
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)
#> 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()
)
As the answer points out, I need to call the font awesome fonts via
fa_path <- systemfonts::font_info(family = "Font Awesome 6 Brands Regular")[["path"]]
font_add(family = "fa-brands", 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 <- systemfonts::font_info(family = "fa-brands-400")[["path"]]
fa_path <- systemfonts::font_info(family = "Font Awesome 6 Brands Regular")[["path"]]
both return "C:\\WINDOWS\\Fonts\\arial.ttf"
, 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_chunk
in 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 = "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()`?
<!-- -->
<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_chunk
in 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 = "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()`?
<!-- -->
<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())
英文:
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 <- "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())
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论