Adding Provinces to Canadian Map in R

≡放荡痞女 提交于 2021-02-20 02:46:43

问题


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?

Thanks


回答1:


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)

enter image description here

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)

enter image description here



来源:https://stackoverflow.com/questions/29421436/adding-provinces-to-canadian-map-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!