[R] Speed up graphics output?

dsheuman@rogers.com dsheuman at rogers.com
Mon May 3 18:51:15 CEST 2004


Hi all,

I've written some code to generate 4 maps per screen and write the output to a jpeg.  The output is fairly quick at the start (about 5 jpegs per minute) and then slows down greatly (1-2 jpegs per minute).

Is there some way to speed it up?  One of my thoughts is to keep the base map static on the screen and just update the points that are being plotted on the map (with the exception of the first map as it has the title I want).  I don't know how to do this though.  After it writes out the jpeg in updates the screen to blank.

Thanks,

Danny



platform i386-pc-mingw32
arch     i386           
os       mingw32        
system   i386, mingw32  
status                  
major    1              
minor    9.0            
year     2004           
month    04             
day      12             
language R 

-------------------------------------------
#Input file is comma-delimited and has GeogID, LON, LAT and CLUSTER
#LON should be in column 2 and LAT in column 3

library(maptools)

#Number of clusters in total
ClusCount <- 150

#Read in Cluster Assignments
datain <- as.data.frame( read.table("c:\\data\\Run1\\kmeansout_150.txt", header=TRUE ))
colnames(datain) <- c("geogid","long","lat","cluster")


#Set up screen device
#split 2x2 - permits 1 map per section
par(bg="white") 
split.screen(c(2,2))

#Load maps once only
w <- read.shape("c:\\data\\region1.shp", dbf.data = TRUE)
x <- read.shape("c:\\data\\region2.shp", dbf.data = TRUE)
y <- read.shape("c:\\data\\region3.shp", dbf.data = TRUE)
z <- read.shape("c:\\data\\region4.shp", dbf.data = TRUE)

#Loop through the clusters and produce maps of each region
#The base maps stay the same with the exception of map1
#which has the title of Cluster # and the points which
#reflect the current cluster.

#Subset data on cluster
for(i in 1:ClusCount){
	mapit <- subset(datain, cluster == i)

	screen(1)
	plot(w, xlab="", ylab="", fg="white", axes = F, main=paste("Cluster",i))
	points( as.matrix(mapit[,2]), as.matrix(mapit[,3]), col="red", pch=19)
	
	screen(2)
	plot(x, ylim=c(42.9, 44.2), xlim=c(-80.2, -78.5), xlab="", ylab="", fg="white", axes = F)
	points( as.matrix(mapit[,2]), as.matrix(mapit[,3]), col="red", pch=19)
	
	screen(3)
	plot(y, ylim=c(45.2, 45.8), xlim=c(-74.3, -73.0), xlab="", ylab="", fg="white", axes = F)
	points( as.matrix(mapit[,2]), as.matrix(mapit[,3]), col="red", pch=19)
	
	screen(4)
	plot(z, ylim=c(48.9, 49.7), xlim=c(-123.6, -121.8), xlab="", ylab="", fg="white", axes = F)
	points( as.matrix(mapit[,2]), as.matrix(mapit[,3]), col="red", pch=19)

	#send to jpeg
	dev.print(jpeg,file=paste("c:\\temp\\cluster",format(i),"_canada.jpeg",sep=""),width=1024,height=768, pointsize = 12,quality = 100, bg = "white") 
	
}




More information about the R-help mailing list