#### plotGoogleMaps setwd("/Users/hughsturrock/Documents/Work/teaching/Spatial Epi UCSF") library(plotGoogleMaps) library(maptools) library(RColorBrewer) library(wesanderson) # Data preparation # Point data data(meuse) head(meuse) # The meuse point data set consists of 155 samples of top soil heavy metal concentrations (ppm), # along with a number of soil and landscape variables. # The samples were collected in a flood plain of the river Meuse, near the village Stein (Germany). # Basic visualization plot(meuse$x,meuse$y) plot(meuse$x,meuse$y,cex=meuse$cadmium/5) # point size proportional to cadmium # Convert to a SpatialPointsDataFrame # For more info see https://cran.r-project.org/web/packages/sp/vignettes/intro_sp.pdf coordinates(meuse)<-~x+y # convert to SPDF # adding Coordinate Referent Sys. proj4string(meuse) <- CRS('+init=epsg:28992') # For nice overview of CRS 'https://www.nceas.ucsb.edu/~frazier/RSpatialGuides/OverviewCoordinateReferenceSystems.pdf' # Create web map of Point data m=plotGoogleMaps(meuse,filename='myMap1.htm') # saves an html file you can open later and share # change some parameters m<-plotGoogleMaps(meuse, filename='myMap2.htm', mapTypeId='SATELLITE', zcol='copper', layerName = 'MEUSE POINTS') # Check out http://kml4earth.appspot.com/icons.html for other icon options # Creat a bubble plot m<-bubbleGoogleMaps(meuse,zcol='zinc', max.radius = 80, filename='myMap3.htm') # change color palette m<-bubbleGoogleMaps(meuse,zcol='zinc', max.radius = 80, filename='myMap3.htm',colPalette=heat.colors(5)) # Plot pie charts with multivariate data m<-segmentGoogleMaps(meuse, zcol=c('zinc','lead','copper'), mapTypeId='ROADMAP', filename='myMap4.htm') # Check out ?plotGoogleMaps for more options to play with # Plot raster data data(meuse.grid) # for a desciptoin see http://www.dpi.inpe.br/gilberto/tutorials/software/R-contrib/sp/html/meuse.grid.html coordinates(meuse.grid)<-c('x','y') meuse.grid<-as(meuse.grid,'SpatialPixelsDataFrame') proj4string(meuse.grid) <- CRS('+init=epsg:28992') plotGoogleMaps(meuse.grid, zcol='dist', mapTypeId='ROADMAP') # distance to meuse river # Plot lines # Convert to SpatialPixelsDataFrame im<-as.image.SpatialGridDataFrame(meuse.grid['dist']) # Us eht ContourLines to Spatial Lines dataframe function in maptools to # generate contour lines you can plot cl<-ContourLines2SLDF(contourLines(im)) proj4string(cl) <- CRS('+init=epsg:28992') mapMeuseCl<- plotGoogleMaps(cl, zcol='level', strokeWeight=1:9 , filename='myMap5.htm', mapTypeId='ROADMAP') ### plotting polygon data # For plotting spatial polygon data frame is used shapefile provided by maptools package. This is # for the 100 counties of North Carolina, and includes counts of numbers of live births (also nonwhite #live births) and numbers of sudden infant deaths, for the July 1, 1974 to June 30, 1978 and # July 1, 1979 to June 30, 1984 periods nc <- readShapeSpatial(system.file("shapes/sids.shp", package="maptools")[1], proj4string=CRS("+init=epsg:4326")) # Plot numbers of SIDs between 1979 and 1984 plotGoogleMaps(nc, zcol="SID79", filename='MyMap6.htm', mapTypeId='TERRAIN', colPalette= wes_palette('Royal1')[c(1,3,4,2)], strokeColor="white") ### Combining different data # To plot multiple data types, the function should contain # argument add=TRUE. The next plot should have the name of previous map the argument # previousMap = . m1<- plotGoogleMaps(cl, zcol='level', strokeWeight=1:9 , add= TRUE) m2<-bubbleGoogleMaps(meuse,zcol='zinc', add=T, colPalette= brewer.pal(5,"Accent"), max.radius = 80,previousMap= m1) m3<- plotGoogleMaps(meuse.grid, zcol='dist',colPalette= brewer.pal(6,"Oranges"),previousMap= m2, filename='combination.htm') # Export as kml file for Google Earth library(plotKML) plotKML(nc["SID79"]) # saves a similarly named kml file in your working directory. # Or change colors plotKML(nc["SID79"],colour_scale = heat.colors(5)) # More resources # http://pakillo.github.io/R-GIS-tutorial/