https://www.r-bloggers.com/a-simple-histogram-and-why-you-need-to-practice-it/
library(ggplot2)
# set "seed" for random numbers
set.seed(42)
# create variable
xvar_rand_norm <- rnorm(1000, mean = 1)
# CREATE DATA FRAME FROM VARIABLE
df.xvar <- data.frame(xvar_rand_norm)
# CALCULATE MEAN
# we'll use this in an annotation
xvar_mean <- mean(xvar_rand_norm)
Here, we’re going to plot the histogram We’ll also add a line at the calculated mean and also add an annotation to specify the value of the calculated mean
ggplot(data = df.xvar, aes(x = xvar_rand_norm)) +
geom_histogram() +
geom_vline(xintercept = xvar_mean, color = "dark red") +
annotate("text", label = paste("Mean: ", round(xvar_mean,digits = 2)),
x = xvar_mean, y = 30, color = "white", size = 5)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.