[R] Labeling charts within a loop

Peter Dalgaard p.dalgaard at biostat.ku.dk
Tue Nov 30 01:32:24 CET 2004


"Doran, Harold" <HDoran at air.org> writes:

> Hi All:
> 
> This may turn out to be very simply, but I can't seem to add the name of
> the school to a chart. The loop I created is below that subsets a
> dataframe and creates a chart for each school based on certain
> variables. As it stands now, they title includes the school's ID number.
> Instead, I want to replace this with the school's actual name, which is
> stored in a variable called schname.
> 
> But so far, my attempts have been messy and ugly? Can anyone see a
> mistake in my code?

No overt mistakes, but multiple infelicities. Don't listen to guys who
want to create sequences of variable names, it is (almost) invariably
the wrong idea.

> #This portion of code takes the subset
> school.list<-as.vector(unique(mansfield.dat$schirn))
> for(school.number in school.list){
> assign(paste("school", school.number, sep=""),
> subset(mansfield.dat,mansfield.dat$schirn==school.number))}

Lessee, that was basically

byschool <- split(mansfield.dat, mansfield.dat$schirn) 

below, you use only 4 variables from the data frame and under
different names, so maybe first extract and rename them and have

#--------------
# code should be executable from here on

vars <- c("bpra3ccf", "bprr3ccf", "bprl3ccf", "bpri3ccf")
d2 <- mansfield.dat[vars]
names(d2) <- c("AV", "RP", "LT", "IT")
byschool <- split(d2, mansfield.dat$schirn)

# now to get the averages, you can do

mnList <- lapply(byschool, colMeans)

# now, let's do a little function to do one of your plots. It will have
# three arguments, the mean vector, the number of the school, and the
# name of the school

doPlot <- function(mn, no, name)
{
   # (I won't argue with your filename scheme)
   pdf(file=paste("school", no, "rel.pdf", sep="")
   # The barplot should get the names right automagically now:
   barplot(mn, width=c(.5,.5,.5,.5), ylim = c(0, 4), col=("salmon"))
   legend(1.0,3.8,legend=c("1=Relative Weakness", 
                "2=Similar to District", "3=Relative Strength"))
   title(main=(paste("Grade 3 Relative Strengths and Weaknesses", "\n",
      "School:", name, sep="")))
  
    dev.off()
}

# All set, now

num <- names(mnList)
mapply(doPlot, mnList, num, schname[num])

# done! End executable code
#--------------------------

Obviously, not having your data, all the above is untested, and most
likely not entirely bugfree.

> 
> for(school.number in school.list){
> AV<-mean(get(paste("school",school.number,sep=""))[["bpra3ccf"]])
> RP<-mean(get(paste("school",school.number,sep=""))[["bprr3ccf"]])
> LT<-mean(get(paste("school",school.number,sep=""))[["bprl3ccf"]])
> IT<-mean(get(paste("school",school.number,sep=""))[["bpri3ccf"]])
> 
> 
> strengthbar<-cbind(AV,RP,LT,IT)
> 
> pdf(paste("school",school.number,"rel", ".pdf",sep=""))
> barplot(strengthbar,names=c("AV", "RP", "LT",
> "IT"),width=c(.5,.5,.5,.5),ylim = c(0, 4),col=("salmon"))
> legend(1.0,3.8,legend=c("1=Relative Weakness", "2=Similar to District",
> "3=Relative Strength"))
> 
> 
> 
> title(main=(paste("Grade 3 Relative Strengths and Weaknesses", "\n",
> "School ", school.number,sep="")))
> 
> 
> dev.off()
> 
> }


-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907




More information about the R-help mailing list