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/
alt文本http://img411.imageshack.us/i/95325388.jpg/
alt text http://img411.imageshack.us/i/91510133.jpg/
alt文本http://img411.imageshack.us/i/91510133.jpg/
(Q2): when i=7
R gives following warning:
(Q2):当i= 7r时,发出以下警告:
(data$ymin == 0)) warning("Filling not well defined when ymin != 0") : missing value where TRUE/FALSE needed
(数据$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?
每一次?
2 个解决方案
#1
2
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. My guess is that some of your questions stem from this mistake, but I am not sure.
你在创作一些非常奇怪的情节。通过使用position="fill",你可以将每条线展开,使其具有1的高度(因为对应于字母的一个观测值是面板中对应于字母的所有观测值的100%),完全失去了你想要绘制的任何信息。我猜你的一些问题源于这个错误,但我不确定。
(Q1) Do you want the bar width to be the same in plots for the different branches? Since you are changing the number of levels of the x variable, the bars have to get wider to fill up the plot. Some solution options:
(Q1)你是否希望不同分支的条形宽度相同?因为你在改变x变量的等级,条形必须变宽才能填满整个图。一些解决方案选择:
- 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 ofscale_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, (N-k)/2))
to your plot. The first term is a multiplicative expansion factor, and this is the default value, and the second term is an additive factor. - 通过使用scale_x_discrete的展开选项,可以使条形图更窄、更集中。如果你有N个总x值(这里N=26个字母),但是一个特定的图只使用其中的k个,然后在你的图中添加+ scale_x_discrete(expand=c(0.05, (N-k)/2))第一项是乘法扩张因子,这是默认值,第二项是加性因子。
(Q2) i=7 is the only group that has multiple number
values corresponding to the same letter/ltrgroup combination. The bar geom does not know what to do with that. I agree that the error message is really cryptic.
(Q2) i=7是唯一一个具有相同字母/ltrgroup组合对应的多个数值的组。bar geom不知道该怎么做。我同意错误消息是非常神秘的。
(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),然后子设置将不会保留未使用的级别。
#2
1
(Q1) I don't think it is possible to fix bar width. Aniko's suggestion to keep all the levels makes most sense to me.
(Q1)我认为不可能固定bar的宽度。对我来说,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。gdata包中的级别,Hmisc-package中的dropunusedlevel。
#1
2
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. My guess is that some of your questions stem from this mistake, but I am not sure.
你在创作一些非常奇怪的情节。通过使用position="fill",你可以将每条线展开,使其具有1的高度(因为对应于字母的一个观测值是面板中对应于字母的所有观测值的100%),完全失去了你想要绘制的任何信息。我猜你的一些问题源于这个错误,但我不确定。
(Q1) Do you want the bar width to be the same in plots for the different branches? Since you are changing the number of levels of the x variable, the bars have to get wider to fill up the plot. Some solution options:
(Q1)你是否希望不同分支的条形宽度相同?因为你在改变x变量的等级,条形必须变宽才能填满整个图。一些解决方案选择:
- 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 ofscale_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, (N-k)/2))
to your plot. The first term is a multiplicative expansion factor, and this is the default value, and the second term is an additive factor. - 通过使用scale_x_discrete的展开选项,可以使条形图更窄、更集中。如果你有N个总x值(这里N=26个字母),但是一个特定的图只使用其中的k个,然后在你的图中添加+ scale_x_discrete(expand=c(0.05, (N-k)/2))第一项是乘法扩张因子,这是默认值,第二项是加性因子。
(Q2) i=7 is the only group that has multiple number
values corresponding to the same letter/ltrgroup combination. The bar geom does not know what to do with that. I agree that the error message is really cryptic.
(Q2) i=7是唯一一个具有相同字母/ltrgroup组合对应的多个数值的组。bar geom不知道该怎么做。我同意错误消息是非常神秘的。
(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),然后子设置将不会保留未使用的级别。
#2
1
(Q1) I don't think it is possible to fix bar width. Aniko's suggestion to keep all the levels makes most sense to me.
(Q1)我认为不可能固定bar的宽度。对我来说,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。gdata包中的级别,Hmisc-package中的dropunusedlevel。