How to draw a ggplot2 histogram and a normal density line in the same graph in the R programming language. More details: statisticsglob...
R code of this video:
set.seed(119864293) # Create example data
data <- data.frame(x = rnorm(300))
install.packages("ggplot2") # Install & load ggplot2
library("ggplot2")
ggplot(data, aes(x)) + # Draw regular histogram without density
geom_histogram()
ggplot(data, aes(x)) + # Draw histogram with probabilities
geom_histogram(aes(y = ..density..))
ggplot(data, aes(x)) + # Draw histogram with density
geom_histogram(aes(y = ..density..)) +
stat_function(fun = dnorm,
args = list(mean = mean(data$x),
sd = sd(data$x)),
col = "#1b98e0",
size = 5)
Follow me on Social Media:
Facebook: / statisticsglobecom
LinkedIn: / statisticsglobe
Twitter: / joachimschork
Негізгі бет Overlay Normal Density Curve on Top of ggplot2 Histogram (R Example) | geom_histogram, stat_function
Пікірлер: 22