I'd like to add an "id" annotation to certain observations in a histogram.
So far, I'm able to add the annotation with no problem, but I'd like the 'y' position of my annotations to be the count of the bin + 1 (for aesthetic reasons).
This is what I have so far:
library(tidyverse)
library(ggrepel)
selected_obs <- c("S10", "S100", "S245", "S900")
set.seed(0)
values <- rnorm(1000)
plot_df <- tibble(id = paste0("S", 1:1000),
values = values) %>%
mutate(obs_labels = ifelse(id %in% selected_obs, id, NA))
ggplot(plot_df, aes(values)) +
geom_histogram(binwidth = 0.3, color = "white") +
geom_label_repel(aes(label = obs_labels, y = 100))
I've seen multiple answers dealing with annotating the count for each bin using geom_text(stat = count", aes(y=..count.., label=..count..)
.
Based on that, I've tried these two work-arounds, but no success:
geom_label_repel(stat = "count", aes(label = obs_labels, y = ..count..))
yields: "Error: geom_label_repel requires the following missing aesthetics: label"geom_label_repel(aes(label = obs_labels, y = ..count..))
yields "Error: Aesthetics must be valid computed stats. Problematic aesthetic(s): y = ..count... Did you map your stat in the wrong layer?".
Anybody that can shed some light here?