user156625
user156625

Reputation: 33

Inserting math symbols in legends of R plots

I am using survminer to produce a survival plot with 2 survival curves, one for patients who had received 4 Packed Red Blood Cells (PRBC) units during surgery and one for patients who received more than 4 PRBC units. Unfortunately I have not been able to insert the proper "<=" sign into the legend using "expression" or "bquote" functions.

I have read previous posts on this subject, but the guidance that I was able to find using expression() and bquote() do not seem to be working out properly.

library(survival)
library(coxed)
library(survminer)

simdata <- sim.survdata(N=300, T=100, num.data.frames=1, xvars = 1)

d <- simdata$data

d$Xfactor <- ifelse(d$X <= 0.3, 1, 0)


# the code chunks below do not seem to work because of the legends

ggsurvplot(
  fit = survfit(Surv(d$y, event = d$failed) ~ d$Xfactor, data = d), 
  xlab = "Years", 
  ylab = "Overall survival probability",
  main = "Overall survival",
  legend.labs = c(bquote(<= ~ "4 Units"),"> 4 Units"))

ggsurvplot(
  fit = survfit(Surv(d$y, event = d$failed) ~ d$Xfactor, data = d), 
  xlab = "Years", 
  ylab = "Overall survival probability",
  main = "Overall survival",
  legend.labs = c(bquote("" ~ <= ~ "4 Units"),"> 4 Units"))

ggsurvplot(
  fit = survfit(Surv(d$y, event = d$failed) ~ d$Xfactor, data = d), 
  xlab = "Years", 
  ylab = "Overall survival probability",
  main = "Overall survival",
  legend.labs = c(expression("",<=,"4 Units"),"> 4 Units"))

ggsurvplot(
  fit = survfit(Surv(d$y, event = d$failed) ~ d$Xfactor, data = d), 
  xlab = "Years", 
  ylab = "Overall survival probability",
  main = "Overall survival",
  legend.labs = c(expression(<=,"4 Units"),"> 4 Units"))

ggsurvplot(
  fit = survfit(Surv(d$y, event = d$failed) ~ d$Xfactor, data = d), 
  xlab = "Years", 
  ylab = "Overall survival probability",
  main = "Overall survival",
  legend.labs = c(expression(<= "4 Units"),"> 4 Units"))



Upvotes: 2

Views: 1002

Answers (1)

George Savva
George Savva

Reputation: 5336

To place a unicode character in a string you can use the unicode number for the symbol you want escaped with \u. That is using \u2265 for ≥ and \u2264 for ≤.

ggsurvplot(
  fit = survfit(Surv(d$y, event = d$failed) ~ d$Xfactor, data = d), 
  xlab = "Years", 
  ylab = "Overall survival probability",
  main = "Overall survival",
  legend.labs = c("\u2264 4 Units","> 4 Units"))

enter image description here

Upvotes: 1

Related Questions