在堆叠的ggplot2条形图中打印组x轴标签

时间:2014-07-09 05:27:52

标签: r ggplot2

这是我的数据框

> head(dat1, n=10)
   GROUP  id variable value
B G17       P1 0.002
A  G1       P3 0.002
A  G1       P2 0.003
A  G4       P2 0.003
A  G4       P3 0.003
A  G1       P4 0.003
A  G7       P2 0.004
B G13       P2 0.004
A  G4       P4 0.004
B G15       P4 0.004

现在,绘制数据

panel<-theme(panel.background = element_rect(fill = "transparent",colour = NA), panel.grid.minor = element_blank(), panel.grid.major = element_blank(),
             plot.background = element_rect(fill = "transparent",colour = NA))
p<-ggplot(data=dat1, aes(x=id, y=value, fill=variable))+geom_bar(stat="identity", width=1)+scale_y_continuous(expand = c(0,0))+ 
  scale_fill_brewer(palette="Set1") 
q<-p+panel+xlab(" ")+ylab(" ")
q

图表是, plot

现在,我想用我的df第一列中的常用组标签替换x轴标签。
这类问题已经在这里得到解答:Multirow axis labels with nested grouping variables 但我的问题不同,因为我正在使用叠加的情节。
求救!

2 个答案:

答案 0 :(得分:3)

借助@ agstudy提出的解决方案,我们可以创建一个自定义轴功能。我们这样做

element_grob.element_custom <- function(element, x ,...)  {
    cat <- list(...)[[1]]
    groups <- levels(element$categories)
    ll <- split(element$levels, element$categories)
    tt <- as.numeric(x)
    group.pos <- sapply(groups, function(g) mean(range(tt[ cat %in% ll[[g]] ])))
    tg <- textGrob(groups, x=unit(group.pos, 'native'))
    gTree(children=gList(tg), cl = "custom_axis")
}

axis.groups = function(levels, categories) {
    stopifnot(is.factor(levels) & is.factor(categories))
    structure(
        list(categories=categories, levels=levels),
        class = c("element_custom","element_blank")  
    )
}

grobHeight.custom_axis <- 
    heightDetails.custom_axis = function(x, ...)
    unit(1, "lines")

这些函数共同定义自定义轴的属性。然后,让我们使用您的示例数据

dat1 <- structure(list(GROUP = structure(c(2L, 1L, 1L, 1L, 1L, 1L, 1L, 
2L, 1L, 2L), .Label = c("A", "B"), class = "factor"), id = structure(c(17L, 
1L, 1L, 4L, 4L, 1L, 7L, 13L, 4L, 15L), .Label = c("G1", "G2", 
"G3", "G4", "G5", "G6", "G7", "G8", "G9", "G10", "G11", "G12", 
"G13", "G14", "G15", "G16", "G17"), class = "factor"), variable = structure(c(1L, 
3L, 2L, 2L, 3L, 4L, 2L, 2L, 4L, 4L), .Label = c("P1", "P2", "P3", 
"P4"), class = "factor"), value = c(0.002, 0.002, 0.003, 0.003, 
0.003, 0.003, 0.004, 0.004, 0.004, 0.004)), .Names = c("GROUP", 
"id", "variable", "value"), row.names = c(NA, -10L), class = "data.frame")

现在,我们调用自定义函数。函数axis.groups有两个参数,第一个是每个条形的名称,然后是每个组所属的类别。

现在我们绘制情节

ggplot(data=dat1, aes(x=id, y=value, fill=variable))+
    geom_bar(stat="identity", width=1)+
    scale_y_continuous(expand = c(0,0))+ 
    scale_fill_brewer(palette="Set1")  + 
    xlab(" ")+ylab(" ") +
    panel + 
    theme(axis.text.x = axis.groups(un$id, un$GROUP))

,结果是

enter image description here

您需要确保您的关卡按&#34; GROUP&#34;排序。而不是&#34; id&#34;因为标签只会放在条形图的中心,所以你不希望这些组重叠

如果每组中总是有一个奇数,那么你也可以

gb<-tapply(as.numeric(dat1$id), dat1$GROUP, 
    function(x) levels(dat1$id[])[floor(median(x))])

ggplot(data=dat1, aes(x=id, y=value, fill=variable))+
    geom_bar(stat="identity", width=1)+
    scale_y_continuous(expand = c(0,0))+ 
    scale_fill_brewer(palette="Set1")  + 
    panel + 
    xlab("")+ylab("") +
    scale_x_discrete(breaks=gb, labels=names(gb)) 

这需要更少的工作。这次标签不一定在中间,它直接位于中间的一个条形下面(如果组中有一个偶数个条形条,则直接位于它的左侧)

答案 1 :(得分:2)

只需添加手册x刻度:

q + scale_x_discrete(breaks = dat1$id, labels = dat1$GROUP)

修改:可能更好的选择是使用构面:

q + facet_grid(. ~ GROUP, scales = "free")