Mathematical/Superscript Annotation in ggplo2 with draw_label

Viewed 63

I would like to do a "simple" thing. I would like to annotate a plot with some statistical information.

  cor<-cor.test(PB.reg@data$x,PB.reg@data$y, method = "pearson")
  label<-(paste("Passing Bablok Regression (n =", length(PB.reg@data$x),")",sep=""))
  label2<-(paste("y = ", PB.reg@para[1,1], " + ",PB.reg@para[2,1], " * Method 1",  sep=""))
  label3<-(paste("R^2 = ",round(cor$estimate^2,3),sep="" ))

  p<-ggplot(subset) +
    aes(x = Rater_Mean, y = Smartphone_Mean, colour = Participant, label=Participant) +
    geom_point(size = 2) +
    scale_color_viridis_d(option = "inferno", alpha=0.8, direction=-1) +
    theme_classic()+
    geom_abline(intercept = PB.reg@para[1,1], slope = PB.reg@para[2,1],colour ="black", size=1, alpha=0.4)+
    geom_abline(intercept = PB.reg@para[1,3], slope = PB.reg@para[2,3],colour ="#2c718e",linetype="dotted")+
    geom_abline(intercept = PB.reg@para[1,4], slope = PB.reg@para[2,4],colour ="#2c718e",linetype="dotted")+
    geom_abline(intercept = 0, slope = 1, linetype="dotted", color="red")
    #geom_abline(intercept=seq(PB.reg@para[1,3],PB.reg@para[1,1],0.01), slope=PB.reg@para[2,3], alpha=0.2)+ #abline für LCI
    #geom_abline(intercept=seq(PB.reg@para[1,4],PB.reg@para[1,1],0.01), slope=PB.reg@para[2,4], alpha=0.2) #abline für UCI

  p<-p+
    ggtitle("Method 1 vs Method 2")+
    labs(x = "Method 1 [ms]", y = "Method 2 [ms]", title = "Method 1 vs Method 2")+
    theme(text = element_text(size = 16),axis.text = element_text(size = 12),plot.title = element_text(hjust = 0.5))



  ggdraw(p) + draw_label(label, .75, .2, size=12)+draw_label(label2, .75, .18,size=12)+draw_label(parse(text=lb1,keep.source = getOption("keep.source")), .75, .16,size=12)

enter image description here

As you can tell.... I got so far that I can annotate in the right bottom corner. Now I am struggeling with R^2. 1) How can I get the mathematical correct superscript? 2) Would it be possible to align all the draw_labels either left or right?

1 Answers
bquote(R^2 == .(round(cor$estimate^2,3)))
Related