0

I've used the following code to plot Kaplan-Meier survival curves and the corresponding risk table. I'd like all elements of the plot and table to have font Arial size 12.

library(survival)
library(survminer)
data(lung)

fit <- survfit(Surv(time, status) ~ sex, data = lung)

png(filename="./plots/lung.sohelp.font.png", width = 6.5, height = 6.5, units = 'in', res=150)     

ggsurvplot(fit, title="Survival by Sex in 'lung' Data", xlab = "Follow Up Time (Days)",
conf.int = FALSE, 
pval=TRUE, 
pval.method=TRUE, 
risk.table=TRUE,
risk.table.pos="out",
risk.table.col="black",
risk.table.y.text.col=FALSE,
tables.theme = theme_cleantable(),
legend.labs=c("Male","Female"),
font.tickslab = c(12),
legend.title="Sex",
ggtheme = theme_classic2(base_size=12, base_family = "Arial"),
           font.family = "Arial"
   )

dev.off()

Plot: https://i.stack.imgur.com/5ZG30.png

I notice a few things about this plot:
1. The Male/Female labels in the risk table are smaller than the font 'Number at risk'.
2. The legend and axis fonts looks smaller than the main title and p-value fonts.
2. The numbers in the plot and risk table do not look like Arial font.
3. Without the option font.tickslab = c(12) the tick marks are too small. This seems redundant to the ggtheme option.

How can I apply the same font (Arial, 12) to all elements of the plot and risk table? Thanks.

r2evans
  • 141,215
  • 6
  • 77
  • 149
TJ87
  • 404
  • 1
  • 3
  • 13

1 Answers1

2

Edit

If you are using Windows, you may need to install fonts (Changing fonts in ggplot2) before running your code:

library(extrafont)
font_import()
loadfonts(device = "win")      
windowsFonts()            

Now,

$Arial
[1] "Arial"

should be on the list.

library(survival)
library(survminer)
data(lung)

fit <- survfit(Surv(time, status) ~ sex, data = lung)

p <- ggsurvplot(fit, title="Survival by Sex in 'lung' Data", xlab = "Follow Up Time (Days)",
  conf.int = FALSE, 
  pval=TRUE, 
  pval.method=TRUE, 
  risk.table=TRUE,
  risk.table.pos="out",
  risk.table.col="black",
  risk.table.y.text.col=FALSE,
  tables.theme = theme_cleantable(),
  legend.labs=c("Male","Female"),
  font.tickslab = c(12),
  legend.title="Sex",
  ggtheme = theme_classic2(base_size=12, base_family = "Arial"),
           font.family = "Arial"
)
ggpar(p, 
      font.main = c(12, "bold"),
      font.x = c(12, "bold"),
      font.y = c(12, "bold"),
      font.caption = c(12, "bold"), 
      font.legend = c(12, "bold"), 
      font.tickslab = c(12, "bold"))

enter image description here

Community
  • 1
  • 1
Zhiqiang Wang
  • 6,206
  • 2
  • 13
  • 27
  • Thank you for the suggestion. However, my goal is to make the text uniformly Arial, 12 throughout the plot and risk table. While your options in ggpar helped make the font sizes more uniform, the fonts do not look like Arial. I've prepared a Word doc to compare the plot elements from 11/26 to Arial, 12 but I'm not sure how to share it in this comment section. – TJ87 Dec 07 '19 at 00:18
  • If you get error message: `font family not found in Windows font database`, you need to use package `extraFont` and run `font_import`, first. Check: https://stackoverflow.com/questions/34522732/changing-fonts-in-ggplot2 for details. – Zhiqiang Wang Dec 07 '19 at 01:00