在R中添加加拿大地图的省份

时间:2015-04-02 19:59:22

标签: r gis

我一直在浏览R中有很多关于映射的主题,并希望得到一点帮助。

我已经使用此代码构建购买密度的图像,然后将美国州地图覆盖在顶部以及加拿大国家地图上。

这是一个很好的解决方案,但理想情况下,我也希望向加拿大各省展示。

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()

有关如何在第二行添加额外议案以获得各省展示的任何提示?

由于

1 个答案:

答案 0 :(得分:2)

这是一个解决方案,基于传单

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)

enter image description here

这是另一个利用 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)

enter image description here