Search code examples
rfonts

Custom Font with ggsurvplot


I'd like to change the font of all of the text in my ggsurvplot() plot to Proxima Nova, which is saved on my local computer. I've imported the font into R using the showtext package, but I'm not sure how to adjust the ggsurvplot() code to accept a font outside of the ggtheme

library(showtext)

font_add("proxima", "~/Library/Fonts/Proxima\ Nova\ Font.otf")

ggsurvplot(
  surv_fit,
  data = df,
  title = "Survival by Neighborhood SES",
  xlab = "Time since hospital discharge (years)",
  ylab = "Survival Probability",
  legend.title = "Neighborhood",
  legend.labs = c("Prosperous", "Comfortable", "Mid-Tier", "At-Risk", "Distressed"),
  font.legend = 9,
  conf.int = FALSE,
  censor = FALSE,
  surv.scale = "percent",
  break.time.by = 2,
  pval = T,
  palette = c("#FFE082", "#FFB300", "#FF8F00", "#EF5350", "#B71C1C"),
  ggtheme = theme_classic()
) +
    theme(
      text = element_text(family = "proxima")
    )

When I add the theme lines, I get the error Error in ggsurvplot(surv_fit, data = df, title = "Survival by Neighborhood SES", : non-numeric argument to binary operator In addition: Warning message: Incompatible methods ("+.ggsurv", "+.gg") for "+".

I've tried adjusting font.family but it doesn't seem to recognize my preferred font. Is there a different package I should be using for custom fonts? How do I adjust my code to use the font I've loaded with showtext?

ggsurvplot


Solution

  • Got it! It looks like if you adjust the ggtheme line, you can add custom fonts:

    ggtheme = theme_classic(base_family = "proxima"),
       font.family = "proxima"