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)
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 ~ .)