在ggplot2中将辅助分组标签添加到x轴

时间:2014-09-16 18:16:58

标签: r plot ggplot2

我想创建一个包含两个级别的x轴标签的堆叠条形图。对于每个堆叠的条形图,都有主要标签(dat$HUC_12_NAM),然后我想按dat$HUC_10_NAM对这些堆叠的条形图进行分组,并对该组进行标记。我可能会使用annotate来手动定义和放置标签,但这会非常耗时,笨重,并且很容易导致标签错误。

这是数据....

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")

这是当前堆积的条形图......

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

由此产生的情节...... enter image description here

下一个标签或分组将来自dat $ HUC_10_NAM,在此示例中将添加两个额外的标签,&#39; Sand River-Frontal Lake Superior&#39;和&#39; Chequamegon Bay-Frontal Lake Superior&#39;。

也许这只会太杂乱......尤其是长名字。但是,我想看看是否有办法快速,轻松地添加这些二级标签。

由于 -cherrytree

1 个答案:

答案 0 :(得分:2)

如果您愿意而不是添加第二行标签,那么您可以这样做:

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

顺便提一下,您可以使用换行符重新格式化较长的标签,例如:

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

enter image description here