Home > Back-end >  Add standard deviation text overlay on ggplot geom_point()
Add standard deviation text overlay on ggplot geom_point()

Time:02-10

A plot:

library(tidyverse)
diamonds |> 
  filter(x > 0) |> 
  ggplot(aes(x = x, y = log(price)))  
  geom_point()

Looks like this: enter image description here

The standard deviation of log(diamonds$Price) (filtered where x > 0) is 1.014611:

diamonds |> 
  filter(x > 0) |> 
  pull(price) |> 
  log() |> 
  sd() # 1.014611

I wouldlike to overlay this on top of my plot somewhere and call it just 'sd'. In this case it would say 'sd = 1.014611'. Can I do this?

[EDIT]

Bonus if across facets. Tried:

annotations <- diamonds |> 
  filter(x > 0) |> 
  group_by(cut) |> 
  summarise(sd_log_price = paste0("sd = ", round(sd(log(price)), 2)))

diamonds |> 
  filter(x > 0) |> 
  ggplot(aes(x = x, y = log(price)))  
  geom_point()  
  geom_text(label = annotations$sd_log_price)   # Error: Aesthetics must be either length 1 or the same as the data (53932): label
  facet_wrap(. ~ cut)

But this throws the error in the comment.

CodePudding user response:

There are a number of nearly synonymous ways of getting text onto a plot (e.g. ggplot2::annotate(), ggplot2::geom_text()).

Note here that I used the {magrittr} pipe (%>%) instead of the new native pipe (|>) to make use of the . function of the former in the paste0() to create the annotation.

library(tidyverse)

annotation <- diamonds |> 
  filter(x > 0) |> 
  pull(price) |> 
  log() |> 
  sd() |>
  round(6) %>% 
  paste0("sd = ", .)

diamonds |> 
  filter(x > 0) |> 
  ggplot(aes(x = x, y = log(price)))  
  geom_point()  
  geom_text(aes(label = annotation, x = -Inf, y = Inf), hjust = -0.5, vjust = 2, size = 6)

Created on 2022-02-09 by the reprex package (v2.0.1)

  • Related