How to Overlay Multiple Densities in Same Graphic in Base R (Example Code)

In this R article you’ll learn how to overlay multiple density plots.

Constructing Example Data

set.seed(345243)           # Set seed
x <- rnorm(1000, 1)        # Create numeric vectors for densities
y <- rnorm(1000, 0, 2)

Example: Overlay Multiple Densities in R

plot(density(x),           # Plot density of x # Plot first density
     xlim = c(- 7, 7),
     ylim = c(0, 0.5))
lines(density(y),          # Overlay second density
      col = 2)

r graph figure 1 how overlay multiple densities same graphic base r

Leave a Reply

Your email address will not be published.

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