带有嵌套分组变量的多轴标签 [英] Multirow axis labels with nested grouping variables

查看:101
本文介绍了带有嵌套分组变量的多轴标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望两个不同的嵌套分组变量的级别出现在图下面的不同行上,而不是图例中。

  data<  -  read.table(text =Group Category Value 
S1 A 73
S2 A 57
S1 B 7
S2 B 23
S1 C 51
S2 C 87,header = TRUE)

ggplot(data = data,aes(x = Category,y = Value,fill = Group))+
geom_bar(position ='dodge')+
geom_text(aes(label = paste值,%)),
position = position_dodge(width = 0.9),vjust = -0.25)


我想要的是这样的:





有什么想法?

解决方案

可以为 axis.text.x 创建一个自定义元素函数。



 库(ggplot2)
库(网格)

##创建一些数据,其中包含非对称填充aes以概括解决方案
data< - read.table(text =Group Category Value
S1 A 73
S2 A 57
S3 A 57
S4 A 57
S1 B 7
S2 B 23
S3 B 57
S1 C 51
S2 C 57
S3 C 87,header = TRUE)

#用户级接口
axis.groups =函数(组){
结构(
list(groups = groups),
##继承,因为它应该是一个element_text
class = c(element_custom,element_blank)

}
#返回带有两个子元素的gTree:
#类别轴
#组轴
element_grob.element_custom< - function(element,x,...){
cat < - list(...)[[1]]
groups < - element $ group $ (数据$组,数据$类别,I)
tt< - as.numeric(x)
grbs< - 映射(函数(z,t){
labs < - ll [[z]]
vp = viewport(
x = unit(t,'native'),
height = unit(2,'line')) ,
width = unit(diff(tt)[1],'native'),
xscale = c(0,length(labs)))
grid.rect(vp = vp)
textGrob(labs,x = unit(seq_along(labs)-0.5,
'native'),
y = unit(2,'line'),
vp = vp)
},cat,tt)
gX < - textGrob(cat,x = x)
gTree(children = gList(do.call(gList,grbs),gX),cl = custom_axis)
}

###gTrees不知道它们的大小
grobHeight.custom_axis =
heightDetails.custom_axis = function(x,... )
单位(3,行)

##最终情节调用
ggplot(data = data,aes(x = Category,y = Value,fill = Group))+
geom_bar(position = position_dodge(width = 0.9),stat ='identity')+
geom_text(aes(label = paste(Value,%)),
position = position_dodge(width = 0.9),vjust = -0.25)+
theme(axis.text.x = axis.groups(unique(data $ Group)),
legend.position =none)


I would like the levels of two different nested grouping variables to appear on separate lines below the plot, and not in the legend. What I have right now is this code:

data <- read.table(text = "Group Category Value
    S1 A   73
    S2 A   57
    S1 B   7
    S2 B   23
    S1 C   51
    S2 C   87", header = TRUE)

ggplot(data = data, aes(x = Category, y = Value, fill = Group)) + 
  geom_bar(position = 'dodge') +
  geom_text(aes(label = paste(Value, "%")), 
            position = position_dodge(width = 0.9), vjust = -0.25)

What I would like to have is something like this:

Any ideas?

解决方案

You can create a custom element function for axis.text.x.

library(ggplot2)
library(grid)

## create some data with asymmetric fill aes to generalize solution 
data <- read.table(text = "Group Category Value
                   S1 A   73
                   S2 A   57
                   S3 A   57
                   S4 A   57
                   S1 B   7
                   S2 B   23
                   S3 B   57
                   S1 C   51
                   S2 C   57
                   S3 C   87", header=TRUE)

# user-level interface 
axis.groups = function(groups) {
  structure(
    list(groups=groups),
    ## inheritance since it should be a element_text
    class = c("element_custom","element_blank")  
  )
}
# returns a gTree with two children: 
# the categories axis
# the groups axis
element_grob.element_custom <- function(element, x,...)  {
  cat <- list(...)[[1]]
  groups <- element$group
  ll <- by(data$Group,data$Category,I)
  tt <- as.numeric(x)
  grbs <- Map(function(z,t){
    labs <- ll[[z]]
    vp = viewport(
             x = unit(t,'native'), 
             height=unit(2,'line'),
             width=unit(diff(tt)[1],'native'),
             xscale=c(0,length(labs)))
    grid.rect(vp=vp)
    textGrob(labs,x= unit(seq_along(labs)-0.5,
                                'native'),
             y=unit(2,'line'),
             vp=vp)
  },cat,tt)
  g.X <- textGrob(cat, x=x)
  gTree(children=gList(do.call(gList,grbs),g.X), cl = "custom_axis")
}

## # gTrees don't know their size 
grobHeight.custom_axis = 
  heightDetails.custom_axis = function(x, ...)
  unit(3, "lines")

## the final plot call
ggplot(data=data, aes(x=Category, y=Value, fill=Group)) + 
  geom_bar(position = position_dodge(width=0.9),stat='identity') +
  geom_text(aes(label=paste(Value, "%")),
            position=position_dodge(width=0.9), vjust=-0.25)+
  theme(axis.text.x = axis.groups(unique(data$Group)),
        legend.position="none")

这篇关于带有嵌套分组变量的多轴标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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