James Owers
James Owers

Reputation: 8335

ggplot individual densities with mean density

I would like to plot densities from two different categories as lines and then the mean density of each category overlaid as alpha. Below is code to show the 'mean' densities only summarised over disease and treatment. How can I overlay this with lines representing the density for each individual (preferably red for disease = 1 and blue for disease = 2)? i.e. a line per disease, treatment combination in the example:

# Initialise data frame in melted form
    # 2 disease categories
    # 10 people from each disease
    # 2 treatments given to each individual
    # 25 variables with measured values per person/treatment combination
set.seed(6737334)
df <- data.frame(id = 1:(2*10*2*25), var = rep(1:25, times=2*10), 
                 val = rnorm(2*10*2*25),
                 disease = as.factor(rep(1:2, each=2*10*25)),
                 treat = as.factor(rep(1:2, times=10, each=25)))

plot <- ggplot(df, aes(x=val, fill=disease)) + geom_density(alpha=.3) +
    facet_grid(treat ~ .)

print(plot)

Image of ggplot produced by the above code

################## EDIT

This code is very very close to my desired solution. How can I make the lines, currently black, match the colour of the disease category?

# Initialise data frame in melted form
    # 2 disease categories
    # 10 people from each disease
    # 2 treatments given to each individual
    # 25 variables with measured values per person/treatment combination
set.seed(6737334)
df <- data.frame(id = 1:(2*10*2*25), var = rep(1:25, times=2*10),
                 test = as.factor(rep(1:(2*10), each=25)),
                 val = rnorm(2*10*2*25),
                 disease = as.factor(rep(1:2, each=2*10*25)),
                 treat = as.factor(rep(1:2, times=10, each=25)))

ggplot(df) + 
    geom_density(aes(x=val, group=test, color=disease)) +
    geom_density(aes(x=val, fill=disease), color=NA, alpha=.5) +
    facet_grid(treat ~ .)

enter image description here

Upvotes: 1

Views: 1025

Answers (2)

James Owers
James Owers

Reputation: 8335

The method turned out to be to utilise the group option of aes() with interaction. See full example below. Thank you to @Jaap above and this question for pointing in the right direction.

# Initialise data frame in melted form
    # 2 disease categories
    # 10 people from each disease
    # 2 treatments given to each individual
    # 25 variables with measured values per person/treatment combination
set.seed(6737334)
df <- data.frame(id = 1:(2*10*2*25), var = rep(1:25, times=2*10),
                 test = as.factor(rep(1:(2*10), each=25)),
                 val = rnorm(2*10*2*25),
                 disease = as.factor(rep(1:2, each=2*10*25)),
                 treat = as.factor(rep(1:2, times=10, each=25)))

ggplot(df) + 
    geom_density(aes(x=val, group=interaction(test,disease), colour=disease)) +
    geom_density(aes(x=val, fill=disease), alpha=.5) +
    guides(fill=guide_legend(title="mean")) +     # else legend title 'NA'
    facet_grid(treat ~ .)

enter image description here

Upvotes: 1

Jaap
Jaap

Reputation: 83215

If I understand your question correctly, this is probably what you're looking for:

ggplot(df) + 
  geom_density(aes(x=val, color=disease)) +
  geom_density(aes(x=val), color=NA, fill="grey", alpha=.5) +
  facet_grid(treat ~ .) +
  theme_bw()

which gives: enter image description here

Upvotes: 2

Related Questions