Windows10下R项目默认字体设为Times New Roman的报错解决求助
Windows 10 下设置 Times New Roman 为 R 项目默认字体的解决方案
一、先处理报错问题
你碰到的Incompatible methods ("+.tmap", "+.gg")报错,是因为tmap和ggplot2包的+运算符冲突,和字体设置本身无关。解决方式:
- 不同时加载两个包,使用其中一个前,用
detach("package:ggplot2", unload=TRUE)卸载另一个; - 调用函数时明确指定包名,比如
tmap::tm_shape()、ggplot2::ggplot(),避免运算符混淆。
二、设置默认字体的正确方法
方法1:通过 .Rprofile 全局配置
在项目目录下创建或编辑 .Rprofile 文件,添加以下代码:
if (.Platform$OS.type == "windows") { windowsFonts( Times = windowsFont("Times New Roman") ) # 基础绘图默认字体 par(family = "Times") # ggplot2 默认字体 if (requireNamespace("ggplot2", quietly = TRUE)) { ggplot2::theme_set(ggplot2::theme_minimal(base_family = "Times")) } }
保存后重启R会话,基础绘图和ggplot2绘图都会默认使用Times New Roman。
方法2:项目内局部配置
如果不想修改全局,在项目主脚本开头添加:
# 注册字体 windowsFonts(Times = windowsFont("Times New Roman")) # 基础绘图默认字体 par(family = "Times") # ggplot2 默认字体 library(ggplot2) theme_set(theme_minimal(base_family = "Times"))
三、无法勾选RStudio选项的替代方案
- 临时禁用 .Rprofile:启动RStudio时按住
Shift键,直到会话加载完成,会跳过.Rprofile执行; - 手动修改RStudio配置文件:Windows下找到
%APPDATA%\RStudio\rstudio-prefs.json,添加或修改字段:{ "disable_rprofile": true, "quit_child_processes_on_exit": true }
修改后重启RStudio即可生效。
内容的提问来源于stack exchange,提问作者aleksandar_m
相关产品推荐
相关产品推荐

