使用[R]地图包-在世界地图上的特定国家/地区着色 [英] Using [R] maps package - colouring in specific nations on a world map
问题描述
我正在尝试创建世界地图并为某些国家/地区着色.基本上,我想用红色突出显示一些国家,而用蓝色突出显示其他国家.
I'm trying to create a world map and color certain nations. Basically, I would like to highlight some countries in red and other countries in blue.
如果有人可以帮助我为此生成基本的[R]代码,我将非常感谢!
If someone could help me generate the basic [R] code for this, I would be very thankful!!
推荐答案
如果您不习惯使用maps
包,则maptools
包中的对象wrld_simpl
可以使生成此类地图变得非常容易.为了让您入门,这里有几行代码生成一张世界地图,在世界地图中,名称以字母"U"开头的国家/地区用红色标记:
If you are not hooked on using the maps
package, the object wrld_simpl
in the maptools
package can make producing this sort of map pretty easy. Here, to get you started, are a few lines of code that produce a world map in which nations whose names start with the letter "U" are colored in red:
library(maptools)
data(wrld_simpl)
plot(wrld_simpl,
col = c(gray(.80), "red")[grepl("^U", wrld_simpl@data$NAME) + 1])
(wrld_simpl
是SpatialPolygonsDataFrame
类的对象,wrld_simple@data
中包含的data.frame包括一个NAME
列,可用于突出显示所选的任何国家.)
(wrld_simpl
is an object of class SpatialPolygonsDataFrame
, and the data.frame contained in wrld_simple@data
includes a NAME
column that you can use to highlight whichever countries you choose.)
这篇关于使用[R]地图包-在世界地图上的特定国家/地区着色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!