ggplot中的世界分区域

时间:2016-08-06 18:14:45

标签: r ggplot2 maps

我试图在世界地图上绘制数据,其国家可能会或可能不会被分区域分割。我将用一个玩具示例来说明我的意思。除加拿大(一个地区)和纽约(美国地区的一个次区域)外,所有国家的值均为0,其值为1.美国其他地区将为0。

我加载世界数据图并创建所有区域和子区域的矢量。

world <- map_data("world")
country <- c(unique(world$region), unique(world$subregion))

然后我将制作我的数据:除了加拿大和纽约之外,其他地方的值都是0。然后我将国家矢量和统计数据向量合并到ggplot的数据框中。

stats = numeric(length(country)) # set all data to 0
set1 <- c("Canada", "New York")
stats[is.element(country, set1)] <- 1
mydf = data.frame(country = country, stats = stats)

当我对此进行绘制时,加拿大为1,但美国(包括纽约)的全部为0。

ggplot(mydf, aes(map_id = country)) +
    geom_map(aes(fill = stats), map = world) +
    expand_limits(x = world$long, y = world$lat)

从我的数据框中删除USA USA只是将其从地图中删除。使美国地区的统计数据NA变为灰色。我曾经玩过只包含世界地图的子区域,但似乎无法弄清楚如何包含没有子区域的区域。

任何想法都将不胜感激。谢谢!

0 个答案:

没有答案