Using R ggplot to plot density plot for multiple plot.
Using the following data.frame:
set.seed(1234)
df <- data.frame(
sex=factor(rep(c("F", "M"), each=5)),
weight=round(c(rnorm(5, mean=0, sd=0),
rnorm(5, mean=2, sd=5)))
)
Let's first plot only the female group:
library(dplyr)
ggplot(df %>% filter(sex=="F"), aes(x=weight, color=sex)) + geom_density()
Women only density plot
But, if we try to plot both men and women:
ggplot(df, aes(x=weight, color=sex)) + geom_density()
density plot for both women and men
We get a completely different density plot for the women