在堆叠的ggplot2条形图中打印组x轴标签 [英] printing group x-axis labels in stacked ggplot2 bar plot

查看:171
本文介绍了在堆叠的ggplot2条形图中打印组x轴标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的数据框

> 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

现在,绘制数据

Now, plotting the data

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

图是,

现在,我想用df第一栏中的通用组标签替换x轴标签.
此类问题已在此处得到解答:具有嵌套分组的多行轴标签变量 但是我的问题有所不同,因为我使用的是堆积图.
请帮忙!

Now, I want to replace the x-axis labels with common group labels in my df first column.
this type of question is already answered in here: Multirow axis labels with nested grouping variables But my problem is different, because I am using stacked plot.
Help please!

推荐答案

从@agstudy提出的有关您链接的问题的解决方案中大量借鉴,我们可以创建一个自定义轴函数.我们这样做

Borrowing heavily from @agstudy's proposed solution on the question you linked to, we can create a custom axis function. We do that with

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

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

These functions collectively define the properties of the custom axis. Then, let's use your sample data

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具有两个参数,首先是每个独立条的名称,然后是每个组所属的类别.

And now, we call the custom function. The function axis.groups takes two parameters, first, the names for each of the individual bars, then the categories each of those groups belong to.

现在我们绘制情节

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

并导致

您将要确保您的级别按"GROUP"而不是"id"进行排序,因为标签只会位于条形的中间,因此您不希望组重叠

You will want to make sure that your levels are sorted by "GROUP" than "id" because the label will just go in the center of the bars so you don't want the groups to overlap

如果每个组中总是有一个奇数,那么您也可以这样做

If there were always an odd number in each group, then you could also do

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

需要更少的工作.这次标签不必一定在中间,而是直接在中间条的下方(如果组中条的数目为偶数,则在标签的左侧)

which requires far less work. This time the labels doesn't necessarily go in the middle, it goes directly beneath a bar in the middle (or just left of it if there are an even number of bars in a group)

这篇关于在堆叠的ggplot2条形图中打印组x轴标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆