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