2

I am having trouble adding R2 annotations to a faceted plot, where my R2 values are sometimes <0.01 (yes, it's not a good regression). I would like the 2 of R2 to be superscript. I have tried several options but seem to be stymied by the < symbol in my values

eg, using the iris data set, I first set up a new data frame with my R2 values previously calculated. The x & y positions are also set up as these are different for every facet (not essential for the iris dataset, but it is for mine)

SEr2s <- data.frame(Species = c("virginica",  "setosa",  "versicolor" ), 
                  xpos = c(5,7,7), ypos = c(4.2, 2, 4.2), 
                  lab = c("<0.01","0.08", "0.05"))

Then I run my plot:

XYPlot<-ggplot(data = iris, aes(x=Sepal.Length)) + 
  geom_point(aes(y = Sepal.Width), colour="grey40")+
  geom_smooth(aes(y = Sepal.Width), col="grey40", lwd=0.5, method="lm", se=FALSE) +
  geom_text(data = SEr2s, size=3, vjust=0, hjust=0,
            aes(x = xpos,  y = ypos, 
             label = paste("r^2==",lab)), parse = TRUE)+
  theme_bw() +
  theme(strip.background = element_blank(), strip.placement = "outside",
        panel.grid.minor = element_blank(), legend.position = "right") +
  facet_wrap(~Species)

I get this error:

Error in parse(text = text[[i]]) : :1:7: unexpected '<' 1: r^2== < ^

Is there a way to change my code or my labelling dataframe so that it doesn't try to evaluate these symbols?

Claus Wilke
  • 16,992
  • 7
  • 53
  • 104
Jenni G
  • 35
  • 1
  • 4
  • As mentioned [here](https://stackoverflow.com/questions/17639325/unexpected-symbol-error-in-parsetext-str-with-hyphen-after-a-digit), this will work `lab = c("\`<0.01\`","0.08", "0.05")` – A. Suliman Dec 01 '19 at 21:47

2 Answers2

8

You can avoid plotmath if you're using the ggtext package instead, which can handle basic HTML/markdown as input.

library(ggplot2)
library(ggtext)

SEr2s <- data.frame(Species = c("virginica",  "setosa",  "versicolor" ), 
                    xpos = c(5,7,7), ypos = c(4.2, 2, 4.2), 
                    lab = c("<0.01","0.08", "0.05"))

ggplot(data = iris, aes(x=Sepal.Length)) + 
  geom_point(aes(y = Sepal.Width), colour="grey40")+
  geom_smooth(aes(y = Sepal.Width), col="grey40", lwd=0.5, method="lm", se=FALSE) +
  geom_richtext(
    data = SEr2s, size=3, vjust=0, hjust=0,
    aes(x = xpos,  y = ypos, label = paste0("r<sup>2</sup> = ", lab))
  ) +
  theme_bw() +
  theme(
    strip.background = element_blank(), strip.placement = "outside",
    panel.grid.minor = element_blank(), legend.position = "right") +
  facet_wrap(~Species)
#> `geom_smooth()` using formula 'y ~ x'

Created on 2019-12-02 by the reprex package (v0.3.0)

Claus Wilke
  • 16,992
  • 7
  • 53
  • 104
3

I think that the easier solution is to define the subscript in your data.frame (SEr2s):

SEr2s <- data.frame(Species = c("virginica",  "setosa",  "versicolor" ), 
                    xpos = c(5,7,7), ypos = c(4.2, 2, 4.2), 
                    lab = c("atop(R^2<0.01)","atop(R^2==0.08)", "atop(R^2==0.05)"))

Then, you can call ggplot without just with label=lab:


ggplot(data = iris, aes(x=Sepal.Length)) + 
    geom_point(aes(y = Sepal.Width), colour="grey40")+
    geom_smooth(aes(y = Sepal.Width), col="grey40", lwd=0.5, method="lm", se=FALSE) +
    geom_text(data = SEr2s, size=3, vjust=0, hjust=0,
              aes(x = xpos,  y = ypos, 
                  label = lab), parse = TRUE)+
    theme_bw() +
    theme(strip.background = element_blank(), strip.placement = "outside",
          panel.grid.minor = element_blank(), legend.position = "right") +
    facet_wrap(~Species)

I think this gives you the plot you want: plot_R2 https://ibb.co/vwbvqp2

albgarre
  • 119
  • 3
  • Could you please explain what atop means in this? i'm not familiar with that – Jenni G Dec 03 '19 at 00:06
  • 1
    Actually nothing :D I copy-pasted it from an old piece of code and didn't realize it was there. If you remove the call to atop on the definition of the data frame, it still works. The reason why your code did not work is that you are pasting "r^2==" to "<0.01". The result is "r^2==<0.01", something R does not know how to parse. – albgarre Dec 03 '19 at 10:17
  • Great! Thanks for the explanation. Now it makes sense. – Jenni G Dec 04 '19 at 07:29