is there a way to design a map with plotGoogleMaps having a legend that controls layers of the same variable?
Using meuse dataset from gstat package as an example, the goal would be to obtain a thickbox for each type of landuse. Therefore, letting the user decide which layer to look at, either one by one, or a selection of several or all of them:
# Data Preparation
library(sp)
data(meuse)
coordinates(meuse)<-~x+y
proj4string(meuse) <- CRS('+init=epsg:28992')
ic=iconlabels(meuse$landuse, height=12)
m<-plotGoogleMaps(meuse,zcol='landuse',filename='Map.htm', iconMarker=ic)
The best solution would be a command to make a breakdown by values of a variable...
Or alternatively, do we need to create a map for each layer (all 15 of them) and then adding them together? Would it be a more convenient way to acheive this? I got a project with more than 300 layers...
#Alternative the long way : Example with 3 landuse values
data(meuse)
list <- list(unique(meuse$landuse)) #list all 15 landuse values
Ah <- subset(meuse, landuse == "Ah")
coordinates(Ah)<-~x+y
proj4string(Ah) <- CRS('+init=epsg:28992')
ic=iconlabels(Ah$landuse, height=12)
m.Ah<-plotGoogleMaps(Ah,zcol='landuse', legend=FALSE, filename='MapAh.htm', iconMarker=ic, add=TRUE)
Fw <- subset(meuse, landuse == "Fw")
coordinates(Fw)<-~x+y
proj4string(Fw) <- CRS('+init=epsg:28992')
icf=iconlabels(Fw$landuse, height=12)
m.Fw<-plotGoogleMaps(Fw,zcol='landuse',legend=FALSE, filename='MapFw.htm', iconMarker=icf, previousMap=m.Ah, add=TRUE)
W <- subset(meuse, landuse == "W")
coordinates(W)<-~x+y
proj4string(W) <- CRS('+init=epsg:28992')
icw=iconlabels(W$landuse, height=12)
m.W<-plotGoogleMaps(W,zcol='landuse', legend=TRUE, filename='MapFw.htm', iconMarker=icw,previousMap=m.Fw)
The second best solution would be to loop this all.
Here is one solution. Of course, you can make your own function based on this.
library(plyr)
library(plotGoogleMaps)
library(sp)
data(meuse)
# list of SPDFs based on landuse
mlis <- dlply(meuse,'landuse',function(x) {
coordinates(x)<-~x+y
proj4string(x) <- CRS('+init=epsg:28992')
x
})
# prepare colors
cols = PolyCol(meuse$landuse)$col.uniq
#cols = rainbow(15)
names(cols) = levels(meuse$landuse)
# 1st landuse category
lev1 <- levels(meuse$landuse)[1]
ic=iconlabels(mlis[[lev1]]$landuse, height=12, colPalette= cols[lev1])
m<-plotGoogleMaps(mlis[[lev1]],zcol='landuse', legend=FALSE,filename='MapLU.htm', iconMarker=ic,layerName = lev1, add=TRUE)
nlev <-length(levels(meuse$landuse))
for(lev in levels(meuse$landuse)[c(-1,-nlev)]){
ic=iconlabels(mlis[[lev]]$landuse, height=12, colPalette= cols[lev])
m<-plotGoogleMaps(mlis[[lev]],zcol='landuse', legend=FALSE, filename='MapLU.htm', iconMarker=ic,layerName = lev, previousMap=m, add=TRUE)
}
# the last LU cat
lev=levels(meuse$landuse)[nlev]
ic=iconlabels(mlis[[lev]]$landuse, height=12, colPalette= cols[lev])
m<-plotGoogleMaps(mlis[[lev]],zcol='landuse', legend=FALSE, filename='MapLU.htm', iconMarker=ic,layerName = lev,previousMap=m)