worldbank / r-econ-visual-library

This is a repository maintained by DIME Analytics and containing example graphs on how to create graphs for data analysis of Impact Evaluations using R.
https://worldbank.github.io/r-econ-visual-library/
MIT License
47 stars 22 forks source link

Include a stacked area plot #23

Open luisesanmartin opened 3 years ago

luisesanmartin commented 3 years ago

I received an email from one of the attendants of the R course asking if we had an example like this in the visual library. She was asking for something similar to this, but with the densities stacked one against the other instead of overlapping

luisesanmartin commented 3 years ago

Something like this: https://www.r-graph-gallery.com/136-stacked-area-chart.html

What I mean is not actually density plots, but stacked area plots

luizaandrade commented 3 years ago

Hi @luisesanmartin , were you thinking of something like this? image

luisesanmartin commented 3 years ago

yes. The areas should be stacked against each other and not overlapping. In this example, I would understand that the data has ~600 obs with NA and 0 active years, ~50 women with 0 active years, and ~600 men with 0 active years. Is that the case?

luizaandrade commented 3 years ago

yes, that's right.

luisesanmartin commented 3 years ago

thanks, then this is what I suggested indeed

luizaandrade commented 3 years ago
gender_colors <- c("#F4BA3B", "#730B6D")
gender %>%
  ggplot() +
  geom_area(aes(x = founded,
                y = n_gender,
                fill = gender)) +
  labs(x = "Year firm was founded",
       y = "Number of firms",
       title = "Year firm was founded, by gender of leaders") +
  scale_fill_manual(values = gender_colors, 
                    na.value = "gray") +
  theme_classic() +
  theme(
    legend.position = 'top',
    legend.title = element_blank(),
    plot.title = element_text(hjust = 0.5),
    axis.line = element_blank(),
    axis.ticks = element_blank(),
    panel.grid.major = element_line()
  )