你的数据看起来不错。这是一个解决方案geom_map
(以及 colorbrewer 颜色:-):
devtools::source_gist("https://gist.github.com/hrbrmstr/33baa3a79c5cfef0f6df")
gg <- ggplot()
gg <- gg + geom_map(data=ghaMap, map=ghaMap,
aes(map_id=id, group=group,
x=long, y=lat, fill=prod))
gg <- gg + scale_fill_gradient(low="#99d8c9", high="#00441b")
gg <- gg + coord_map()
gg <- gg + theme_map() + theme(legend.position="right")
gg
您可以省略devtools…
and theme_map()
而不影响解决方案。
EDIT使用简化的多边形
library(maptools)
library(sp)
library(ggplot2)
library(rgeos)
library(rgdal)
# get the shapefile
download.file("http://biogeo.ucdavis.edu/data/gadm2/shp/GHA_adm.zip", "GHA_adm.zip")
unzip("GHA_adm.zip", exdir="GHA_adm")
# simplify it
setwd("GHA_adm")
system("ogr2ogr -simplify 0.001 simple1.shp GHA_adm1.shp") # simplify (500K -> 80K)
setwd("..")
# read it in
gadm <- readOGR("GHA_adm", "simple1")
# convert it
gadm_map <- fortify(gadm, region="NAME_1")
# make the values we want to fill with
# you can use "ID_1" but folks I've seen generally like using names. works either way
prod <- data.frame(id=gadm$NAME_1, value=c(12, 26, 12, 22, 0, 11, 4, 5, 4, 4), stringsAs=FALSE)
# merge the data together
gadm_map <- merge(gadm_map, prod, all.x=TRUE) # add it right into the fortified data frame
# plot it
devtools::source_gist("https://gist.github.com/hrbrmstr/33baa3a79c5cfef0f6df")
gg <- ggplot()
gg <- gg + geom_map(data=gadm_map, map=gadm_map,
aes(map_id=id, group=group,
x=long, y=lat, fill=value))
gg <- gg + scale_fill_gradient(low="#99d8c9", high="#00441b")
gg <- gg + coord_map()
gg <- gg + theme_map() + theme(legend.position="right")
gg
(与上面相同的地图,边缘细节稍少)
Using lsos
(via https://stackoverflow.com/questions/1358003/tricks-to-manage-the-available-memory-in-an-r-session):
lsos()
## Type Size Rows Columns
## gadm_map data.frame 275256 5167 9
## gadm SpatialPolygonsDataFrame 168296 10 9
## gg gg 34792 9 NA
## prod data.frame 2136 10 3
与OP相同:
## Type Size Rows Columns
## ghaMap data.frame 3689120 31735 18
## gadm SpatialPolygonsDataFrame 589424 10 11
## gg gg 34792 9 NA
## ghaDF data.frame 5088 10 12
## con url 552 1 NA