Basic Histogram - why-you-need-to-practice-it

https://www.r-bloggers.com/a-simple-histogram-and-why-you-need-to-practice-it/

LOAD GGPLOT2

library(ggplot2)

CREATE VARIABLE, NORMALLY DISTRIBUTED

# 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)

PLOT

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`.