1
votes

How could I add "Division" label on top of the bars themselves in this example of a stacked bar chart?

ggplot2 and a Stacked Bar Chart with Negative Values

I only want to show it for values with space (don't want to overcrowd the figure), so maybe this could be implemented by a minimum bar height. How could I do it for only bars with that minimum height?

Thanks!

1

1 Answers

0
votes

You can use geom_text() which comes with a check_overlap parameter -- see ?geom_text():

dat <- read.table(text = "   Division Year OperatingIncome
1  A  2012           11460
                  2  B  2012            7431
                  3  C  2012           -8121
                  4  D  2012           15719
                  5  E  2012             364
                  6  A  2011           12211
                  7  B  2011            6290
                  8  C  2011           -2657
                  9  D  2011           14657
                  10 E  2011            1257
                  11 A  2010           12895
                  12 B  2010            5381
                  13 C  2010           -2408
                  14 D  2010           11849
                  15 E  2010             517",header = TRUE,sep = "",row.names = 1)

ggplot(dat, aes(x = Year, y = OperatingIncome, fill = Division)) +
  geom_col() +
  geom_text(aes(label = Division), 
            position = position_stack(vjust = 0.5),
            check_overlap = TRUE)

In the example, however, you will see that the labels do not overlap.

plot