我正在制作一张世界地图,并希望突出某些具体国家。这是我的尝试:
require(mapdata)
cc <- map('world', names = TRUE, plot = FALSE)
take <- unlist(sapply(c("uk", "usa", "switzerland","new zealand",
"israel","denmark","sweden","italy",'canada'),
grep, tolower(cc), value = TRUE))
map()
map('world', regions=take, fill=TRUE, col='red', add = TRUE)
nums <- c(12,11,1,2,1,1,1,1,1)
我有两个问题。
1)是否可以在此地图上包含与变量“nums”对应的点。 'nums'中的每个数字指的是从特定国家/地区获取的测量数量,12指的是英国等等...是否可以在地图上包含一个点,该点的半径对应于'nums'中的数字。我知道ggplot2可以实现这一点,但我不确定如何使用mapdata?
2)我怎样才能使这张地图看起来更漂亮,即这里似乎有很多空的国家,是否有一种优雅的方式只包括有色国家?
答案 0 :(得分:2)
这是这样的吗?
require(mapdata)
cc <- map('world', names = TRUE, plot = FALSE)
take <- unlist(sapply(countries <- c("uk", "usa", "switzerland","new zealand",
"israel","denmark","sweden","italy",'canada'),
grep, tolower(cc), value = TRUE))
nums <- c(12,11,1,2,1,1,1,1,1)
# gc <- ggmap::geocode(countries) # a googlemaps query gives this:
gc <- structure(list(lon =
c(-3.435973, -95.712891, 8.227512, 174.885971,
34.851612, 9.501785, 18.643501, 12.56738, -106.346771), lat = c(55.378051,
37.09024, 46.818188, -40.900557, 31.046051, 56.26392, 60.128161,
41.87194, 56.130366)),
.Names = c("lon", "lat"),
class = "data.frame",
row.names = c(NA, -9L))
map(xlim=c(-150, 40), ylim=c(25, 75)) # plot only a part of the world map
map('world', regions=take, fill=TRUE, col='red', add = TRUE, )
with(gc, points(lon, lat, cex=nums, pch=19, col=rgb(0, 1, 0, .8))) # add circles