如何在一个国家/地区中绘制区域,每个区域都用一些相应的值阴影 [英] How to plot regions in a country, each shaded by some corresponding value

查看:92
本文介绍了如何在一个国家/地区中绘制区域,每个区域都用一些相应的值阴影的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尤其是在印度,我有一组不同的区域,每个区域都有市场份额"值.

In particular I have a set of distinct regions in India, each with a "marketshare" value.

regions <- data.frame(Region=c("TELANGANA", "TAMIL NADU", "JAMMU & KASHMIR"), Marketshare=c(.25, .30, .15))
regions
           Region Marketshare
1       TELANGANA        0.25
2      TAMIL NADU        0.30
3 JAMMU & KASHMIR        0.15

我可以轻松地使用ggmap real绘制印度

I can plot India using ggmap real easily using

library(ggmap)
map <- get_map("India", zoom=6, maptype="toner-lite")
ggmap(map)

但是如何才能在数据集中专门包含区域并使用 Marketshare 列为每个区域着色呢?

But how can I specifically include the regions in my dataset and shade each of them using the column Marketshare?

注意:我看过此问题和答案,但对于我的需求而言似乎过于复杂,并且想知道是否有任何更简单的方法.

Note: I've seen this question and answer, but it seems overly complex for my needs and was wondering if there's any easier way.

推荐答案

如hrbrmstr所述,我认为如果您能做出自己的努力,那就太好了.在这里,我创建了两个地图.第一个是ggmap,另一个是ggplot.在示例代码中,您的地图缩放处于关闭状态,以便显示您指定的所有三个区域.所以我修改了您的代码.在地图顶部需要的是所需区域的多边形.为了获得它们,您可以使用get_data()下载数据.然后,您需要对其进行子集化.如链接的问题所示,您需要经过合并过程才能将marketshare添加到最终数据帧.我收集了每个区域的ID,并使用marketshare创建了一个数据框,该数据框为foo. temp是最终数据帧,其中包含多边形信息以及marketshare.您将marketshare用于fill并绘制地图.

As hrbrmstr mentioned, I think it would be great if you can show some efforts you made. Here, I created two maps. The first one is with ggmap, and the other with ggplot. In your example code, your map zoom was off in order to show all three regions you specified. So I revised your code. What you need on top of the map is polygons for the regions you want. In order to get them, you download data using get_data(). Then, you need to subset it. As the linked question showed, you need to go through a merge process in order to add marketshare to a final data frame. I collected id for each regions and created a data frame with marketshare, which is foo. temp is the final data frame which contains polygon information as well as marketshare. You use marketshare for fill and draw a map.

library(raster)
library(ggmap)
library(ggplot2)

### Get India data
india <- getData("GADM", country = "India", level = 2)

map <- get_map("India", zoom = 4, maptype = "toner-lite")
regions <- data.frame(Region=c("Telangana", "Tamil Nadu", "Jammu and Kashmir"), Marketshare=c(.25, .30, .15))

states <- subset(india, NAME_1 %in% regions$Region)

ind1 <- states$ID_2[states$NAME_1 == "Telangana"]
ind2 <- states$ID_2[states$NAME_1 == "Tamil Nadu"]
ind3 <- states$ID_2[states$NAME_1 == "Jammu and Kashmir"]

states <- fortify(states)

foo <- data.frame(id = c(ind1, ind2, ind3),
                  marketshare = rep(regions$Marketshare, times = c(length(ind1), length(ind2), length(ind3))))

temp <- merge(states, foo, by = "id")

ggmap(map) +
geom_map(data = temp, map = temp,
         aes(x = long, y = lat, map_id = id, group = group, fill = marketshare),
         colour = "grey50", size = 0.3) +
theme(legend.position = "none")

或者,您可以使用ggplot绘制地图,以便仅显示印度.在这种情况下,您将根据下载数据india创建一个数据框.绘制一次印度,然后在地图顶部绘制特定区域.我在这里出于颜色原因将marketshare转换为因数.

Alternatively, you can draw a map with ggplot so that you just show India only. In this case, you create a data frame from the download data, india. You draw India once, then you draw specific regions on top of the map. I converted marketshare to factor for colour reasons here.

india.map <- fortify(india)

ggplot() +
geom_map(data = india.map, map = india.map,
         aes(x = long, y = lat, map_id = id, group = group),
         colour = "grey50", size = 0.3) +
geom_map(data = temp, map = temp,
         aes(x = long, y = lat, map_id = id, group = group, fill = factor(marketshare)),
         colour = "grey50", size = 0.3) +
theme(legend.position = "none")

这篇关于如何在一个国家/地区中绘制区域,每个区域都用一些相应的值阴影的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆