I'm making a several histograms using ggplot2
and facet_wrap
and would like to plot the mean value on each panel. Below, I create a dummy data frame, find the mean of each facet, and then create the plots adding the mean using geom_point
.
# Load libraries
library(tidyverse)
# Toy data frame
df <- data.frame(ID = sample(letters[1:3], 100, replace = TRUE), n = runif(100))
# Mean value of each group
df_mean <- df %>% group_by(ID) %>% summarise(mean = mean(n))
# Plot histograms
ggplot(df) +
geom_histogram(aes(n)) +
facet_wrap(~ID) +
geom_point(data = df_mean, aes(x = mean, y = Inf))
I used y = Inf
to place the point at the top of each facet, but – as you can see – it is cropped somewhat. I'd like to nudge it downwards so that it is completely visible. To my knowledge, geom_point
doesn't have a nudge_y
or vadj
argument and 0.7 * Inf
is obviously nonsensical. I also tried adding position = position_nudge(y = -5)
as an argument to geom_point
, but this doesn't appear to have any effect. As a workaround, I even tried using geom_text
and specifying nudge_y
, but – like the position_nudge
solution – it did not have any noticeable effect. Is there an easy way of doing this whilst plotting or do I simply need to calculate the y
value prior to plotting?
...+geom_point(data = df_mean, aes(x = mean, y = 0), col="red")
to place in on the x axis using a different colour. – Lansing