Description Usage Arguments Author(s) See Also Examples
This function plots/overlays polygons on a map. Typically, the polygons originate from a shapefile.
1 2 3 4 |
MyMap |
map image returned from e.g. |
polys |
or of class SpatialPolygons from the package sp polygons to overlay; these can be either of class PolySet from the package PBSmapping |
col |
(optional) vector of colors, one for each polygon |
border |
the color to draw the border. The default, NULL, means to use par("fg"). Use border = NA to omit borders, see polygon |
lwd |
line width, see par |
verbose |
level of verbosity |
add |
start a new plot or add to an existing |
textInPolys |
text to be displayed inside polygons. |
... |
further arguments passed to |
Markus Loecher
PlotOnStaticMap mypolygon
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | if (interactive()){
#require(PBSmapping);
shpFile <- paste(system.file(package = "RgoogleMaps"), "/shapes/bg11_d00.shp", sep = "")
#shpFile <- system.file('bg11_d00.shp', package = "RgoogleMaps");
shp=importShapefile(shpFile,projection="LL");
bb <- qbbox(lat = shp[,"Y"], lon = shp[,"X"]);
MyMap <- GetMap.bbox(bb$lonR, bb$latR, destfile = "DC.png");
PlotPolysOnStaticMap(MyMap, shp, lwd=.5, col = rgb(0.25,0.25,0.25,0.025), add = F);
#Try an open street map:
mapOSM <- GetMap.OSM(lonR=bb$lonR, latR=bb$latR, scale = 150000, destfile = "DC.png");
PlotPolysOnStaticMap(mapOSM, shp, lwd=.5, col = rgb(0.75,0.25,0.25,0.15), add = F);
#North Carolina SIDS data set:
shpFile <- system.file("shapes/sids.shp", package="maptools");
shp=importShapefile(shpFile,projection="LL");
bb <- qbbox(lat = shp[,"Y"], lon = shp[,"X"]);
MyMap <- GetMap.bbox(bb$lonR, bb$latR, destfile = "SIDS.png");
#compute regularized SID rate
sid <- 100*attr(shp, "PolyData")$SID74/(attr(shp, "PolyData")$BIR74+500)
b <- as.integer(cut(sid, quantile(sid, seq(0,1,length=8)) ));
b[is.na(b)] <- 1;
opal <- col2rgb(grey.colors(7), alpha=TRUE)/255; opal["alpha",] <- 0.2;
shp[,"col"] <- rgb(0.1,0.1,0.1,0.2);
for (i in 1:length(b))
shp[shp[,"PID"] == i,"col"] <- rgb(opal[1,b[i]],opal[2,b[i]],opal[3,b[i]],opal[4,b[i]]);
PlotPolysOnStaticMap(MyMap, shp, lwd=.5, col = shp[,"col"], add = F);
#compare the accuracy of this plot to a Google Map overlay:
library(maptools);
qk <- SpatialPointsDataFrame(as.data.frame(shp[, c("X","Y")]), as.data.frame(shp[, c("X","Y")]))
sp::proj4string(qk) <- CRS("+proj=longlat");
tf <- "NC.counties";
SGqk <- GE_SpatialGrid(qk)
png(file=paste(tf, ".png", sep=""), width=SGqk$width, height=SGqk$height,
bg="transparent")
par(mar=c(0,0,0,0), xaxs="i", yaxs="i");par(mai = rep(0,4))
PBSmapping::plotPolys(shp, plt=NULL)
dev.off()
maptools::kmlOverlay(SGqk, paste(tf, ".kml", sep=""), paste(tf, ".png", sep=""));
#This kml file can now be inspected in Google Earth or Google Maps
#or choose an aspect ratio that corresponds better to North Carolina's elongated shape:
MyMap <- GetMap.bbox(bb$lonR, bb$latR, destfile = "SIDS.png", size = c(640, 320), zoom = 7);
PlotPolysOnStaticMap(MyMap, shp, lwd=.5, col = shp[,"col"], add = F);
}
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.