在R中的加拿大地图中添加省 [英] Adding Provinces to Canadian Map in R

查看:95
本文介绍了在R中的加拿大地图中添加省的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经浏览了很多有关R中映射的主题,希望能有所帮助.

I've been browsing a lot of the topics on mapping in R and would appreciate a little help.

我已将此代码用于构建购买密度的图像,然后将美国国家地图和加拿大国家地图叠加在上面.

I've made it to this code which builds an image of a purchase density then overlays a US State map on top and a Canadian national map as well.

这是一个好的解决方案,但理想情况下,我还要展示加拿大的省份.

It's an ok solution, but Ideally I'd like to show the provinces in Canada as well.

library(mapdata);
library(maps);
library(maptools);
library(spatstat);

png(filename=file_name, type="cairo-png", bg="transparent", width=10.*960, height=10.*960, pointsize=1);
spatstat.options(npixel=c(1000,1000));
densitymap<-density(points, sigma=0.15, weights=dedupedMergedZips[!is.na(dedupedMergedZips$longitude), zipCount]);

my.palette <- colorRampPalette(c("#3F3F3F","#e2ffcc","#b6ff7f","white"), bias=2, space="rgb")

image(densitymap, col=my.palette(200));
map("state", col="grey", fill=FALSE, bg="transparent", lwd=3.0, xlim=longitudeLimits, ylim=latitudeLimits, add = TRUE);
map("worldHires","Canada", xlim=longitudeLimits, ylim=latitudeLimits, col="grey", fill=FALSE, bg="transparent", lwd=3.0, add=TRUE)

dev.off()

关于我如何在第二行中添加额外的论据以显示省份的任何提示?

Any tips on how I could add an additional Arguement to the second line to get the provinces to show?

谢谢

推荐答案

以下是基于传单的解决方案:

Here is a solution, based on leaflet:

library(rgdal)

if (!file.exists("./src/ref/ne_50m_admin_1_states_provinces_lakes/ne_50m_admin_1_states_provinces_lakes.dbf")){
  download.file(file.path('http://www.naturalearthdata.com/http/',
                          'www.naturalearthdata.com/download/50m/cultural',
                          'ne_50m_admin_1_states_provinces_lakes.zip'), 
                f <- tempfile())
  unzip(f, exdir = "./src/ref/ne_50m_admin_1_states_provinces_lakes")
  rm(f)
}

region <- readOGR("./src/ref/ne_50m_admin_1_states_provinces_lakes", 'ne_50m_admin_1_states_provinces_lakes', encoding='UTF-8')

library(leaflet)

leaflet() %>% 
  addTiles() %>% 
  setView(-74.09, 45.7,  zoom = 3) %>% 
  addPolygons(data = subset(region, name %in% c("British Columbia", "Alberta", "Saskatchewan", "Manitoba", "Ontario", "Quebec", "New Brunswick", "Prince Edward Island", "Nova Scotia", "Newfoundland and Labrador", "Yukon", "Northwest Territories", "Nunavut")), 
              fillColor = topo.colors(10, alpha = NULL),
              weight = 1)

这是另一个利用 ggplot2 的建议:

Here is another proposal leveraging ggplot2:

library(ggplot2)

regions <- subset(region, name %in% c("British Columbia", "Alberta", "Saskatchewan", "Manitoba", "Ontario", "Quebec", "New Brunswick", "Prince Edward Island", "Nova Scotia", "Newfoundland and Labrador", "Yukon", "Northwest Territories", "Nunavut")) # region is defined in the first part of the code (see above)

ggplot(regions) + 
  aes(long,lat, group = group, fill = group) + 
  geom_polygon() +
  geom_path(color="white") +
  coord_equal() + 
  guides(fill = FALSE)

这篇关于在R中的加拿大地图中添加省的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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