I'm trying to make a stacked bar chart of microbiome abundance (dataset at the end). Since there are a lot of classes, it's hard to tell the difference between colors, so I want to put text labels for each category on the bars (I know there are other questions about adding data labels, but I couldn't find anything for category text).
Here's the ggplot
call I'm using:
ggplot(abun, aes(x = status, y = freq, fill = Order)) +
geom_bar(stat = "identity", col = "black") +
ylab("Frequency (%)") +
geom_text(label = abun$Order, position = position_stack(vjust = 0.5)) +
theme(text = element_text(size = 20, face = "bold"), legend.text = element_text(size = 12, face = "plain"))
But here's the chart it puts out:
As far as I can tell, the problem is that the geom_text
labels are being added in incorrect order. How can I get the right labels onto the appropriate segments of the bars?
EDIT: Using
geom_text(label=c(rev(levels(abun$Order)),rev(levels(abun$Order))),position=position_stack(vjust=0.5))
fixes it, but that's surprisingly inelegant. Is there a better workaround?
Dataset:
abun=structure(list(Order = structure(c(11L, 5L, 15L, 1L, 8L, 7L,
12L, 2L, 6L, 10L, 3L, 4L, 14L, 13L, 9L, 11L, 5L, 15L, 1L, 8L,
7L, 12L, 2L, 6L, 10L, 3L, 4L, 14L, 13L, 9L), .Label = c("Actinomycetales",
"Bacteroidales", "BD7-3", "Bifidobacteriales", "Clostridiales",
"Enterobacteriales", "Fusobacteriales", "Lactobacillales", "Other",
"Pasteurellales", "Pseudomonadales", "SBla14", "Synergistales",
"Turicibacterales", "Unidentified"), class = "factor"), status = structure(c(2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("one",
"two"), class = "factor"), freq = c(23.45555556, 20.22962963,
19.98888889, 13.46296296, 7.562962963, 2.607407407, 5.6, 1.451851852,
0.196296296, 1.4, 0.651851852, 0.018518519, 0.644444444, 0.681481481,
1.888888889, 60.42, 21.2, 3.94, 0.2, 0.84, 3.44, 0.44, 3.36,
2.46, 0.22, 0.64, 1.14, 0.4, 0.14, 1.06)), .Names = c("Order",
"status", "freq"), row.names = c(NA, -30L), class = "data.frame")