为什么我在ggiraph中不能使用ggplot2识别的系统字体?

问题描述 投票:0回答:1

我正在尝试使用我组织的 ggplot 主题与 ggiraph 创建交互式绘图。指定的字体如下:

font_light <- "Segoe UI Semilight"

主题使用字体的方式如下:

# font_import() # did this once
loadfonts(device = "win") # do this every session

theme_org <- function() {
   theme(
      # Text Elements
      plot.subtitle = element_text(
        family = font_light,
        size = 11,
        hjust = 0,
        margin = margin(b = 10)
      )
   )

当我使用这个主题生成常规 ggplot 时没有问题。但是当我在 girafe() 调用中使用 ggplot 对象时,出现以下错误:

Error in `girafe()`:
! Font family 'Segoe UI Semilight' has not been found on your system or is not registered.
ℹ You can use a google font with `gdtools::register_gfont()`.
ℹ You can use any font with `systemfonts::register_font()`.

这是我的 Segoe 系统字体:

> systemfonts::system_fonts() %>% filter(grepl("Segoe", family))
# A tibble: 20 × 9
   path                                index name                    family            style     weight width italic monospace
   <chr>                               <int> <chr>                   <chr>             <chr>     <ord>  <ord> <lgl>  <lgl>    
 1 "C:\\Windows\\Fonts\\segmdl2.ttf"       0 SegoeMDL2Assets         Segoe MDL2 Assets Regular   normal norm… FALSE  FALSE    
 2 "C:\\Windows\\Fonts\\segoepr.ttf"       0 SegoePrint              Segoe Print       Regular   normal norm… FALSE  FALSE    
 3 "C:\\Windows\\Fonts\\segoeprb.ttf"      0 SegoePrint-Bold         Segoe Print       Bold      bold   norm… FALSE  FALSE    
 4 "C:\\Windows\\Fonts\\segoesc.ttf"       0 SegoeScript             Segoe Script      Regular   normal norm… FALSE  FALSE    
 5 "C:\\Windows\\Fonts\\segoescb.ttf"      0 SegoeScript-Bold        Segoe Script      Bold      bold   norm… FALSE  FALSE    
 6 "C:\\Windows\\Fonts\\segoeui.ttf"       0 SegoeUI                 Segoe UI          Regular   normal norm… FALSE  FALSE    
 7 "C:\\Windows\\Fonts\\seguibl.ttf"       0 SegoeUIBlack            Segoe UI          Black     heavy  norm… FALSE  FALSE    
 8 "C:\\Windows\\Fonts\\seguibli.ttf"      0 SegoeUIBlack-Italic     Segoe UI          Black It… heavy  norm… TRUE   FALSE    
 9 "C:\\Windows\\Fonts\\segoeuib.ttf"      0 SegoeUI-Bold            Segoe UI          Bold      bold   norm… FALSE  FALSE    
10 "C:\\Windows\\Fonts\\segoeuiz.ttf"      0 SegoeUI-BoldItalic      Segoe UI          Bold Ita… bold   norm… TRUE   FALSE    
11 "C:\\Windows\\Fonts\\seguiemj.ttf"      0 SegoeUIEmoji            Segoe UI Emoji    Regular   normal norm… FALSE  FALSE    
12 "C:\\Windows\\Fonts\\seguihis.ttf"      0 SegoeUIHistoric         Segoe UI Historic Regular   normal norm… FALSE  FALSE    
13 "C:\\Windows\\Fonts\\segoeuii.ttf"      0 SegoeUI-Italic          Segoe UI          Italic    normal norm… TRUE   FALSE    
14 "C:\\Windows\\Fonts\\segoeuil.ttf"      0 SegoeUI-Light           Segoe UI          Light     light  norm… FALSE  FALSE    
15 "C:\\Windows\\Fonts\\seguili.ttf"       0 SegoeUI-LightItalic     Segoe UI          Light It… light  norm… TRUE   FALSE    
16 "C:\\Windows\\Fonts\\seguisb.ttf"       0 SegoeUI-Semibold        Segoe UI          Semibold  semib… norm… FALSE  FALSE    
17 "C:\\Windows\\Fonts\\seguisbi.ttf"      0 SegoeUI-SemiboldItalic  Segoe UI          Semibold… semib… norm… TRUE   FALSE    
18 "C:\\Windows\\Fonts\\segoeuisl.ttf"     0 SegoeUI-Semilight       Segoe UI          Semilight NA     norm… FALSE  FALSE    
19 "C:\\Windows\\Fonts\\seguisli.ttf"      0 SegoeUI-SemilightItalic Segoe UI          Semiligh… NA     norm… TRUE   FALSE    
20 "C:\\Windows\\Fonts\\seguisym.ttf"      0 SegoeUISymbol           Segoe UI Symbol   Regular   normal norm… FALSE  FALSE 

如有任何建议,我们将不胜感激!

我已阅读此处的讨论,但它似乎不适用于我的情况。我尝试过注册字体,但 ggplot 和 ggiraph 读取姓氏的方式似乎有所不同。

r ggplot2 ggiraph
1个回答
0
投票

我的系统上没有安装

"Segoe UI"
(在 Mac 上运行),因此我使用
"Roboto Light"
作为代表。

让我们首先根据

ggiraph
中的默认示例重现您的问题。首先,正如您的情况一样,
"Roboto"
系列包含多种字体或样式。现在,在
"Roboto Light"
中使用
ggplot2
我得到了具有正确字体的绘图。但是,使用
girafe()
转换为交互式绘图时出现错误。

library(ggplot2)
library(ggiraph)

font_light <- "Roboto Light"

fonts <- systemfonts::system_fonts()

fonts[grepl("^Roboto", fonts$family), c("path", "index", "name", "family", "style")] |> 
  transform(path = basename(path))
#>                       path index                name family         style
#> 1  Roboto-MediumItalic.ttf     0 Roboto-MediumItalic Roboto Medium Italic
#> 2   Roboto-BlackItalic.ttf     0  Roboto-BlackItalic Roboto  Black Italic
#> 3         Roboto-Light.ttf     0        Roboto-Light Roboto         Light
#> 4          Roboto-Bold.ttf     0         Roboto-Bold Roboto          Bold
#> 5        Roboto-Italic.ttf     0       Roboto-Italic Roboto        Italic
#> 6   Roboto-LightItalic.ttf     0  Roboto-LightItalic Roboto  Light Italic
#> 7       Roboto-Regular.ttf     0      Roboto-Regular Roboto       Regular
#> 8    Roboto-BoldItalic.ttf     0   Roboto-BoldItalic Roboto   Bold Italic
#> 9        Roboto-Medium.ttf     0       Roboto-Medium Roboto        Medium
#> 10        Roboto-Black.ttf     0        Roboto-Black Roboto         Black
#> 11         Roboto-Thin.ttf     0         Roboto-Thin Roboto          Thin
#> 12   Roboto-ThinItalic.ttf     0   Roboto-ThinItalic Roboto   Thin Italic

theme_org <- function() {
  theme(
    plot.subtitle = element_text(
      family = font_light,
      size = 11,
      hjust = 0,
      margin = margin(b = 10)
    )
  )
}

data <- mtcars
data$carname <- row.names(data)

gg_point <- ggplot(data = data) +
  geom_point_interactive(aes(
    x = wt, y = qsec, color = disp,
    tooltip = carname, data_id = carname
  )) +
  labs(
    title = "Number of cars by cyl",
    subtitle = "Number of cars by cyl"
  ) +
  theme_org()

gg_point


girafe(ggobj = gg_point)
#> Error in `girafe()`:
#> ! Font family 'Roboto Light' has not been found on your system or is not
#>   registered.
#> ℹ You can use a google font with `gdtools::register_gfont()`.
#> ℹ You can use any font with `systemfonts::register_font()`.

但是可以通过在运行

systemfonts::register_font
之前使用
girafe()
注册字体来解决这个问题,其中我传递一个
list
,其中包含名称为
"Roboto-Light"
的所需字体的路径以及
plain=
参数的索引,如下所示:

注意:对于示例,不需要索引,即

plain = font_light_info$path
就足够了,但如果字体文件包含多种字体,则需要索引。

# Register font

font_light_info <- fonts[fonts$name == "Roboto-Light", ]
systemfonts::register_font(
  font_light, 
  plain = list(font_light_info$path, font_light_info$index)
)

girafe(ggobj = gg_point)

© www.soinside.com 2019 - 2024. All rights reserved.