I am drawing a cumulative freq polygon graph with R, I would like to separate the categories (three) to better see the trend difference. Unfortunately when I do that, they look on a different Y scale:
ggplot(aes(x = years, color = sex))+
geom_freqpoly(aes(y = cumsum(..count..)), binwidth = 2)
This is the code and the output
I would like all categories starting from count 0. Thank you for your help
Question solved, example with sample dataset:
df <- data.frame(
year = c(1980, 1980, 1980, 1982, 1982, 1982, 1982, 1982,
1990, 1990, 1990, 1990, 1990, 1990, 1990,
1990, 1991, 1991, 1991, 1991, 1991, 1993, 1993),
sex = c("Un", "Ma", "Fe", "Ma", "Fe", "Un", "Un", "Fe", "Ma"
, "Ma", "Ma", "Ma", "Fe", "Fe", "Fe", "Ma", "Ma", "Fe", "Ma",
"Ma", "Fe", "Fe", "Ma")
)
library(ggplot2)
df |>
ggplot(aes(x = year, color = sex)) +
geom_freqpoly(aes(y=cumsum(..count..)))
#cumsum does not take into account the grouping by color = sex
#use ave or tapply instead
df |>
ggplot(aes(x = year, color = sex)) +
geom_freqpoly((aes(y = after_stat(ave(count, color, FUN = cumsum)))))