0

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.

Doug Fir
  • 19,971
  • 47
  • 169
  • 299

1 Answers1

1

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)

Dan Adams
  • 4,971
  • 9
  • 28
  • Thanks this did work. Any idea how to do this across facets? I made an edit – Doug Fir Feb 09 '22 at 21:06
  • 1
    You feed in a dataframe with the faceting variable and the label for each facet. See [here](https://stackoverflow.com/questions/11889625/annotating-text-on-individual-facet-in-ggplot2) – Dan Adams Feb 09 '22 at 21:14
  • Yep, that link got me what I needed, cheers – Doug Fir Feb 09 '22 at 21:26