使用ggplot()基于R中的ISO3代码在世界地图上着色国家/地区

Gna*_*ark 4 gis r ggplot2 world-map

我想知道我是否可以使用彩色国家ggplot?使用rworldmap它非常简单,并且通过简单的方法为lat/long值添加点ggplot也很容易.

有没有一种方法可以根据一个简单的表格使用ggplot国家名称(ISO3中的国家名称和每个国家的数字?)着色国家/地区.着色应该基于计数.

p <- ggplot(legend=FALSE) +

geom_polygon(fill = "darkseagreen", data=world, aes(x=long, y=lat,group=group)) + 
 geom_path(colour = "grey40") + 
 theme(panel.background = element_rect(fill = "lightsteelblue2", colour = "grey")) +
 theme(panel.grid.major = element_line(colour = "grey90")
) +
 theme(panel.grid.minor = element_blank()) +
 theme(axis.text.x = element_blank(),
 axis.text.y = element_blank()) +
 theme(axis.ticks = element_blank()) +
 xlab("") + ylab("")
Run Code Online (Sandbox Code Playgroud)

hrb*_*str 9

ggplot非常简单.在下文中,我使用GeoJSON版本的Natural Earth国家边界(存储ISO3代码iso_a3)投射到Winkel-Tripel.我将世界银行人口数据的CSV存储在一个要点中,并将其读入简单表中.然后我构建两个图层,一个是世界几何图形的基础图层,然后是填充多边形图形.因为它是预先投影的,所以我使用coord_equalvs coord_map(这对于choropleth很好,但如果你打算绘制线条则不需要预先投影它们,那么也是如此):

library(maptools)
library(mapproj)
library(rgeos)
library(rgdal)
library(ggplot2)
library(jsonlite)
library(RCurl)

# naturalearth world map geojson
URL <- "https://github.com/nvkelso/natural-earth-vector/raw/master/geojson/ne_50m_admin_0_countries.geojson.gz"
fil <- basename(URL)

if (!file.exists(fil)) download.file(URL, fil)
R.utils::gunzip(fil)
world <- readOGR(dsn="ne_50m_admin_0_countries.geojson", layer="OGRGeoJSON")

# remove antarctica
world <- world[!world$iso_a3 %in% c("ATA"),]

world <- spTransform(world, CRS("+proj=wintri"))

dat_url <- getURL("https://gist.githubusercontent.com/hrbrmstr/7a0ddc5c0bb986314af3/raw/6a07913aded24c611a468d951af3ab3488c5b702/pop.csv")
pop <- read.csv(text=dat_url, stringsAsFactors=FALSE, header=TRUE)

map <- fortify(world, region="iso_a3")

# data frame of markers 
labs <- data.frame(lat=c(39.5, 35.50), 
                   lon=c(-98.35, 103.27), 
                   title=c("US", "China"))

# pre-project them to winkel-tripel
coordinates(labs) <-  ~lon+lat
c_labs <- as.data.frame(SpatialPointsDataFrame(spTransform(
  SpatialPoints(labs, CRS("+proj=longlat")), CRS("+proj=wintri")),
  labs@data))

gg <- ggplot()
gg <- gg + geom_map(data=map, map=map,
                    aes(x=long, y=lat, map_id=id, group=group),
                    fill="#ffffff", color=NA)
gg <- gg + geom_map(data=pop, map=map, color="white", size=0.15,
                    aes(fill=log(X2013), group=Country.Code, map_id=Country.Code))
gg <- gg + geom_point(data=c_labs, aes(x=lon, y=lat), size=4)
gg <- gg + scale_fill_gradient(low="#f7fcb9", high="#31a354", name="Population by Country\n(2013, log scale)")
gg <- gg + labs(title="2013 Population")
gg <- gg + coord_equal(ratio=1)
gg <- gg + ggthemes::theme_map()
gg <- gg + theme(legend.position="bottom")
gg <- gg + theme(legend.key = element_blank())
gg <- gg + theme(plot.title=element_text(size=16))
gg
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  • 非常好!这是一个替代网址(我当前网址有问题):https://github.com/nvkelso/natural-earth-vector/blob/master/geojson/ne_50m_admin_0_countries.geojson.gz?raw = true.我双击``.gz``,适当地设置目录,``setwd("〜/ R/maps")``并用``world < - readOGR(dsn ="ne_50m_admin_0_countries.geojson")读取它,layer ="OGRGeoJSON")`` (2认同)