Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201894

ggplot density plot for multiple groups

$
0
0

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

enter image description here

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

enter image description here

We get a completely different density plot for the women


Viewing all articles
Browse latest Browse all 201894

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>