r - 移动并重新缩放阿拉斯加和夏威夷

标签 r ggplot2 rgdal maptools

我正在关注 this移动和重新缩放阿拉斯加和夏威夷的教程。这是我正在运行的代码:

x = c("ggplot2", "rgdal", "maptools", "mapproj", "rgeos")
lapply(x, library, character.only = TRUE)

remove.territories = function(.df) {
  subset(.df, 
         .df$id != "AS" &
           .df$id != "MP" &
           .df$id != "GU" & 
           .df$id != "PR" &
           .df$id != "VI" 
  )
}

plain_theme = theme(axis.text=element_blank()) + 
  theme(panel.background = element_blank(), 
        panel.grid = element_blank(), 
        axis.ticks = element_blank())

no_ylab = ylab("") 
no_xlab = xlab("")

# From https://www.census.gov/geo/maps-data/data/cbf/cbf_state.html
us <- readOGR(dsn = "./cb_2014_us_state_5m.shp",
                  layer = "cb_2014_us_state_5m", verbose = FALSE)

# Transform geographical coordinates to Lambert Azimuth Equal Area projection
us_aea = spTransform(us, CRS("+proj=laea +lat_0=45 +lon_0=-100 +x_0=0 +y_0=0 +a=6370997 +b=6370997 +units=m +no_defs"))
us_aea@data$id = rownames(us_aea@data)

#Move Alaska (scaled down) and Hawaii
alaska = us_aea[us_aea$STATEFP=="02",]
alaska = elide(alaska, rotate=-50)
alaska = elide(alaska, scale=max(apply(bbox(alaska), 1, diff)) / 2.3)
alaska = elide(alaska, shift=c(-2100000, -2500000))
proj4string(alaska) = proj4string(us_aea)

hawaii = us_aea[us_aea$STATEFP=="15",]
hawaii = elide(hawaii, rotate=-35)
hawaii = elide(hawaii, shift=c(5400000, -1400000))
proj4string(hawaii) = proj4string(us_aea)

#Remove Alaska and Hawaii from base map and substitute transformed versions

us50 <- fortify(us_aea, region="STUSPS")
us50 = remove.territories(us50)

#plot 

p = ggplot(data=us50) + 
  geom_map(map=us50, aes(x=long, y=lat, map_id=id, group=group), ,fill="white", color="dark grey", size=0.15) + 
  no_ylab + 
  no_xlab + 
  plain_theme
p

我不确定我错过了什么我的 map 看起来不像教程中的 map :

usa

最佳答案

此代码生成带有 leaflet 的 map :

remove.territories = function(.df) {
  subset(.df, 
         .df$id != "AS" &
           .df$id != "MP" &
           .df$id != "GU" & 
           .df$id != "PR" &
           .df$id != "VI" 
  )
}

x = c("leaflet", "rgdal", "maptools", "mapproj", "rgeos")
lapply(x, library, character.only = TRUE)

# From https://www.census.gov/geo/maps-data/data/cbf/cbf_state.html
us <- readOGR(dsn = "./cb_2014_us_state_5m.shp",
              layer = "cb_2014_us_state_5m", verbose = FALSE)

# convert it to Albers equal area
us_aea <- spTransform(us, CRS("+proj=laea +lat_0=45 +lon_0=-100 +x_0=0 +y_0=0 +a=6370997 +b=6370997 +units=m +no_defs"))
us_aea@data$id <- rownames(us_aea@data)

# extract, then rotate, shrink & move alaska (and reset projection)
# need to use state IDs via # https://www.census.gov/geo/reference/ansi_statetables.html
alaska <- us_aea[us_aea$STATEFP=="02",]
alaska <- elide(alaska, rotate=-50)
alaska <- elide(alaska, scale=max(apply(bbox(alaska), 1, diff)) / 2.3)
alaska <- elide(alaska, shift=c(-2100000, -2500000))
proj4string(alaska) <- proj4string(us_aea)

# extract, then rotate & shift hawaii
hawaii <- us_aea[us_aea$STATEFP=="15",]
hawaii <- elide(hawaii, rotate=-35)
hawaii <- elide(hawaii, shift=c(5400000, -1400000))
proj4string(hawaii) <- proj4string(us_aea)

# remove old states and put new ones back in; note the different order
# we're also removing puerto rico in this example but you can move it
# between texas and florida via similar methods to the ones we just used
us_aea <- us_aea[!us_aea$STATEFP %in% c("02", "15", "72"),]
us_aea <- rbind(us_aea, alaska, hawaii)
# transform data again
us_aea2 <- spTransform(us_aea, proj4string(us))
#Leaflet
map <- leaflet(us_aea2) 
pal <- colorNumeric(
  palette = "YlGnBu",
  domain = us_aea2$ALAND)
map %>% 
  addPolygons(
    stroke = FALSE, smoothFactor = 0.2, fillOpacity = 1,
    color = ~ pal(ALAND)
  ) %>% 
  setView(lng = -98.579394, lat = 37, zoom = 4)

我的猜测是有一种更有效的方法可以做到这一点。

关于r - 移动并重新缩放阿拉斯加和夏威夷,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32341912/

相关文章:

r - R中使用foreach时是否导入 "parallel"包

r - 如何在r中创建多折线图

r - 没有ggplot2图正在工作: "Error in y[setdiff(names(y), names(x))] : object of type ' closure' is not subsettable"

r - 使用readOGR时对德国变音符号的编码

r - 在点图上画圆圈以标记 ggplot2 中的最佳区域

r - 如何为R中的变量设置属性?

r - 访问 X[Y, j] 中 j 中具有重复名称的 Y 列合并

r - 订购并填充 2 个不同的变量 geom_bar ggplot2 R

r - 无法再加载 rgdal

r - 在 rstudio 中安装 rgdal 库 - 在 libgdal 中找不到 GDALAllRegister