【问题标题】:How to colour a single country in R maps package (worldhires)如何在 R 地图包中为单个国家着色(worldhires)
【发布时间】:2015-01-27 23:21:16
【问题描述】:

我是新来的,我正在尝试在沿海地区的地图上绘制点 - 因此我想展示一条海岸线并只为一个国家着色,周围是毗邻的国家。

我的代码是

library(maps)  
library(mapdata)

map("worldHires", xlim=c(-90,-70), ylim=c(-20,-2), # Re-defines the latitude and longitude range
    col = "gray", fill=TRUE)

但是我只想在秘鲁上色。到目前为止,我已经设法做到了:

map('worldHires', 'Peru', col = "grey90", fill=TRUE, xlim=c(-90,-70), ylim=c(-20,-2))

但这并没有显示毗邻的国家,我真的很想显示所有毗邻的国家,只是给秘鲁上色。

我在另一个线程中看到了使用简单地图工具的建议 - 但细节略少(见下文)

library(maptools)

data(wrld_simpl)
plot(wrld_simpl, 
     col = c(gray(.80), "red")[grepl("Peru", wrld_simpl@data$NAME) + 1], 
xlim=c(-90,-70), ylim=c(-20,-2))

有谁知道如何使用 worldhire 来做到这一点?这可能真的很简单,只是我还没有解决。

【问题讨论】:

    标签: r colors maps


    【解决方案1】:

    诀窍是使用map() 提取秘鲁边界,然后在世界地图轮廓上绘制秘鲁填充。

    library(maps)  
    library(mapdata)
    
    peru <- map("worldHires", regions="Peru", plot=FALSE, fill=TRUE)
    
    map("worldHires", xlim=c(-100,-30), ylim=c(-30,10))
    map(peru, col="red", fill=TRUE, add=TRUE)
    

    【讨论】:

      【解决方案2】:

      通过 ggplot 的 Hadleyverse 版本:

      library(maps)  
      library(mapdata)
      library(ggplot2)
      
      coast_map <- fortify(map("worldHires", fill=TRUE, plot=FALSE))
      
      gg <- ggplot()
      gg <- gg + geom_map(data=coast_map, map=coast_map,
                          aes(x=long, y=lat, map_id=region),
                          fill="white", color="black")
      gg <- gg + geom_map(data=data.frame(region="Peru"), map=coast_map,
                          aes(map_id=region), fill="steelblue")
      gg <- gg + xlim(-90,-70) + ylim(-20,-2)
      gg <- gg + coord_map()
      gg <- gg + theme_bw()
      gg
      

      【讨论】:

      • geom_point 会做类似的事情。您可以通过该查询提交一个新问题(修改这个可能是错误的形式),我可以举一个例子。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-17
      • 1970-01-01
      • 2016-03-21
      • 1970-01-01
      • 2012-01-17
      • 2021-06-09
      • 1970-01-01
      相关资源
      最近更新 更多