Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pass an expression in geom_text using both a variable and greek letter

I am trying to add text to a plot which includes both a variable and a greek letter. I would like to display the values that the variables pt and P_T take on but I’d like the greek letter to be used for alpha. The code below gives the following error:

Error in f(): ! Aesthetics must be valid data columns. Problematic
aesthetic(s): label = expression(…). Did you mistype the name of a
data column or forget to add after_stat()?

Any help would be greatly appreciated!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

x <- rep(seq(1,5),3)
value <- runif(15)
alpha <- rep(c(0.5,0.6,0.7),each=5)
my_data <- data.frame(x=x,value=value,alpha=alpha)
pt=6
P_T=0.7


ggplot(data=my_data, aes(x=x, y=value,group=as.factor(alpha),color=as.factor(alpha)))+
  geom_line()+
  scale_x_continuous(name =paste("True Landmark PFS at", pt, "Months"), breaks=seq(1,5,1))+
  theme(plot.margin= unit(c(0.7, 0.7, 0.7,0.7), "cm"))+
  theme(plot.title = element_text(hjust = 0.5, size=12))+
  scale_y_continuous(name ="Probability of ESOE",breaks=seq(0,1,by=0.25),limits=c(0,1))+
  guides(color = guide_legend(title = expression(alpha)))
  geom_text(aes(x=3, label=expression(paste("Posterior prob (True RMST at",pt,"month>",alpha,"|data>",P_T)), y=0.75),
            angle=0, size=2.5, color="black")

>Solution :

If you just want to display a greek letter the one option would be to simply use the unicode, e.g. \u03B1 for alpha. Also, as you want to add just one annotation to your plot I would suggest to use annotate as with geom_text you add the annotation multiple times:

set.seed(123)

library(ggplot2)

ggplot(data = my_data, aes(x = x, y = value, group = as.factor(alpha), color = as.factor(alpha))) +
  geom_line() +
  scale_x_continuous(name = paste("True Landmark PFS at", pt, "Months"), breaks = seq(1, 5, 1)) +
  theme(plot.margin = unit(c(0.7, 0.7, 0.7, 0.7), "cm")) +
  theme(plot.title = element_text(hjust = 0.5, size = 12)) +
  scale_y_continuous(name = "Probability of ESOE", breaks = seq(0, 1, by = 0.25), limits = c(0, 1)) +
  guides(color = guide_legend(title = expression(alpha))) +
  annotate(geom = "text", x = 3, label = paste("Posterior prob (True RMST at", pt, "month > \u03B1 | data>", P_T), y = 0.75,
    angle = 0, size = 2.5, color = "black"
  )

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading