I've read a related post (Simpler population pyramid in ggplot2), but I have a slightly different setup which results in a messed-up pyramid.
Make the test data frame:
test <- data.frame(cbind(c(replicate(3,"population 1"), replicate(3,"population 2")),c("top","middle","bottom","top","middle","bottom"),c(70,25,5,82,13,3)))
Fix the factor ordering:
levels(test$X3)
[1] "13" "25" "3" "5" "70" "82"
test$X3 <- factor(test$X3, levels=c(70,25,5,82,13,3))
levels(test$X2)
[1] "Bottom" "Middle" "Top"
test$X2 <- factor(test$X2, levels=c("Top","Middle","Bottom"))
Try
library(ggplot2)
ggplot(data = test, aes(x=X3, y=X2)) +
geom_bar(data = subset(test, X1=="population 1") , stat = "identity")+
coord_flip()
But it's wrong, and I can't figure out why. The top/middle/bottom factors are in inverse order:
Ultimately I want to make the following:
EDIT - I fixed the one-sided block by imposing the factor re-order in the opposite direction explicitly (below) but I still do not understand why ggplot won't recognize how to plot the data, so any explanation is welcome.
# THIS PLOTS ONE SIDE OF THE PYRAMID CORRECTLY
testdf <- data.frame(cbind(c(replicate(3,"population 1"), replicate(3,"population 2")),c("Top","Middle","Bottom","Top","Middle","Bottom"),c(70,25,5,82,13,3)))
testdf$X3 <- factor(testdf$X3, levels=c(5,25,70,3,13,82))
testdf$X2 <- factor(testdf$X2, levels=c("Bottom","Middle","Top"))
g <- ggplot(data = testdf, aes(x=X3, y=X2))
g <- g + geom_bar(data = subset(testdf, X1=="population 1") , stat = "identity")
g + coord_flip()