0

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:

Chart with backwards labels

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")
Matt
  • 954
  • 1
  • 9
  • 24
  • A workaround would be `geom_text(label = rev(abun$Order), position = position_stack(vjust = 0.5))` if they're reversed. – Dan Jun 02 '17 at 17:42
  • Almost-- looks like I have to use `geom_text(label=c(rev(levels(abun$Order)),rev(levels(abun$Order))),position=position_stack(vjust=0.5))` – Matt Jun 02 '17 at 17:47

3 Answers3

0

Does this work?

ggplot(abun, aes(x = status, y = freq, fill = Order, label = Order)) +
  geom_bar(stat = "identity", col = "black") +
  ylab("Frequency (%)") +
  geom_text(position = position_stack(vjust = 0.5)) +
  theme(text = element_text(size = 20, face = "bold"), legend.text = element_text(size = 12, face = "plain"))
Dan
  • 11,370
  • 4
  • 43
  • 68
0

Changed the geom_text call thus:

ggplot(abun,aes(x=status,y=freq,fill=Order))+
geom_bar(stat="identity",col="black")+
ylab("Frequency (%)")+
geom_text(label=c(rev(levels(abun$Order)),rev(levels(abun$Order))),position=position_stack(vjust=0.5))+
theme(text=element_text(size=20,face="bold"),legend.text=element_text(size=12,face="plain"))

yielding:

enter image description here

It seems geom_text cycles through all factor levels without regard for x. Fixing this required running through the reversed list twice: label=c(rev(levels(abun$Order)),rev(levels(abun$Order)))

So for n factor levels under the x mapping, include c(rev(levels(LabelText))) n times.

Thanks to @Lyngbakr for getting me on the right track.

Matt
  • 954
  • 1
  • 9
  • 24
0

As @Lyngbakr suggested you can fix this with the label aesthetic either in the main plot function or the geom_text as below.

ggplot(abun, aes(x = status, y = freq, fill = Order)) +
    geom_bar(stat = "identity", col = "black") +
    ylab("Frequency (%)") +
    geom_text(aes(label = Order), position = position_stack(vjust = 0.5)) +
    theme(text = element_text(size = 20, face = "bold"), legend.text = element_text(size = 12, face = "plain"))

This occurs becasue the default grouping in geom_text (see: http://ggplot2.tidyverse.org/reference/aes_group_order.html) does not work here and needs to be specified with an aesthetic.

I had a very similar problem myself: Out of order text labels on stack bar plot (ggplot)

Richard J. Acton
  • 885
  • 4
  • 17