I'm creating a stacked bar plot, but can't get ggplot2 to stack the bars in the order I want, even though I've set the factor levels of the stacking variable to the desired order. The data frame I'm using for plotting is called numCty and is pasted in below using dput. Here's my plotting code:
ggplot(numCty, aes(County, count, fill=Status)) +
geom_bar(stat="identity", colour="black", lwd=0.2) +
geom_text(aes(label=count, y=pos), colour="grey30", size=2.4) +
coord_flip() +
xlab("County") + ylab("Number of Projects") +
labs(fill="Project Status") +
theme(axis.text=element_text(size=8),
axis.title=element_text(size=9))
The plot looks exactly how I want it, with one exception: The ordering of the bars is currently (from left to right) green, blue, red (representing Status values of, respectively, Active, Inactive, and Completed). However, the ordering I want is red, green, blue (Status values of, respectively Completed, Active, Inactive).
As you can see in the numCty data frame pasted below, the factor levels of Status are in the order Completed, Active, Inactive, which (I would have thought) should result in ggplot stacking them in that order. Yet, ggplot is ignoring this ordering.
Thanks for any suggestions on how to get the desired ordering of the stacked bars.
Joel
numCty = structure(list(County = structure(c(19L, 19L, 19L, 1L, 2L, 15L,
10L, 10L, 10L, 18L, 18L, 18L, 11L, 11L, 11L, 16L, 16L, 20L, 20L,
20L, 12L, 17L, 17L, 17L, 13L, 3L, 14L, 4L, 21L, 21L, 21L, 5L,
6L, 7L, 9L, 8L), .Label = c("Butte", "Fresno", "San Joaquin",
"Santa Barbara", "Solano", "Stanislaus", "Tulare", "Yolo", "Ventura",
"Kern", "Merced", "Sacramento", "San Diego", "San Mateo", "Imperial",
"Monterey", "San Bernardino", "Los Angeles", "Alameda", "Orange",
"Santa Clara"), class = "factor"), Status = structure(c(2L, 3L,
1L, 2L, 2L, 2L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 3L, 1L, 2L,
3L, 1L, 2L, 2L, 3L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 1L, 2L, 2L, 2L,
2L, 2L), .Label = c("Completed", "Active", "Inactive"), class = "factor"),
count = c(7L, 1L, 1L, 1L, 1L, 4L, 1L, 1L, 1L, 6L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 1L, 4L, 4L, 3L, 3L, 1L, 1L, 3L, 1L, 3L,
1L, 10L, 3L, 3L, 1L, 1L, 1L, 2L, 1L), pos = c(3.5, 7.5, 8.5,
0.5, 0.5, 2, 0.5, 1.5, 2.5, 3, 6.5, 7.5, 0.5, 1.5, 2.5, 1,
3, 0.5, 3, 7, 1.5, 1.5, 3.5, 4.5, 1.5, 0.5, 1.5, 0.5, 5,
11.5, 14.5, 0.5, 0.5, 0.5, 1, 0.5)), .Names = c("County",
"Status", "count", "pos"), row.names = c(NA, -36L), class = "data.frame")