1

I have a csv file, which I converted it to Spatial Points Data Frame. And I want to split the spatial data and export each map to a jpg file.

I did the following:

SB <- read.csv("SB_040914.csv", header=TRUE, sep = ";")
SB <- SB %>% group_by(Fecha, CodPar, x, y) %>% summarise(mean(Sube),mean(Evade), mean(Baja))
SB <- data.frame(SB)
SB <- SB[complete.cases(SB),]

coordinates(SB) <- ~ x + y 
proj4string(obj = SB) <- CRS("+init=epsg:32719")

SB <- split(SB, f = SB$Fecha)  

The Split function, shows me the next warnings:

Warning messages: 1: In min(x) : no non-missing arguments to min; returning Inf 2: In max(x) : no non-missing arguments to max; returning -Inf 3: In min(x) : no non-missing arguments to min; returning Inf 4: In max(x) : no non-missing arguments to max; returning -Inf

And for trying to export I did this:

for (i in SB) {  
  jpeg(paste(SB[[1]],".jpg",sep=""))
  bubble(SB[[i]], "mean.Evade.", col=c("#00ff0088", "#00ff0088"), 
         main = "Evasión" )
  dev.off()
}

Which shows me the next error: Error in as.character.default() : no method for coercing this S4 class to a vector 6 as.character.default() 5 as.character() 4 paste(SB[[1]], ".jpg", sep = "") 3 gsub("%%", "", s) 2 checkIntFormat(filename) 1 jpeg(paste(SB[[1]], ".jpg", sep = ""))

As you can see, this is made in R.

The link to the data: https://www.sendspace.com/file/nd6j8h

2
  • 1
    I suggest you post a link your csv file. It will be much easier to reproduce what you did.
    – cengel
    Commented Jan 6, 2015 at 3:32
  • @cengel I edited the post
    – Ariel
    Commented Jan 6, 2015 at 4:13

1 Answer 1

1

The split command requires a data frame or vector and you are giving it a SpatialPointsDataFrame. All you have to do is to do the split first and then create your SpatialPointsDataFrame. Below is the complete code for this.

library(sp)
library(dplyr)
SB <- read.csv("SB_040914.csv", header=TRUE, sep = ",")

SB <- SB %>% group_by(Fecha, CodPar, x, y) %>% summarise(mean(Sube),mean(Evade), mean(Baja))
SB <- data.frame(SB)
SB <- SB[complete.cases(SB),]

# split here!
SB <- split(SB, f = SB$Fecha)

# then produce the maps
lapply(SB, function(x) {
  coordinates(x) <- ~x + y
  proj4string(x) <- CRS("+init=epsg:32719")
  jpeg(print((paste0(gsub("/", "_", x$Fecha[1]),".jpg"))))
  print(bubble(x, "mean.Evade.", col=c("#00ff0088", "#00ff0088"), main = "Evasión" ))
  dev.off()
})

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.