繁体   English   中英

绘制形状文件时R崩溃

[英]R crashes when plotting shape files

EDIT1:我正在使用Windows Vista和R 2.12.1

我正在从maptools包中绘制带有spplot的84张地图。 我正在使用两个视口,一个视口用于绘制地图,一个视口用于绘制随时间变化的趋势。 我编写了一个循环,遍历数据的每个子集,绘制了根据国家/地区得分进行颜色编码的地图,并绘制了迭代中该得分的趋势。 循环如下所示:

##Read in the shape file, it is 48mb
eu27 <- readShapeSpatial("C:/Users/Thomas/Documents/EU27_shapefile/eu27_cyp.shp",proj4string=CRS     ("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"))

##Drop not used variables from the shapefile data table
eu27@data <- data.frame(Order = 1:dim(eu27)[1], Country = as.character(eu27@data[,c("NAME_ISO")]))

##Fix longitude and lattitude for plotting later
xl <- c(-15,36) 
yl <- c(33,73)

##Set the directory for storing the plots
setwd("C:/Users/Thomas/Documents/maps/")

##Define the breaks and  colors
brks <- seq(0,8,1)
colcode <- terrain.colors(length(brks))

##Count used in the loop to keep track of iterations
count <- 1

##Vector to store the trend in time
total <- vector()

##Beginning of loop
for(i in 2001:2003){
 for(j in 1:12){
                ##Subset the data and merge it with the shape file data table
  dat <- d3[d3$Year == i & d3$Month == j, c("No","No.Neg","Country")]
  eu27dat <- merge(eu27@data,dat, by="Country", all.x=TRUE)
  eu27@data <- eu27dat[order(eu27dat$Order),]
  eu27@data$tot <- eu27@data$No + eu27@data$No.Neg

                ##Store the map plot with countries color coded in the plot1 object
  plot1 <- spplot(eu27, "tot", main = paste("Year: ",i,", Month: ",j, sep = ""), xlim=xl, ylim=yl, at = brks, col.regions = colcode)

                ##Remove the variables created
  eu27@data$No <- NULL
  eu27@data$No.Neg <- NULL
  eu27@data$tot <- NULL

                ##Update the vector with trend data from the current iteration
  total[count] <- sum(dat$No.Neg, na.rm = T)

                ##Store the trend data plot in the plot2 object
  plot2 <-xyplot(total ~ 1:length(total), type = "b", xlab = "Time", ylab = "No Votes", ylim = c(-1,4))

                ##Open a PNG device
  png(file = paste("mapNo",count,".png", sep = ""),width=20, height=12, units="cm",bg="white", pointsize=20, res=300)

                ##Create two viewports for the two plots created above
  pushViewport(viewport(layout = grid.layout(1, 2, unit(c(2, 1), "null"))))

                ##Print plot1 in the first viewport
  pushViewport(viewport(layout.pos.col = 1, layout.pos.row = 1)) 
  print(plot1, newpage = F) 
  upViewport() 

                ##Print plot2 in the second viewport
  pushViewport(viewport(layout.pos.col = 2, layout.pos.row = 1)) 
  print(plot2, newpage = F) 
  upViewport()

                ##Close the device
  dev.off()

                ##Update the count
  count <- count + 1
 }
}

问题是R在8次迭代后崩溃,我怀疑我以某种方式耗尽了大量内存,但是我真的不知道发生了什么。

EDIT2:我收到一条Windows错误消息,提示(从Germahn转换):Windows的R前端不再起作用

EDIT3:我一直在监视Windows任务管理器中的内存使用情况,经过8次迭代后,内存几乎完全用完了。

EDIT4:使用不同的图形设备(png,jpeg,pdf)时出现相同的错误。 我能够在不使用视口的情况下运行循环,因此我怀疑这与视口有关。

最好,托马斯

如果它适用于较小的shapefile,但不适用于较大的shapefile(48Mb是较大的shapefile),则可以,它将是内存。 我看到的有时候可以帮助您解决一件事的是,将循环中的所有内容粘贴到函数中,因此您的文件如下所示:

for(i in 2001:2003){
 for(j in 1:12){
   doit(i,j,[etc])
  }
}

每次调用doit()后,希望会有很多事情超出范围,因此会进行垃圾回收。 我知道这个技巧适用于R(和Splus)的早期版本,但现在已经全部解决了。 查看一些与内存相关的R函数,以了解您的进程使用情况。 这是Windows还是Unix?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM