Creating a population pyramid plot in R using GGPlot

I tried to follow the example given here :


n1 <- ggplot(nigeria, aes(x = Age, y = Population, fill = Gender)) + 
  geom_bar(subset = .(Gender == "Female"), stat = "identity") + 
  geom_bar(subset = .(Gender == "Male"), stat = "identity") + 
  scale_y_continuous(breaks = seq(-15000000, 15000000, 5000000), 
                     labels = paste0(as.character(c(seq(15, 0, -5), seq(5, 15, 5))), "m")) + 
  coord_flip() + 
  scale_fill_brewer(palette = "Set1") + 
  theme_bw()

n1

however my data is setup a little different. Males and females are located in sparate columns in my data. Is there a way create a population pyrimad using the following data:

pop_1950 = data.frame (age_groups = c("0-4", "5 - 12", "13 - 18", "19 - 24", "25 - 34", "35  - 44", "45 - 54", "55 - 64", "65 - 74", "75 - 84", "85 - 94", "95+"),
                       females = c(151272.13, 207176.23, 138778.36, 115109.06, 192698.05,  18232.01, 156810.06, 124283.91, 105981.35,  48945.70,  7273.47,   301.96),
                       males = c(158878.66, 215774.86, 148482.68, 123611.00, 194782.15,  19387.82, 163137.82, 126669.64, 104974.21,  46382.39,   5146.77,   170.79))



Read more here: https://stackoverflow.com/questions/67153088/creating-a-population-pyramid-plot-in-r-using-ggplot

Content Attribution

This content was originally published by NomNonYon at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: