Skip to content

Instantly share code, notes, and snippets.

@oscarperpinan
Last active July 30, 2020 23:18
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save oscarperpinan/5451682 to your computer and use it in GitHub Desktop.
Save oscarperpinan/5451682 to your computer and use it in GitHub Desktop.
library(sp)
library(ggmap)
library(RColorBrewer)
## Get data from OpenPV. Open this URL:
## https://openpv.nrel.gov/search?&state=CA&minsize=100&maxsize=30600&pagenum=1&nppage=25
## and export the results as a CSV file named "californiaOpenPV.csv"
caPV <- read.csv('californiaOpenPV.csv')
## Longitude and Latitude are the names of columns where the spatial information is stored.
## With the coordinates<- method caPV is now an SpatialPointsDataFrame
coordinates(caPV) <- ~ Longitude + Latitude
proj4string(caPV) <- CRS("+proj=longlat +datum=WGS84")
names(caPV)[3] <- 'Pdc.kW'
## Download stamen tiles using the bounding box of the SpatialPointsDataFrame object
bbPoints <- bbox(caPV)
gmap <- get_map(c(bbPoints), maptype='watercolor', source='stamen', crop=FALSE)
## http://spatialreference.org/ref/sr-org/6864/
## Bounding box of the map to resize and position the image with grid.raster
bbMap <- attr(gmap, 'bb')
latCenter <- with(bbMap, ll.lat + ur.lat)/2
lonCenter <- with(bbMap, ll.lon + ur.lon)/2
height <- with(bbMap, ur.lat - ll.lat)
width <- with(bbMap, ur.lon - ll.lon)
## Use sp.layout of spplot: a list with the name of the function
## ('grid.raster') and its arguments
sp.raster <- list('grid.raster', gmap,
x=lonCenter, y=latCenter,
width=width, height=height,
default.units='native')
## Define classes and sizes of the circle for each class
breaks <- c(100, 200, 500, 1e3, 25e3)
classes <- cut(caPV$Pdc.kW, breaks)
meds <- tapply(caPV$Pdc.kW, classes, FUN=median)
sizes <- (meds/max(meds))^0.57 * 1.8
## Finally, the spplot function
spplot(caPV["Pdc.kW"],
cuts = breaks,
col.regions=brewer.pal(n=5, 'Greens'),
cex=sizes,
edge.col='black', alpha=0.7,
scales=list(draw=TRUE), key.space='right',
sp.layout=sp.raster)
@oscarperpinan
Copy link
Author

Watercolor stamen maps are now in jpeg format. Version 2.4 of ggmap fixes this: dkahle/ggmap@d657e1c.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment