【问题标题】:Color countries on world map based on ISO3 codes in R using ggplot()使用 ggplot() 根据 R 中的 ISO3 代码为世界地图上的国家着色
【发布时间】:2014-12-07 01:15:54
【问题描述】:

我想知道我是否可以使用ggplot 为国家/地区着色?使用rworldmap 这非常简单,并且通过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("")

【问题讨论】:

  • 当然可以,您只需要在数据集中填写一列并设置aes(fill = that_column)。请记住,地理数据绘制的顺序很重要,因此更喜欢plyr::joindplyr::left_join 而不是merge,因为它们保留了第一个数据框的顺序。
  • 感谢您的评论。 “that_column”列可以由 ISO3 国家代码组成,相应的国家会被着色吗?如何定义渐变,以便着色反映一些数字(基本上是每个要着色的国家的计数)?
  • 从答案中可能一切都清楚了,但为了使这条评论链更完整:不,that_column 将是实际数据,如人口、GDP,无论您测量什么。

标签: r ggplot2 gis world-map


【解决方案1】:

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

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

【讨论】:

  • 能否请您展示如何根据纬度/经度添加标记或点?
  • 我得到以下错误:isTRUE(gpclibPermitStatus()) is not TRUE
  • 我已经更新了注释的例子,但是如果你想添加更多,你应该发布另一个问题。
  • 非常好!这是一个替代网址(我对当前网址有疑问):github.com/nvkelso/natural-earth-vector/blob/master/geojson/…。我双击.gz,适当设置目录,setwd("~/R/maps") 并用world &lt;- readOGR(dsn = "ne_50m_admin_0_countries.geojson", layer = "OGRGeoJSON")阅读它
  • @JerryN 只需使用ggthemes::theme_map()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-13
  • 2020-03-10
  • 1970-01-01
  • 2012-04-07
  • 1970-01-01
相关资源
最近更新 更多