您可以使用 windowsFonts() 命令查看可用的字体。例如,当我开始查看时,我的字体是这样的:
> windowsFonts() $serif [1] "TT Times New Roman" $sans [1] "TT Arial" $mono [1] "TT Courier New"
安装extraFont 软件包,然后像这样运行 font_import(大约花了 5 分钟):
library(extrafont) font_import() loadfonts(device = "win")
最后尝试以下代码 ggplot
library(ggplot2) library(extrafont) loadfonts(device = "win") a <- ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point() + ggtitle("Fuel Efficiency of 32 Cars") + xlab("Weight (x1000 lb)") + ylab("Miles per Gallon") + theme(text=element_text(size=16, family="Comic Sans MS")) print(a)
如果是ggsurvplot
library(survival) library(survminer) loadfonts(device = "win") surPlot <- ggsurvplot( fit, data = rt, conf.int = F, # 不显示置信区间 pval = pValue, # 显示p值 pval.size = 10, # 设置p值的字体大小 legend.labs = c("High", "Low"), # 设置图例标签 legend.title = current_column_name, # 设置图例标题 xlab = "Time (month)", # 设置x轴标签 break.time.by = 1, # 设置x轴的刻度间隔 risk.table.title = "", # 风险表标题 risk.table = F, # 不显示风险表 risk.table.height = 0.25, ggtheme = theme_classic2(base_family = "Times New Roman"), font.main = c(16, "bold"), font.title= c(16, "bold"), font.subtitle= c(16, "bold"), font.caption= c(16, "bold"), font.x = c(16, "bold"), font.y = c(16, "bold"), font.tickslab = c(14, "bold"), font.legend = c(16, "bold") )