ggplot中的条形图,每组的条数不同 [英] bargraph in ggplot with different numbers of bars per group

查看:188
本文介绍了ggplot中的条形图,每组的条数不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据集看起来像这样:

 `DSET< -data.frame(cbind(c(rep(V1 ,3),rep(V2,3),V3),
c(rep(c(X1,X2,X3),2),X1 $ bc(rep(1,7))))`
`name(DSET)< -c(A,B,C)`
`DSET [,3] < -c(1,-2,1,3,-1,2,-3)`



<带有三个分组变量(V1,V2,V3)和每个组(X1,X2,X3)三个变量。问题是组3(V3)只有一个变量(X1),但缺少另外两个变量。如果我想制作一个条形图,现在一切正常,尽管第3组的栏比第一组和第二组的栏大三倍。


 `Grph <-ggplot(DSET,aes(x = A,y = C,fill = B))`
`dodge < - position_dodge(width = 0.9)`
`Grph + geom_bar(position = dodge)`

我尝试添加两行,缺少组和X2和X3的值为0,并且它有点作用。

 `DSET <-data.frame(cbind(c(rep(V1,3),rep(V2 ,3),rep(V3,3)),
c(rep(c(X1,X2,X3),3)),c(rep(1,9))) )`
`name(DSET)< -c(A,B,C)`
`DSET [,3]< -c(1,-2,1 ,3,-1,2,-3,0,0)`

`Grph < - ggplot(DSET,aes(x = A,y = C,fill = B))`
`dodge < - position_dodge(width = 0.9)`
`Grph + geom_bar(position = dodge)`

但是我真正想要实现的是一个根据每个组的条数来保持条宽保持不变,从而调整组大小的图。有没有办法做到这一点?



期待您的帮助!

解决方案

如果我正确理解您的问题,您希望分配给V3的空间量小于分配的空间到V1和V2,因为V3中只有一个X,并且您希望每个条的宽度代表X是相同的?如果是这样,你可以使用方面得到这个,但不能用简单的x比例。

首先,创建两个数据框(并给它们不同的名称):

  DSET < -  data.frame(A = c(rep(V1,3),rep( (c(X1,X2,X3),2),X1),
C = c(1,-2,1,3,-1,2,-3))

DSET2 < - data.frame(A = rep(c(V1,V2 (3),
B = rep(c(X1,X2,X3),times = 3),
C = c(1,-2 ,1,3,-1,2,-3,0,0))

您的两张图:

  Grph < -  ggplot(DSET,aes(x = A,y = C,fill = B))+ 
geom_bar(position = position_dodge(width = 0.9))
Grph

  Grph%+%DSET2 



为了得到你想要的方面,使用:

  ggplot(DSET,aes(x = B,y = C,fill = B))+ 
geom_bar(position = position_dodge(width = 0.9))+
facet_grid(。〜A, scale =free_x,space =free)


My dataset looks like this:

`DSET<-data.frame(cbind(c(rep("V1",3),rep("V2",3),"V3"),
                       c(rep(c("X1","X2","X3"),2),"X1"),
                       c(rep(1,7))))`    
`names(DSET)<-c("A","B","C")`    
`DSET[,3]<-c(1,-2,1,3,-1,2,-3)` 

With three grouping variables (V1,V2,V3) and three variables per Group (X1,X2,X3). The Problem is that group 3 (V3) has only one variable (X1) but lacks the two others. If I want to produce a bargraph, now, everything works fine despite the fact that the bar of group 3 is three times as large than the ones in group one and two.

`Grph<-ggplot(DSET,aes(x=A,y=C,fill=B))`    
`dodge <- position_dodge(width=0.9)`    
`Grph+geom_bar(position=dodge)` 

I tried to add two rows with the lacking groups and the value 0 for X2 and X3 and It works somewhat.

`DSET<-data.frame(cbind(c(rep("V1",3),rep("V2",3),rep("V3",3)),
                       c(rep(c("X1","X2","X3"),3)),c(rep(1,9))))`    
`names(DSET)<-c("A","B","C")`    
`DSET[,3]<-c(1,-2,1,3,-1,2,-3,0,0)` 

`Grph<- ggplot(DSET,aes(x=A,y=C,fill=B))`    
`dodge <- position_dodge(width=0.9)`    
`Grph+geom_bar(position=dodge)` 

But what I would really like to achieve is a plot that adjusts the group size depending on the number of bars each group has by keeping the bar width constant. Is there any way to do this?

looking forward to your help!

解决方案

If I understand your question correctly, you want the amount of space allocated to "V3" to be less than the space allocated to "V1" and "V2" since there is only one "X" in "V3", and you want the width of each bar representing an "X" to be the same? If so, you can get this using facets, but not with a simple x scale.

First, an easier way to create your two data frames (and giving them different names):

DSET <- data.frame(A=c(rep("V1",3),rep("V2",3),"V3"),
                   B=c(rep(c("X1","X2","X3"),2),"X1"),
                   C=c(1,-2,1,3,-1,2,-3))

DSET2 <- data.frame(A=rep(c("V1","V2","V3"), each=3),
                    B=rep(c("X1","X2","X3"), times=3),
                    C=c(1,-2,1,3,-1,2,-3,0,0))

Your two graphs:

Grph <- ggplot(DSET, aes(x=A, y=C, fill=B)) +
  geom_bar(position=position_dodge(width=0.9))
Grph

Grph %+% DSET2

To get like what you want using facets, use:

ggplot(DSET, aes(x=B, y=C, fill=B)) +
  geom_bar(position=position_dodge(width=0.9)) +
  facet_grid(.~A, scale="free_x", space="free")

这篇关于ggplot中的条形图,每组的条数不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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