Avdyl Bytyqi
Avdyl Bytyqi

Reputation: 1

How to plot a density function of two variables?

I have two variables with the same length, v1 = actual alpha and v2 = stimulated alpha.

v1= (0.1, 0.6, 0.8, 0.11) v2= (0.3, 0.1, 0.5, 0.7)

I want to show a density function where these two are compared, kind replicating this picture:

enter image description here

Upvotes: 0

Views: 747

Answers (2)

Chris Ruehlemann
Chris Ruehlemann

Reputation: 21440

Here's a base R solution (based on @Allan's second dataframe):

hist(df$x[df$group=="Simulated"], 
     freq = F,
     xlab = "Alpha in %",
     border = "white", 
     main = "Density function for Actual and Simulated data", cex.main = 0.9, 
     xlim = range(df$x[df$group=="Actual"]))
lines(density(df$x[df$group=="Simulated"]), lty = 2)
lines(density(df$x[df$group=="Actual"]), lty = 1)
legend("topleft", legend = c("Actual", "Simulated"), bty = "n", lty = c(1,2))
grid()

enter image description here

Alternatively, with a bit more color:

hist(df$x[df$group=="Simulated"], 
     freq = F,
     xlab = "Alpha in %",
     border = "white", 
     main = "Density function for Actual and Simulated Alpha", cex.main = 0.9, 
     xlim = range(df$x[df$group=="Actual"]))
bg <- par("usr")
rect(bg[1], bg[3], bg[2], bg[4], col="grey50", border = NA, density = 70)
grid()
lines(density(df$x[df$group=="Simulated"]), lty = 2, col = "blue")
lines(density(df$x[df$group=="Actual"]), lty = 1, col = "blue")
legend("topleft", legend = c("Actual", "Simulated"), bty = "n", lty = c(1,2), col = "blue")

enter image description here

Upvotes: 1

Allan Cameron
Allan Cameron

Reputation: 174566

To make the plotting easier, I would create a data frame like this:

v1 <- c(0.1, 0.6, 0.8, 0.11) 
v2 <- c(0.3, 0.1, 0.5, 0.7)

df <- data.frame(x = c(v1, v2), group = rep(c("Actual", "Simulated"), each = 4))

Now you can plot the densities easily using ggplot:

library(ggplot2)

ggplot(df) + 
  stat_density(aes(x, linetype = group), geom = "line", position = "identity") +
  scale_linetype_manual(values = c(1, 2)) +
  theme_bw() +
  theme(legend.position = c(0.9, 0.85))

Of course, this doesn't look much like the density plot you provided - that's just because the data in v1 and v2 are too short to have a central tendency. Here's exactly the same plot with some toy data that better matches the data used in your plot:

set.seed(69)
v1 <- rnorm(100, -0.1, 0.12)
v2 <- rnorm(100, 0, 0.06)
df <- data.frame(x = c(v1, v2), group = rep(c("Actual", "Simulated"), each = 100))

ggplot(df) + 
  stat_density(aes(x, linetype = group), geom = "line", position = "identity") +
  scale_linetype_manual(values = c(1, 2)) +
  theme_bw() +
  theme(legend.position = c(0.9, 0.85)) +
  scale_x_continuous(limits = c(-.6, .4))

enter image description here

Created on 2020-05-21 by the reprex package (v0.3.0)

Upvotes: 1

Related Questions