Search code examples
rggplot2density-plot

R facet_wrap and geom_density with multiple groups


Here's my dataframe:

df <- data.frame(state = sample(c(0, 1), replace = TRUE, size = 100),
                 X1 = rnorm(100, 0, 1),
                 X2 = rnorm(100, 1, 2),
                 X3 = rnorm(100, 2, 3))

What I would like to do is to plot for each variable X1, X2, X3 two densities/histograms (given the value of state) on the same plot BUT in such a way that all of the plots are on the same facet. I've done these things separately:

ggplot() + 
  geom_density(data = df, aes(x = X1, group = state, fill = state), alpha = 0.5, adjust = 2) + 
  xlab("X1") +
  ylab("Density")

ggplot(gather(df[df$state == 0, 2:4]), aes(value)) + 
  geom_density() + 
  facet_wrap(~key, scales = 'free_x')

enter image description here enter image description here

but I struggle to make it work together.


Solution

  • I'm assuming that you want the three facets for variables X1, X2 and X3, each with two curves filled by state.

    You'll need to convert state to a factor, to make it a categorical variable, using dplyr::mutate(). I would also use the newer tidyr::pivot_longer() instead of gather: this will generate columns name + value by default.

    Your data but with a seed to make it reproducible and named df1:

    set.seed(1001)
    df1 <- data.frame(state = sample(c(0, 1), replace = TRUE, size = 100),
                     X1 = rnorm(100, 0, 1),
                     X2 = rnorm(100, 1, 2),
                     X3 = rnorm(100, 2, 3))
    

    The plot:

    library(dplyr)
    library(tidyr)
    library(ggplot2)
    
    df1 %>% 
      pivot_longer(-state) %>% 
      mutate(state = as.factor(state)) %>% 
      ggplot(aes(value)) + 
      geom_density(aes(fill = state), alpha  = 0.5) + 
      facet_wrap(~name)
    

    Result:

    enter image description here