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

如何查看MacOS系统中ggplot2的theme_classic()默认字体?

查看MacOS下ggplot2 theme_classic()的默认字体方法
  • 方法一:直接提取主题的字体配置
    ggplot2的主题元素字体可直接通过调用主题对象查看,运行以下代码即可:

    library(ggplot2)
    # 查看theme_classic全局文本的字体
    theme_classic()$text$family
    # 单独查看标题、坐标轴文本的字体设置
    theme_classic()$plot.title$family
    theme_classic()$axis.text$family
    

    如果返回NULL,说明用的是系统默认字体——MacOS下ggplot2默认调用系统无衬线字体,通常是San Francisco系列。

  • 方法二:通过绘图对象提取实际使用的字体
    先绘制一个示例图形,再从生成的绘图对象里扒字体信息:

    # 生成示例图
    p <- ggplot(mtcars, aes(mpg, wt)) + geom_point() + theme_classic()
    # 遍历主题元素,提取所有带字体设置的项
    sapply(p$theme, function(x) if(!is.null(x$family)) x$family)
    

    也可以用grid包查看渲染后的实际字体:

    library(grid)
    # 渲染图形
    print(p)
    # 筛选出文本类元素
    txt_elements <- grid.ls(print = FALSE)[grepl("text", grid.ls(print = FALSE)$name), ]
    # 查看第一个文本元素的字体
    grid.get(txt_elements$name[1])$gp$fontfamily
    
  • 方法三:用systemfonts包检测当前调用的字体
    这个包能精准检测系统中正在使用的字体,步骤如下:

    library(systemfonts)
    # 先渲染图形触发字体调用
    print(p)
    # 查看最近使用的字体列表
    recent_fonts()
    

    MacOS环境下,无指定字体时通常会显示SF Pro Display或San Francisco相关字体条目。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 16:53:10