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))
You need to convert your data to long format, so instead of having columns for Male and Female you have 'Sex' and 'Number'.
Converting data to long format is often useful for ggplot2 in general.