cherrytree
cherrytree

Reputation: 1581

add secondary grouping label to x axis in ggplot2

I would like to create a stacked bar graph that contains two levels of x-axis labels. For each stacked bar there is the primary label (dat$HUC_12_NAM), then I would like to group these stacked bars by dat$HUC_10_NAM and label this group as well. I could likely use annotate to manually define and place the labels, but that would be very time consuming, clunky, and could easily result in mis-labeling.

Here is the data....

dat <- structure(list(HUC_12_NAM = structure(c(3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Apostle Islands", 
"Raspberry River-Frontal Lake Superior", "Sand River", "Saxine Creek-Frontal Lake Superior"
), class = "factor"), HUC_10_NAM = structure(c(2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 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, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Chequamegon Bay-Frontal Lake Superior", 
"Sand River-Frontal Lake Superior"), class = "factor"), variable = structure(c(9L, 
8L, 4L, 1L, 6L, 11L, 14L, 13L, 10L, 7L, NA, 5L, 15L, 3L, 2L, 
12L, 8L, 6L, 3L, 2L, 4L, 1L, 15L, 5L, 11L, 14L, 10L, 9L, 13L, 
7L, 12L, NA, 12L, 4L, 10L, 8L, 3L, NA, 2L, 6L, 1L, 13L, 7L, 11L, 
9L, 14L, 5L, 15L, 9L, 1L, 8L, 12L, 10L, 4L, 3L, 11L, NA, 7L, 
15L, 13L, 14L, 6L, 5L, 2L), .Label = c("Agriculture", "Barren land", 
"Developed - High intensity", "Developed - Medium intensity", 
"Developed - Low intensity", "Developed - Open space", "Evergreen forest", 
"Deciduous forest", "Mixed forest", "Herbaceous", "Pasture", 
"Shrub", "Woody wetland", "Herbaceous wetland", "Water"), class = "factor"), 
    perc_veg = c(11.8839579283911, 57.2626205743974, 0.00544969027593598, 
    0.514995731075951, 2.59586913477084, 2.53864738687351, 0.108085523806064, 
    5.3007320750604, 0.731166778688078, 6.04007338916238, 0, 
    0.0953695798288797, 0.11807662264528, 0, 0.00363312685062399, 
    12.8013224581736, 58.9563880536275, 4.47423752571726, 0.0158260043860641, 
    0.101738599624698, 0.0633040175442563, 0.180868621555018, 
    1.07390744048292, 0.300694083335217, 2.65876873685876, 0.00226085776943772, 
    0.065564875313694, 15.484614862879, 2.68363817232258, 7.99665393050123, 
    5.94153421808234, 0, 2.79708137828397, 0.0260443580892536, 
    0.0078546476777114, 30.3801236073503, 0.028524773145373, 
    0, 0.470038653134625, 1.99838773021352, 0.0355526158043779, 
    4.43084809524794, 23.6515843651171, 0.169081626325472, 32.6501167862089, 
    0.595713015978007, 0.174455858947064, 2.5845924884764, 23.2366527830367, 
    0.25141991669822, 52.6482393032942, 3.73494888299886, 0.136312003029156, 
    0.00605831124574025, 0, 1.85535781900795, 0, 11.0851950018932, 
    0.427110942824688, 2.85800833017796, 0, 3.54714123438092, 
    0.146914047709201, 0.0666414237031428)), .Names = c("HUC_12_NAM", 
"HUC_10_NAM", "variable", "perc_veg"), row.names = c(1L, 2L, 
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 
17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 
30L, 31L, 32L, 81L, 82L, 83L, 84L, 85L, 86L, 87L, 88L, 89L, 90L, 
91L, 92L, 93L, 94L, 95L, 96L, 97L, 98L, 99L, 100L, 101L, 102L, 
103L, 104L, 105L, 106L, 107L, 108L, 109L, 110L, 111L, 112L), class = "data.frame")

And here is the current stacked bar plot...

library(ggplot2)
p <- ggplot () + geom_bar(data=dat,aes(x=HUC_12_NAM,y=perc_veg,fill=variable),stat='identity')
p <- p + coord_flip() #this helps fit the xlabel
p

And the resulting plot... enter image description here

The next label, or grouping, would be from dat$HUC_10_NAM and in this example would add two additional labels, 'Sand River-Frontal Lake Superior' and 'Chequamegon Bay-Frontal Lake Superior'.

Maybe this would just be too cluttered...especially with the long names. But, I would like to see if there is a way to add these second level labels quickly and easily.

Thanks -cherrytree

Upvotes: 1

Views: 1127

Answers (1)

eipi10
eipi10

Reputation: 93871

If you're willing to facet instead of adding a second row of labels, then you can do this:

ggplot(data=dat, aes(x=HUC_12_NAM, y=perc_veg, fill=variable)) + 
  geom_bar(stat='identity') +
  facet_grid(. ~ HUC_10_NAM, scales="free")

enter image description here

Incidentally, you can reformat the longer labels with a line-break, for example:

dat[,1:2] = lapply(1:2, function(x) gsub("-","\n", dat[,x]))

enter image description here

Upvotes: 2

Related Questions