tags:

views:

316

answers:

2

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?

alt text

alt text

(Q2): when i=7 R gives following warning:

(data$ymin == 0)) warning("Filling not well defined when ymin != 0") : 
missing value where TRUE/FALSE needed

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

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

everytime?

Thanks

+1  A: 

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.

(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:

  • 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
  • You can get your bars narrower and centered on the plot by using the expand option of 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, (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.

(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.

(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.

Aniko
Yes. I want to keep bar width same for all branches, Since some branches have so many x values (some has only one) when i scroll through plots i get from one huge bar to many tiny ones.
mehmetselim
I still think that keeping the same x levels for all plots is the best thing to do if you want to compare plots, but I edited my answer to include a solution using `expand`.
Aniko
+1  A: 

(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.

(Q2) replace binwidth = 1 with stat="identity", as I don't think you need stat="bin".

(Q3) Other options include drop.levels in gdata-package, and dropUnusedLevels in Hmisc-package.

learnr