如何设置ggsurvplot中字体为Times New Roman?

文章介绍了如何在R编程中利用`windowsFonts()`查看Windows系统可用字体,然后通过`extrafont`包导入额外字体,以改变ggplot和ggsurvplot中的文本样式,如`ComicSansMS`和`TimesNewRoman`等。
摘要由CSDN通过智能技术生成

您可以使用 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")
)
  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值