2
votes

how to adjust transparency when plotting spatial polygons over Open street maps map tiles? I can do it with ggmaps, but I have to do each color seperately.

code for get map tile from the OpenStreetMap package:

map = openproj(openmap(
            c(lat= max(as.numeric(as.character(zip$INTPTLAT10))),
             lon=   min(as.numeric(as.character(zip$INTPTLON10)))),
            c(lat= min(as.numeric(as.character(zip$INTPTLAT10))),
              lon= max(as.numeric(as.character(zip$INTPTLON10)))),
                       type="osm"))
plot(map)

Heat map:

zip=readShapePoly( "tl_2010_04_zcta510.shp" )
zip$groups2=sample(1:10, length(zip[,1]), replace=T)
brks=classIntervals(zip$groups2, n=9, style="quantile")$brks
cols <- colors[findInterval(zip$groups2, brks, all.inside=TRUE)]
plot( zip , col = cols , axes=F , add=TRUE)

This is the phoenix .shp file from US census 2010.

1
You have not provided "zip" at least not in a manner that is obvious. How did you make "zip" from the "phoenix .shp file from US census 2010". It's also unclear whether you understand that transparency often encoded with a fourth hex digit pair, eg; "#D9D9D980"IRTFM
you download the phoenix .shp file from here: goo.gl/DgY0H (the file name is tl_2010_04_zcta510.shp). then read the shape file into R using readShapePoly().cconnell
There's no file at that page with that name.IRTFM
go here: census.gov/cgi-bin/geo/shapefiles2010/main. choose zip tabulation, and then choose Arizona to download the file.cconnell

1 Answers

2
votes

With some difficulty I found the ftp site that had the specified file as a zip file (before the other directions appeared): ftp://ftp2.census.gov/geo/tiger/TIGER2010/ZCTA5/2010/

Then further difficulties ensued as two more packages were needed to run the code above:

require(maptools)
require(classInt)

The transparency information in RGB-encoded color values is the last two hex-code entries in an 8 digit nex-number which is stored as a character value. Appending a value less than "FF" results in some degree of transparency, but it turned out that the "NA" values were not actually getting appended properly and needed to be reset to NA_character_:

 cols <- paste0( cols, "20")  # as suggested in my earlier comment
 plot(map)
 plot( zip , col = cols , axes=F , add=TRUE)
#Error in polypath(x = mcrds[, 1], y = mcrds[, 2], border = border, col = col,  : 
#  invalid color name 'NA20'
 is.na(cols) <- grepl("NA", cols)
 plot( zip , col = cols , axes=F , add=TRUE)

Rather than posting the 1.4 MB file that resulted from converting the 7MB pdf file to PNG format, I zoomed in on the NorthEast corner of Arizona and captured a screenshot: enter image description here

It's possible to see the geographic features and roads through the transparent overlays. That's US-40 running between Flagstaff and Kingman in the lower portion of the plot.