I'm looking to show cumulative proportion of a column variable (e.g., mpg) by color (fcyl) and one without any color. This is different from using cowplot or patchwork to show them side-by-side. I specifically want them to be shown together.
Here's an example:
mtcars <- mutate(mtcars, fcyl = factor(cyl, levels = c("4","6","8"),
labels = c("4 cycles", "6 cycles", "8 cycles")))
mtcars %>% filter(fcyl %in% c("4 cycles", "6 cycles")) %>%
ggplot(aes(x = mpg, color = fcyl)) stat_ecdf(size=2)
#This graph shows cumulative proportion of mpg by 4 and 6 cycles of 'fcyl'
mtcars %>%
ggplot(aes(x = mpg)) stat_ecdf(size=2) #This one is total cumulative density proportion
I tried adding a stat_edcf
to the first plot and its possible to do this for all three factors in fcyl but not the two specific ones.
mtcars %>%
ggplot(aes(x = mpg)) stat_ecdf(size=2)
stat_ecdf(aes(x = mpg, color = fcyl), size=2) #This works
mtcars %>%
ggplot(aes(x = mpg)) stat_ecdf(size=2)
stat_ecdf(aes(x = mpg, color = filter(mtcars, fcyl %in% c("4 cycles", "6 cycles")), size=2))
#This does not work.
How do I show them in one graph?
CodePudding user response:
Just add a new layer:
mtcars %>%
filter(fcyl %in% c("4 cycles", "6 cycles")) %>%
ggplot(aes(x = mpg))
stat_ecdf(size = 2, aes(color = 'total'))
stat_ecdf(size = 2, aes(color = fcyl))
scale_color_manual(values = c("#F8766D", "#00BFC4", "black"))