You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在ggplot主题中指定Segoe UI等字体的特定样式?

在ggplot中使用Segoe UI的细分字体样式问题

我们可以通过systemfonts::system_fonts()查看系统中Segoe UI的所有字体变体:

systemfonts::system_fonts() %>% filter(family == "Segoe UI")

输出结果:

# A tibble: 12 × 9
   path                                index name                    family   style            weight   width  italic monospace
   <chr>                               <int> <chr>                   <chr>    <chr>            <ord>    <ord>  <lgl>  <lgl>    
 1 "C:\\WINDOWS\\Fonts\\segoeui.ttf"       0 SegoeUI                 Segoe UI Regular          normal   normal FALSE  FALSE    
 2 "C:\\WINDOWS\\Fonts\\seguibl.ttf"       0 SegoeUIBlack            Segoe UI Black            heavy    normal FALSE  FALSE    
 3 "C:\\WINDOWS\\Fonts\\seguibli.ttf"      0 SegoeUIBlack-Italic     Segoe UI Black Italic     heavy    normal TRUE   FALSE    
 4 "C:\\WINDOWS\\Fonts\\segoeuib.ttf"      0 SegoeUI-Bold            Segoe UI Bold             bold     normal FALSE  FALSE    
 5 "C:\\WINDOWS\\Fonts\\segoeuiz.ttf"      0 SegoeUI-BoldItalic      Segoe UI Bold Italic      bold     normal TRUE   FALSE    
 6 "C:\\WINDOWS\\Fonts\\segoeuii.ttf"      0 SegoeUI-Italic          Segoe UI Italic           normal   normal TRUE   FALSE    
 7 "C:\\WINDOWS\\Fonts\\segoeuil.ttf"      0 SegoeUI-Light           Segoe UI Light            light    normal FALSE  FALSE    
 8 "C:\\WINDOWS\\Fonts\\seguili.ttf"       0 SegoeUI-LightItalic     Segoe UI Light Italic     light    normal TRUE   FALSE    
 9 "C:\\WINDOWS\\Fonts\\seguisb.ttf"       0 SegoeUI-Semibold        Segoe UI Semibold         semibold normal FALSE  FALSE    
10 "C:\\WINDOWS\\Fonts\\seguisbi.ttf"      0 SegoeUI-SemiboldItalic  Segoe UI Semibold Italic  semibold normal TRUE   FALSE    
11 "C:\\WINDOWS\\Fonts\\segoeuisl.ttf"     0 SegoeUI-Semilight       Segoe UI Semilight        NA       normal FALSE  FALSE    
12 "C:\\WINDOWS\\Fonts\\seguisli.ttf"      0 SegoeUI-SemilightItalic Segoe UI Semilight Italic NA       normal TRUE   FALSE    

但尝试在ggplot主题中调用这些细分样式时,face参数无法识别Semibold这类自定义字重:

ggplot(mtcars) +
  aes(x=mpg, y=hp) +
  geom_point() +
  theme(text = element_text(family="Segoe UI", face="Semibold", size=20))

报错信息:

Error in FUN(X[[i]], ...) : invalid fontface Semibold

直接使用name列中的值作为family参数同样无法生效。


解决方法

方法1:注册细分字体为独立字体系列

ggplot的face参数仅支持有限的内置值(plain/bold/italic/bold.italic),无法直接识别Semibold这类细分字重。可以用systemfonts::register_font()将目标字体变体注册为独立的字体系列,之后就能直接调用:

# 注册Segoe UI Semibold
systemfonts::register_font(
  name = "Segoe UI Semibold",
  plain = "C:\\WINDOWS\\Fonts\\seguisb.ttf"
)

# 注册Segoe UI Light(可选)
systemfonts::register_font(
  name = "Segoe UI Light",
  plain = "C:\\WINDOWS\\Fonts\\segoeuil.ttf"
)

# 在ggplot中使用
ggplot(mtcars) +
  aes(x=mpg, y=hp) +
  geom_point() +
  theme(text = element_text(family="Segoe UI Semibold", size=20))

方法2:直接指定字体文件路径

如果不想注册字体,也可以在element_text中通过family参数直接指定字体文件的完整路径:

ggplot(mtcars) +
  aes(x=mpg, y=hp) +
  geom_point() +
  theme(text = element_text(family="C:\\WINDOWS\\Fonts\\seguisb.ttf", size=20))

方法3:使用showtext包实现跨平台兼容

如果需要跨平台使用目标字体,showtext包能更便捷地加载系统字体:

library(showtext)
# 加载Segoe UI Semibold(Windows下可直接用字体文件名)
font_add("Segoe UI Semibold", regular = "seguisb.ttf")
# 开启showtext自动渲染
showtext_auto()

# 绘图
ggplot(mtcars) +
  aes(x=mpg, y=hp) +
  geom_point() +
  theme(text = element_text(family="Segoe UI Semibold", size=20))

内容的提问来源于stack exchange,提问作者sharoz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 21:32:48