简体   繁体   English

ggplot2中的条形宽度geom_bar

[英]bar width in ggplot2 geom_bar

I am trying to produce plots with a loop. 我正在尝试使用循环生成图。

l1<-factor(rep(letters,4))
n1<-abs(rnorm(104))*10000
b1<-rep(c("1","2","3","4","5","6","7","8"),c(2,2,11,24,11,20,33,1))
k1<-rep((rep(c("A","B","C","D"),c(2,3,4,4))),8)
my.df<-data.frame(l1,b1,k1,n1)                            #make a dataframe

names(my.df)<-c("letter","branch","ltrtype","number")     #factor names
library(ggplot2)

branch.list<-unique(my.df$branch)
sayi<-length(branch.list)                                 # list of factor:letters

for (i in 1:sayi) {

branch.iter<-branch.list[i]
my.df.plot<-subset(my.df,my.df$branch==branch.iter,drop=T)

my.df.plot$branch<-factor(my.df.plot$branch)               #So that unused levels don't show up
my.df.plot$letter<-factor(my.df.plot$letter)               #So that unused levels don't show up
my.df.plot$ltrtype<-factor(my.df.plot$ltrtype)             #So that unused levels don't show up
my.df.plot$number<-as.numeric(as.character(my.df.plot$number))
my.df.plot<-data.frame(my.df.plot)

myfilename<-paste(branch.iter,".jpeg",sep="")
jpeg(file=myfilename)

cizim<-ggplot(my.df.plot,aes(letter,number,fill=ltrtype))
cizim<-cizim + geom_bar(width = 1, position = "fill", binwidth = 1) +     facet_grid(ltrtype~.)
cizim<-cizim + opts(title=branch.iter)

print(cizim)
dev.off()

}

(Q1): When number of levels in x-axis change width of bars change How can i prevent this and make bar width in every plot same? (Q1):当x轴上的级别数改变时,条形的宽度也改变了。如何防止这种情况并使每个图中的条形宽度相同?

alt text http://img411.imageshack.us/i/95325388.jpg/ 替代文字http://img411.imageshack.us/i/95325388.jpg/

alt text http://img411.imageshack.us/i/91510133.jpg/ 替代文字http://img411.imageshack.us/i/91510133.jpg/

(Q2): when i=7 R gives following warning: (Q2):当i=7 R时,发出以下警告:

(data$ymin == 0)) warning("Filling not well defined when ymin != 0") : missing value where TRUE/FALSE needed (data $ ymin == 0))警告(“当ymin!= 0时,填充定义不正确”):缺少值,需要TRUE / FALSE

what can i do about it? 我该怎么办?

(Q3): Is there an easier way to drop unused levels in such a case so i don't have to use (Q3):在这种情况下,有没有更简单的方法来降低未使用的电量,所以我不必使用

 my.df.plot$branch<-factor(my.df.plot$branch)

everytime? 每次?

You are producing some very strange plots. 您正在生成一些非常奇怪的情节。 By using position="fill" you are stretching out each bar to have height 1 (because the one observation corresponding to the letter is 100% of all observations corresponding to the letter within a panel), completely loosing whatever information you are trying to plot. 通过使用position="fill"您可以拉伸每个条形使其高度为1(因为对应于该字母的一个观测值是对应于面板中字母的所有观测值的100%),从而完全失去了您想要绘制的任何信息。 My guess is that some of your questions stem from this mistake, but I am not sure. 我的猜测是您的某些问题是由该错误引起的,但我不确定。

(Q1) Do you want the bar width to be the same in plots for the different branches? (Q1)您是否希望在不同分支的图中,条形宽度相同? Since you are changing the number of levels of the x variable, the bars have to get wider to fill up the plot. 由于您正在更改x变量的级别数,因此条形图必须变宽以填充图。 Some solution options: 一些解决方案选项:

  • Do some smart resizing of the width of the plot to go around that. 对图的宽度进行一些智能调整,以解决该问题。
  • Leave all the x levels in - I think this is the cleanest way 保留所有x级别-我认为这是最干净的方法
  • You can get your bars narrower and centered on the plot by using the expand option of scale_x_discrete . 通过使用scale_x_discreteexpand选项,可以使条形变窄并在图上scale_x_discrete So if you have N total x-values (here N=26 letters), but a particular plot only uses k of them, then add + scale_x_discrete(expand=c(0.05, (Nk)/2)) to your plot. 因此,如果您总共有N个x值(此处N = 26个字母),但是特定绘图仅使用其中的k个,则将+ scale_x_discrete(expand=c(0.05, (Nk)/2))到绘图中。 The first term is a multiplicative expansion factor, and this is the default value, and the second term is an additive factor. 第一项是乘法扩展因子,这是默认值,第二项是加法因子。

(Q2) i=7 is the only group that has multiple number values corresponding to the same letter/ltrgroup combination. (Q2)i = 7是唯一具有对应于相同字母/ ltrgroup组合的多个number值的组。 The bar geom does not know what to do with that. 条形几何不知道该怎么办。 I agree that the error message is really cryptic. 我同意该错误消息确实很神秘。

(Q3) One option is to avoid using factors - use data.frame(...,stringsAsFactors=FALSE) when combining character vectors, and then subsetting will not keep unused levels around. (Q3)一种选择是避免使用因素-在组合字符向量时使用data.frame(...,stringsAsFactors=FALSE) ,然后子集将不会保留未使用的级别。

(Q1) I don't think it is possible to fix bar width. (第1季度)我认为无法固定钢筋宽度。 Aniko's suggestion to keep all the levels makes most sense to me. Aniko关于保持所有级别的建议对我来说最有意义。

(Q2) replace binwidth = 1 with stat="identity" , as I don't think you need stat="bin" . (Q2)将binwidth = 1替换为stat="identity" ,因为我认为您不需要stat="bin"

(Q3) Other options include drop.levels in gdata -package, and dropUnusedLevels in Hmisc -package. (Q3)的其他选项包括drop.levelsgdata -package,和dropUnusedLevelsHmisc -package。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM