1

我正在尝试使用 tmap 和简单的功能制作一张与美国其他州分开的地图。我轻推坐标所需的数量(在下面的示例中为东部地区) - 但出于某种原因,佛罗里达州和纽约州拒绝移动。我尝试在循环外手动调整,结果相同。如果我想将 sf 对象移动一个常数,那么接近它的最佳方法是什么?

还有——为什么密歇根看起来像融化在密歇根湖里?

library(tmap)
library(tmaptools)
library(tigris)

us_geo <- states(class = "sf")

us_geo$PADD[us_geo$NAME %in% 
              c("Maine", "Vermont", "New Hampshire", "Massachusetts", "Connecticut", "Rhode Island",
                "New York", "Pennsylvania", "New Jersey", "Delaware", "District of Columbia", "Maryland",
                "West Virginia", "Virginia", "North Carolina", "South Carolina", "Georgia", "Florida")] <- "East Coast"

us_geo$PADD[us_geo$NAME %in% 
              c("South Dakota", "North Dakota","Nebraska", "Kansas", "Oklahoma", 
                "Minnesota", "Iowa", "Missouri", "Wisconsin", "Illinois", "Indiana",
                "Michigan", "Ohio", "Kentucky", "Tennessee")] <- "Midwest"

us_geo$PADD[us_geo$NAME %in% 
              c("New Mexico", "Texas", "Arkansas", "Louisiana", "Alabama", "Mississippi")] <- "Gulf Coast"

us_geo$PADD[us_geo$NAME %in% 
              c("Montana", "Idaho", "Wyoming", "Utah", "Colorado")] <- "Rocky Mountain"

us_geo$PADD[us_geo$NAME %in% 
              c("Washington", "Oregon", "Nevada", "Arizona", "California")] <- "West Coast"

us_geo <- us_geo[!is.na(us_geo$PADD),]



x <- grep("East", us_geo$PADD)

for(i in x){
  us_geo[i,]$geometry[[1]][[1]][[1]][,1] <- us_geo[i,]$geometry[[1]][[1]][[1]][,1] +5
}

qtm(us_geo)

在此处输入图像描述

4

1 回答 1

0

我实际上并没有移动 MULTIPOLYGON 中的所有对象。我猜前面有multi-的词让我很困惑。只需要添加另一个循环。另外,我改变了决议,让密歇根重新出现。

library(tmap)
library(tmaptools)
library(tigris)

us_geo <- states(class = "sf", res = "5m", cb = TRUE)

us_geo$PADD[us_geo$NAME %in% 
              c("Maine", "Vermont", "New Hampshire", "Massachusetts", "Connecticut", "Rhode Island",
                "New York", "Pennsylvania", "New Jersey", "Delaware", "District of Columbia", "Maryland",
                "West Virginia", "Virginia", "North Carolina", "South Carolina", "Georgia", "Florida")] <- "East Coast"

us_geo$PADD[us_geo$NAME %in% 
              c("South Dakota", "North Dakota","Nebraska", "Kansas", "Oklahoma", 
                "Minnesota", "Iowa", "Missouri", "Wisconsin", "Illinois", "Indiana",
                "Michigan", "Ohio", "Kentucky", "Tennessee")] <- "Midwest"

us_geo$PADD[us_geo$NAME %in% 
              c("New Mexico", "Texas", "Arkansas", "Louisiana", "Alabama", "Mississippi")] <- "Gulf Coast"

us_geo$PADD[us_geo$NAME %in% 
              c("Montana", "Idaho", "Wyoming", "Utah", "Colorado")] <- "Rocky Mountain"

us_geo$PADD[us_geo$NAME %in% 
              c("Washington", "Oregon", "Nevada", "Arizona", "California")] <- "West Coast"

us_geo <- us_geo[!is.na(us_geo$PADD),]

qtm(us_geo)

x <- grep("East", us_geo$PADD)

for(i in x){
  for(j in 1:length(us_geo[i,]$geometry[[1]])){
  us_geo[i,]$geometry[[1]][[j]][[1]][,1] <- us_geo[i,]$geometry[[1]][[j]][[1]][,1] +5
  }
}

qtm(us_geo)

在此处输入图像描述

于 2018-02-22T23:22:37.397 回答