UseR10085
UseR10085

Reputation: 8200

How to use superscript in geom_text?

I am trying to use superscript in geom_text. But I am unable to do that. I am using the following code

library(caret)
library(tidyverse)

summ <- iris %>% 
  group_by(Species) %>% 
  summarise(R = cor(Sepal.Length, Petal.Length, use="pairwise.complete.obs"),
            RMSE = RMSE(Sepal.Length, Petal.Length)) %>% 
  mutate_if(is.numeric, round, digits=2)

p <- ggplot(data=iris, aes(x = Sepal.Length, y = Petal.Length)) +
  geom_point(color="blue",alpha = 1/3) + 
  facet_wrap(Species ~ ., scales="free") +
  geom_smooth(method=lm, fill="black", formula = y ~ x) +
  xlab("Sepal Length") +
  ylab("Petal Length") + theme_bw() +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) 

# Here we create our annotations data frame.
df.annotations <- data.frame()

# R
df.annotations <- rbind(df.annotations,
                        cbind(as.character(summ$Species),
                              paste("R", summ$R,
                                    sep = " = ")))

# RMSE
df.annotations <- rbind(df.annotations,
                        cbind(as.character(summ$Species),
                              paste("RMSE", summ$RMSE,
                                    sep = " = ")))

# This here is important, especially naming the first column
# Species
colnames(df.annotations) <- c("Species", "label")


vertical_adjustment = ifelse(grepl("\\bR\\b",df.annotations$label), 1.5, 3)

p + geom_text(data = df.annotations, aes(x=-Inf, y=+Inf, label=label),
              hjust = -0.1, vjust = vertical_adjustment, size=3.5)

enter image description here

My expected output is

enter image description here

How can I add the unit for RMSE after the value and superscript 2 after R i.e. R^2? You can also see that there is a slight shift in the alignment of RMSE with respect to R. How can I make them into the same line?

Upvotes: 4

Views: 2673

Answers (2)

Brenton M. Wiernik
Brenton M. Wiernik

Reputation: 1316

Another approach is to use the ggtext package, which enables markdown formatting in ggplot text objects. I find this syntax much easier to use than R's plotmath, especially for things like italics and color:

df.annotations <- data.frame(
  Species = rep(summ$Species, 2),
  label = c(
    paste0("R<sup>2</sup> = ", summ$R),
    paste0("RMSE = ", summ$RMSE, "m<sup>3</sup> m<sup>-3</sup>")
  )
)

vertical_adjustment = ifelse(grepl("\\bR\\b", df.annotations$label), 1.5, 2.5)

p + geom_richtext(data = df.annotations, aes(x=-Inf, y=+Inf, label=label),
             hjust = 0, vjust = vertical_adjustment, size=3.5, label.size = 0)

enter image description here

Upvotes: 1

stefan
stefan

Reputation: 125697

This could be achieved like so:

  1. Set parse=TRUE in geom_text
  2. Make use of ?plotmath to add superscripts to your labels
# Here we create our annotations data frame.
df.annotations <- data.frame(
  Species = rep(summ$Species, 2),
  label = c(
    paste0("~R^{2} == ", summ$R),
    paste0("~RMSE == ", summ$RMSE, "~m^{3} ~m^{-3}")
  )
)

vertical_adjustment = ifelse(grepl("\\bR\\b", df.annotations$label), 1.5, 3)

p + geom_text(data = df.annotations, aes(x=-Inf, y=+Inf, label=label),
              hjust = 0, vjust = vertical_adjustment, size=3.5, parse = TRUE)

Upvotes: 4

Related Questions