[R] barchart producing incorrect number of barcharts when columns renamed

Colm G. Connolly Colm.Connolly at tcd.ie
Wed Apr 25 16:35:37 CEST 2007


Hi everybody,

I'm having problems with the barchart command in the lattice package.

I'm creating barcharts from matrices with with anything from 20 to 71  
columns. When I leave the column names alone, that is they are set in  
the read.table command (and inherited by subsequent commands) the  
correct number barcharts is created by the "print(barchart(...))"  
command. However, when I reset the column names by means of a scan  
command, the number of barcharts drawn by the same command is  
incorrect: it is always too few. The scan commands produce lists the  
same length as the number of columns for which I want barcharts.

In total I've got four pages with barcharts. The numbers in the table  
below indicate the number of barcharts per page. The numbers without  
() are the number of barcharts that I expect (and get when I don't  
reset the column titles). The numbers in () are the numbers of  
barcharts I get when I reset the column titles.

            Not Clipped            Clipped
Errs       20 (18)                 71 (46)
Stops     32 (24)                 46 (36)

The following is the code used to create the barcharts with sample  
text output below it.

library('lattice')
rm(list=ls())

textFontSize=6;
pdf('../data/cocaineBarcharts.pdf', paper='a4')
fontsize=trellis.par.get("fontsize");
fontsize$text=textFontSize;
trellis.par.set("fontsize", fontsize);

resultsDirs=c("../data/Group.results.noclipping", "../data/ 
Group.results.clipped");
#resultsDirs=c("../data/Group.results.clipped");

for (resultsDir in resultsDirs) {
   cat(resultsDir, "\n")
   if (any(grep("clipped", resultsDir)))
     {
       clipping="(Clipped)";
     }
   else
     {
       clipping="(NOT Clipped)";
     }

   roi.errs=read.table(paste(resultsDir,  
"allGroupsROI.acrossGroupWithinEvent.errs", sep="/"), header=T, sep="");
   roi.errs.names=names(roi.errs);
                                         # ctrl
   roi.errs.ctrl<-roi.errs[roi.errs[,"Group"]=="ctrl", 4:length 
(roi.errs)]
   roi.errs.ctrl.subjects=roi.errs[roi.errs[,"Group"]=="ctrl", 2]
                                         # short
   roi.errs.short<-roi.errs[roi.errs[,"Group"]=="short", 4:length 
(roi.errs)]
   roi.errs.short.subjects=roi.errs[roi.errs[,"Group"]=="short", 2]
                                         # long
   roi.errs.long<-roi.errs[roi.errs[,"Group"]=="long", 4:length 
(roi.errs)]
   roi.errs.long.subjects=roi.errs[roi.errs[,"Group"]=="long", 2]


   roi.stops=read.table(paste(resultsDir,  
"allGroupsROI.acrossGroupWithinEvent.stops", sep="/"), header=T,  
sep="");
   roi.stops.names=names(roi.stops);
                                         # ctrl
   roi.stops.ctrl<-roi.stops[roi.stops[,"Group"]=="ctrl", 4:length 
(roi.stops)]
   roi.stops.ctrl.subjects=roi.stops[roi.stops[,"Group"]=="ctrl", 2]
                                         # short
   roi.stops.short<-roi.stops[roi.stops[,"Group"]=="short", 4:length 
(roi.stops)]
   roi.stops.short.subjects=roi.stops[roi.stops[,"Group"]=="short", 2]
                                         # long
   roi.stops.long<-roi.stops[roi.stops[,"Group"]=="long", 4:length 
(roi.stops)]
   roi.stops.long.subjects=roi.stops[roi.stops[,"Group"]=="long", 2]


#matrixToPlot=as.matrix(roi.errs.ctrl[1:5,])
#yylim=c(floor(min(matrixToPlot)), ceiling(max(matrixToPlot)))
#barplot(matrixToPlot, col=c(2:6), beside=T, ylim=yylim,  
names.arg=colnames(roi.errs.ctrl),
#        border=c(2:6), legend.text=roi.errs$Subject[1:5])

   roi.errs.ctrl.matrix=as.matrix(roi.errs.ctrl)
   roi.errs.short.matrix=as.matrix(roi.errs.short)
   roi.errs.long.matrix=as.matrix(roi.errs.long)

   roi.stops.ctrl.matrix=as.matrix(roi.stops.ctrl)
   roi.stops.short.matrix=as.matrix(roi.stops.short)
   roi.stops.long.matrix=as.matrix(roi.stops.long)

#####################################################################
### errors
#####################################################################

#  pdf(paste(resultsDir, 'errorsByGroup.pdf', sep="/"), paper='a4')
#  fontsize=trellis.par.get("fontsize");
#  fontsize$text=textFontSize;
#  trellis.par.set("fontsize", fontsize);

   roi.errs.ctrl.means=colMeans(roi.errs.ctrl.matrix)
   roi.errs.short.means=colMeans(roi.errs.short.matrix)
   roi.errs.long.means=colMeans(roi.errs.long.matrix)
   yylim=c(floor(min(roi.errs[, 4:length(roi.errs)])), ceiling(max 
(roi.errs[, 4:length(roi.errs)])))

   errs.Means=rbind(roi.errs.ctrl.means, roi.errs.short.means,  
roi.errs.long.means)
   rownames(errs.Means)=c('control', 'short', 'long')
   cat("errs.Means dimensions before col name change ", dim 
(errs.Means), "\n");
   colnames(errs.Means) = scan(paste(resultsDir,  
"clusterLocations.errs.csv", sep="/"), sep=",", what="character")
   cat("errs.Means dimensions after col name change ", dim 
(errs.Means), "\n");

   print(barchart(errs.Means, groups=rownames(errs.Means), xlab='Mean  
Intensity',
                  main=paste("Mean Cluster Intensity for Errors",  
clipping),
                  ylab='Group', col=rainbow(3), border=rainbow(3)))
#  dev.off()


#####################################################################
### Stops
#####################################################################

#  pdf(paste(resultsDir, 'stopsByGroup.pdf', sep="/"), paper='a4')
#  fontsize=trellis.par.get("fontsize");
#  fontsize$text=textFontSize;
#  trellis.par.set("fontsize", fontsize);

   roi.stops.ctrl.means=colMeans(roi.stops.ctrl.matrix)
   roi.stops.short.means=colMeans(roi.stops.short.matrix)
   roi.stops.long.means=colMeans(roi.stops.long.matrix)
   yylim=c(floor(min(roi.stops[, 4:length(roi.stops)])), ceiling(max 
(roi.stops[, 4:length(roi.stops)])))

   stops.Means=rbind(roi.stops.ctrl.means, roi.stops.short.means,  
roi.stops.long.means)
   rownames(stops.Means)=c('control', 'short', 'long')
   cat( "stops.Means dimensions before col name change ", dim 
(stops.Means), "\n");
   colnames(stops.Means) = scan(paste(resultsDir,  
"clusterLocations.stops.csv", sep="/"), sep=",", what="character")
   cat( "stops.Means dimensions after col name change ", dim 
(stops.Means), "\n");

   print(barchart(stops.Means, groups=rownames(stops.Means),  
xlab='Mean Intensity',
                  main=paste("Mean Cluster Intensity for Stops",  
clipping),
                  ylab='Group', col=rainbow(3), border=rainbow(3)))
#  dev.off()

}
dev.off()

Sample output:

../data/Group.results.noclipping
errs.Means dimensions before col name change  3 20
Read 20 items
errs.Means dimensions after col name change  3 20
stops.Means dimensions before col name change  3 32
Read 32 items
stops.Means dimensions after col name change  3 32
../data/Group.results.clipped
errs.Means dimensions before col name change  3 71
Read 71 items
errs.Means dimensions after col name change  3 71
stops.Means dimensions before col name change  3 46
Read 46 items
stops.Means dimensions after col name change  3 46

The text file storing the names for the columns is created by a perl  
script and is in comma separated value format.

Can some please tell me what I've done wrong?

Thank in advance,

Regards,
--
Dr Colm G. Connolly
School of Psychology and Institute of Neuroscience
The Lloyd Building
University of Dublin
Trinity College, Dublin 2, Éire
Tel: +353-1-896-8475
Fax: +353-1-671-3183



More information about the R-help mailing list