简体   繁体   中英

How to colour a single country in R maps package (worldhires)

I am new here and I am trying to plot points on a map of a coastal region - therefore I would like to show a coastline and colour just one country, surrounded by adjoining countries.

My code is

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)

However I would like to colour in just Peru. I have so far managed to do this:

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

but this doesn't show adjoining countries, and I would really like to show all adjoining countries and just colour Peru.

I have seen advice in another thread using the simple map tool - but there is slightly less detail (see below)

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))

Does anyone know how to do it using worldhires? It's probably really simple and I just haven't worked it out.

The trick is to use map() to extract the Peru boundaries, then overplotting the Peru fill on a worlmap outline.

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)

在此处输入图片说明

A Hadleyverse version via ggplot:

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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM