Plot Normal, Left & Right Skewed Densities in R (Example Code)

This article explains how to create a plot of normal, left, and right skewed densities in the R programming language.

Creating Example Data

set.seed(5698320)                     # Create example data frame
my_df <- data.frame(values = c(rbeta(500, 5, 2),
                               rbeta(500, 5, 5),
                               rbeta(500, 2, 5)),
                    skewness = rep(c("right", "normal", "left"), each = 500))
head(my_df)                           # Display head of example data
#      values skewness
# 1 0.7893105    right
# 2 0.8030419    right
# 3 0.9785181    right
# 4 0.7854078    right
# 5 0.7410255    right
# 6 0.2533721    right

Example: Plot Normal, Left & Right Skewed Distributions Using ggplot2 Package

install.packages("ggplot2")           # Install & load ggplot2 package
library("ggplot2")
ggplot(my_df,                         # Normal, left & right skewed densities
       aes(x = values,
           color = skewness)) +
  geom_density()

r graph figure 1 plot normal left right skewed densities r

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top