1

I'd like to include a text label that indicates the correlation coefficient on a series of facet_wrapped scatterplots (eg: "r = -0.52").

So my scatterplots look something like this:

library(tidyverse)
df <- mtcars
df %>% 
  ggplot(aes(y = mpg, x = hp)) +
  geom_point(colour = "blue") + 
  facet_grid(~ factor(cyl))

Created on 2019-06-26 by the reprex package (v0.2.1)

I've then tried to create three separate correlation coefficients, after grouping, and I'd like the label to show up on the diagram, but it's not quite right. I'd like it to say something like: "r = -0.52"

library(tidyverse)
df <- mtcars
df2 <- df %>% 
  group_by(cyl) %>% 
  summarise(correlation = cor(y = mpg, x = hp, use = "pairwise.complete.obs"))
df2
#> # A tibble: 3 x 2
#>     cyl correlation
#>   <dbl>       <dbl>
#> 1     4      -0.524
#> 2     6      -0.127
#> 3     8      -0.284
df <- left_join(df, df2, by = "cyl")

df %>% 
  ggplot(aes(y = mpg, x = hp)) +
  geom_point(colour = "blue") +
  geom_text(x = 200, y = 30, label = expression(paste(italic(r), " = ", df$correlation))) +
  facet_grid(~ factor(cyl))
#> Warning in is.na(x): is.na() applied to non-(list or vector) of type
#> 'expression'

Created on 2019-06-26 by the reprex package (v0.2.1)

Jeremy K.
  • 1,710
  • 14
  • 35

2 Answers2

4

You can do

  geom_text(x = 200, y = 30, 
            label = paste0("italic(r) == ", df$correlation), parse = TRUE) 

To display a rounded correlation:

  geom_text(x = 200, y = 30, 
            label = paste0("italic(r) == ", round(df$correlation,2)), parse = TRUE) 
Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225
  • 1
    That's it, nice. I think a clearer title for this question would be "How to combine italic and non-italic components of a `geom_text` call". – heds1 Jun 26 '19 at 08:03
  • 1
    great maybe just add the rounding as edit for completeness: ` geom_text(x = 200, y = 30, label = paste0("italic(r) == ", round(df$correlation,2)), parse = TRUE)` – Stephen Henderson Jun 26 '19 at 08:14
  • 1
    Good call, @heds1. I didn't realise. I'll try to edit the title now. – Jeremy K. Jun 26 '19 at 08:21
  • one other question: when I'm calling `round(df$correlation, 2)`, that's actually a whole column of the same correlation coefficients repeated. Is there a way where I could take the average of the column? Obviously, it has the same answer in this case, but just for my future knowledge. – Jeremy K. Jun 26 '19 at 09:43
2

Try putting the geom_text after the facet_grid for R to know where to put it:

df %>% 
  ggplot(aes(y = mpg, x = hp)) +
  geom_point(colour = "blue") +
  facet_grid(~ factor(cyl)) +
geom_text(x = 200, y = 30, 
          label = paste0("italic(r) == ", round(df$correlation,2)), parse = TRUE)

enter image description here

Carles
  • 2,731
  • 14
  • 25