Draw Normal, Left & Right Skewed Distributions (2 Examples) | ggplot2 Density Plot | tidyr Package

preview_player
Показать описание
R code of this video:

x2 = rbeta(1000, 5, 2),
x3 = rnorm(1000),
x4 = rbeta(1000, 2, 5),
x5 = rbeta(1000, 2, 10))

plot(density(data$x1), col = 2, # Overlay all columns as densities
xlim = c(- 3, 3),
ylim = c(0, 5))
lines(density(data$x2), col = 3)
lines(density(data$x3), col = 4)
lines(density(data$x4), col = 5)
lines(density(data$x5), col = 6)
legend("topleft", # Add legend to plot
legend = c("x1 = rbeta(1000, 10, 2)",
"x2 = rbeta(1000, 5, 2)",
"x3 = rnorm(1000)",
"x4 = rbeta(1000, 2, 5)",
"x5 = rbeta(1000, 2, 10)"),
col = 2:6,
lty = 1,
cex = 0.8)

library("tidyr")

data_long <- data %>% # Convert wide to long data
pivot_longer(colnames(data)) %>%

library("ggplot2") # Load ggplot2 package

ggplot(data_long, # Draw all densities in ggplot2 plot
aes(value,
fill = name)) +
geom_density(alpha = 0.25)

Follow me on Social Media:

Рекомендации по теме
Комментарии
Автор

Beta distributions are used in Bayes work and it would have been nice to get more detail on those and how they work. Nice pivoting to ggplot2 (no pun intended).

haraldurkarlsson