[R-sig-Geo] Export subsets of Spatial Points Data Frame to images
Frede Aakmann Tøgersen
frtog at vestas.com
Tue Jan 6 08:17:35 CET 2015
Hi Ariel
First, it looks like you’re learning R. The best way to learn R is to solve on your own the problems you encounter. Letting people show how to do your R work doesn’t help you learning R that much.
Second, in the future please include the error message and the trace back from latest error when asking questions on R problems. Then it is much easier for us to answer you. Like this:
> for (i in names(SB2)) {
+ jpeg(paste(i, ".jpg", sep=""))
+ print(bubble(SB2[[i]], "Evade", col=c("#00ff0088", "#00ff0088"), main = "Ev" )) # subsetting using i
+ dev.off()
+ }
Error in jpeg(paste(i, ".jpg", sep = "")) (from #2) : unable to start jpeg() device
In addition: Warning messages:
1: In jpeg(paste(i, ".jpg", sep = "")) :
unable to open file '01/03/2013.jpg' for writing
2: In jpeg(paste(i, ".jpg", sep = "")) : opening device failed
> traceback()
1: jpeg(paste(i, ".jpg", sep = "")) at #2
In this case the trace back didn’t give additional information but it will in most cases.
This indicate that there is problems with the file names. You have:
> names(SB2)
[1] "01/03/2013" "04/01/2013" "04/04/2013" "05/04/2013" "05/12/2012"
[6] "06/12/2012" "07/01/2013" "07/12/2012" "08/04/2013" "08/11/2012"
[11] "09/01/2013" "09/04/2013" "10/01/2013" "10/04/2013" "11/01/2013"
[16] "11/02/2013" "11/03/2013" "11/04/2013" "12/03/2013" "12/04/2013"
[21] "12/11/2012" "13/03/2013" "13/11/2012" "14/03/2013" "14/12/2012"
[26] "15/03/2013" "15/04/2013" "16/04/2013" "16/11/2012" "17/04/2013"
[31] "18/12/2012" "19/11/2012" "20/11/2012" "24/12/2012" "25/03/2013"
[36] "26/12/2012" "27/03/2013" "27/12/2012"
So the format of the date is like day/month/year and this is bad for file names since / (slash) is the delimiter character used in paths.
After importing data (notice I added the stringsAsFactors = FALSE argument in order not to convert strings to factors)
SB <- read.csv("c:/users/frtog/Desktop/SB_040914v2.csv", header=TRUE, sep = ";", stringsAsFactors = FALSE)
You can do either
SB$Fecha <- gsub("/", "-", SB$Fecha)
or
SB$Fecha <- as.POSIXct(strptime(SB$Fecha, format = "%d/%m/%Y"))
Next problem is that in the elements of SB2 your data do not have a column named “Evade” but one named “mean(Evade)” (coming from the summarise()). However bubble seems to not handle such column names graceful so I suggest that you do:
SB <- SB %>% group_by(Fecha, CodPar, x, y) %>% summarise(meanSube = mean(Sube),
meanEvade = mean(Evade),
meanBaja = mean(Baja))
where in summarise() you use the name = value specification on the columns that you want aggregation.
So this works for me:
library(sp)
library(dplyr)
SB <- read.csv("c:/users/frtog/Desktop/SB_040914v2.csv", header=TRUE, sep = ";", stringsAsFactors = FALSE)
## SB$Fecha <- gsub("/", "-", SB$Fecha)
SB$Fecha <- as.POSIXct(strptime(SB$Fecha, format = "%d/%m/%Y"))
SB <- SB %>% group_by(Fecha, CodPar, x, y) %>% summarise(meanSube = mean(Sube),
meanEvade = mean(Evade),
meanBaja = mean(Baja))
SB <- SB[complete.cases(SB),]
SB <- as.data.frame(SB)
coordinates(SB) <- ~ x + y
proj4string(obj = SB) <- CRS("+init=epsg:32719")
SB2 <- split(SB, f = SB$Fecha)
for (i in names(SB2)) {
jpeg(paste(i, ".jpg", sep=""))
print(bubble(SB2[[i]], "meanEvade", col=c("#00ff0088", "#00ff0088"), main = "Ev" )) # subsetting using i
dev.off()
}
Yours sincerely / Med venlig hilsen
Frede Aakmann Tøgersen
Specialist, M.Sc., Ph.D.
Plant Performance & Modeling
Technology & Service Solutions
T +45 9730 5135
M +45 2547 6050
frtog at vestas.com<mailto:frtog at vestas.com>
http://www.vestas.com<http://www.vestas.com/>
Company reg. name: Vestas Wind Systems A/S
This e-mail is subject to our e-mail disclaimer statement.
Please refer to www.vestas.com/legal/notice<http://www.vestas.com/legal/notice>
If you have received this e-mail in error please contact the sender.
From: Ariel Fuentesdi [mailto:ariel.fuentesdi at usach.cl]
Sent: 5. januar 2015 21:49
To: Frede Aakmann Tøgersen
Cc: r-sig-geo at r-project.org
Subject: Re: [R-sig-Geo] Export subsets of Spatial Points Data Frame to images
Thank you for your help, it works perfectly.
Though, in my original data still gives me some problems to export it to jpg files.
The original data is something like I attach to this mail.
I fixed it with the code you sent me, and it remains as:
SB <- read.csv("SB_040914v2.csv", header=TRUE, sep = ";")
SB <- SB %>% group_by(Fecha, CodPar, x, y) %>% summarise(mean(Sube),
mean(Evade), mean(Baja))
SB <- SB[complete.cases(SB),]
SB <- as.data.frame(SB)
coordinates(SB) <- ~ x + y
proj4string(obj = SB) <- CRS("+init=epsg:32719")
SB2 <- split(SB, f = SB$Fecha)
for (i in names(SB2)) {
jpeg(paste(i, ".jpg", sep=""))
print(bubble(SB2[[i]], "Evade", col=c("#00ff0088", "#00ff0088"),
main = "Ev" )) # subsetting using i
dev.off()
}
giving me troubles in the loop.
Best regards,
Ariel
2015-01-05 12:53 GMT-04:00 Frede Aakmann Tøgersen <frtog at vestas.com<mailto:frtog at vestas.com>>:
Hi Ariel
I cannot execute the for loop of your code. Can you execute the code?
Several things are wrong.
After splitting your sp object called on the date you now have df2 as a named list of sp objects with the names being the dates.
Here is your code:
for (i in df2) {
jpeg(paste(df2[[1]],".jpg",sep=""))
bubble(df[i], "F_Up", col=c("#00ff0088", "#00ff0088"),
main = "df2" )
dev.off()
}
You are trying to define a file name using paste() but it does not work:
>
> paste(df2[[1]],".jpg",sep="")
Error in as.character.default(<S4 object of class "SpatialPointsDataFrame">) :
no method for coercing this S4 class to a vector
>
Also in the loop you're always extracting the first element of df2. Is that what you want?
Next you're trying to make a bubble plot but in the loop you're using the sp object df and not df2. Also R will complain over you subsetting an sp object using an sp object.
There are several ways to obtain what you want. I suggest that you loop over the names of the df2 list of sp objects:
> names(df2)
[1] "2015-01-01" "2015-01-02" "2015-01-03" "2015-01-04" "2015-01-05"
>
Since bubble() is based on the lattice package you will need to use print() on the lattice object created when using bubble() when inside a loop.
Now this I have tested and it works for me.
for (i in names(df2)) {
jpeg(paste(i, ".jpg", sep="")) # i is the name of each element in df2
print(bubble(df2[[i]], "F_Up", col=c("#00ff0088", "#00ff0088"), main = "df2" )) # subsetting using i
dev.off()
}
Yours sincerely / Med venlig hilsen
Frede Aakmann Tøgersen
Specialist, M.Sc., Ph.D.
Plant Performance & Modeling
Technology & Service Solutions
T +45 9730 5135<tel:%2B45%209730%205135>
M +45 2547 6050<tel:%2B45%202547%206050>
frtog at vestas.com<mailto:frtog at vestas.com>
http://www.vestas.com
Company reg. name: Vestas Wind Systems A/S
This e-mail is subject to our e-mail disclaimer statement.
Please refer to www.vestas.com/legal/notice<http://www.vestas.com/legal/notice>
If you have received this e-mail in error please contact the sender.
From: Ariel Fuentesdi [mailto:ariel.fuentesdi at usach.cl<mailto:ariel.fuentesdi at usach.cl>]
Sent: 5. januar 2015 16:44
To: Frede Aakmann Tøgersen
Cc: r-sig-geo at r-project.org<mailto:r-sig-geo at r-project.org>
Subject: Re: [R-sig-Geo] Export subsets of Spatial Points Data Frame to images
Frede,
This is the reproducible example:
date <- seq(as.Date("2015/1/1"), as.Date("2015/1/5"), "days")
stops <- c("aa", "bb", "cc", "dd", "ee")
F_Up <- c(1:10)[-seq(from = 4, to = 8, by = 1)]
x <- c(342328.0, 342374.5, 342413.2, 342348.6, 342394.6)
y <- c(6308284, 6308123, 6307915, 6307903, 6307915)
df <- data.frame(date, stops, F_Up, x, y)
coordinates(df) <- ~ x + y
proj4string(obj = df) <- CRS("+init=epsg:32719")
df2 <- split(df, f = df$date)
for (i in df2) {
jpeg(paste(df2[[1]],".jpg",sep=""))
bubble(df[i], "F_Up", col=c("#00ff0088", "#00ff0088"),
main = "df2" )
dev.off()
}
2015-01-05 3:22 GMT-04:00 Frede Aakmann Tøgersen <frtog at vestas.com<mailto:frtog at vestas.com>>:
Hi Ariel
Please provide a reproducible example. Now I have to guess what the SB object is because you didn't provide any details about that.
I guess that the group_by() and summarise() are from the dplyr package.
I guess that when you have imported the csv file you have an object SB containing columns Fecha, CodPar, x, and y.
If so, your calls to the two dplyr functions are not correct. As first argument they need a data object, which is supposed to be of a tbl type like e.g. data frame.
Why didn't you get an error when issuing e.g. group_by(Fecha, CodPar, x, y) instead of group_by(SB, Fecha, CodPar, x, y)?
Yours sincerely / Med venlig hilsen
Frede Aakmann Tøgersen
Specialist, M.Sc., Ph.D.
Plant Performance & Modeling
Technology & Service Solutions
T +45 9730 5135<tel:%2B45%209730%205135>
M +45 2547 6050<tel:%2B45%202547%206050>
frtog at vestas.com<mailto:frtog at vestas.com>
http://www.vestas.com
Company reg. name: Vestas Wind Systems A/S
This e-mail is subject to our e-mail disclaimer statement.
Please refer to www.vestas.com/legal/notice<http://www.vestas.com/legal/notice>
If you have received this e-mail in error please contact the sender.
> -----Original Message-----
> From: R-sig-Geo [mailto:r-sig-geo-bounces at r-project.org<mailto:r-sig-geo-bounces at r-project.org>] On Behalf Of Ariel
> Fuentesdi
> Sent: 5. januar 2015 04:48
> To: r-sig-geo at r-project.org<mailto:r-sig-geo at r-project.org>
> Subject: [R-sig-Geo] Export subsets of Spatial Points Data Frame to images
>
> 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(<S4 object of class
> "SpatialPointsDataFrame">) :
> no method for coercing this S4 class to a vector
> 6 as.character.default(<S4 object of class
> structure("SpatialPointsDataFrame", package = "sp")>)
> 5 as.character(<S4 object of class structure("SpatialPointsDataFrame",
> package = "sp")>)
> 4 paste(SB[[1]], ".jpg", sep = "")
> 3 gsub("%%", "", s)
> 2 checkIntFormat(filename)
> 1 jpeg(paste(SB[[1]], ".jpg", sep = ""))**
>
> [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org<mailto:R-sig-Geo at r-project.org>
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
[[alternative HTML version deleted]]
More information about the R-sig-Geo
mailing list