Saat ini, saya menggunakan Shapefile Distrik Kongres Amerika Serikat. Saya ingin membuat Kartogram menggunakan langkah-langkah yang tercantum dalam kartogram paket. Tapi sepertinya saya tidak bisa membuatnya menjadi objek kartogram menggunakan fungsi cartogram_cont dengan sukses.

Bantuan, saran, atau wawasan apa pun yang dapat Anda tawarkan untuk membuat saya melewati titik ini dan lebih dekat dengan kartogram akan sangat membantu.

Silakan bagian bawah kode untuk di mana kesalahan terjadi.

Terima kasih!

######################### Library #####################
library(sf)
library(tmap)
library(cartogram)
library(ggplot2)
library(ggmap)
library(maptools)


###################### Get Congress Map ###################
get_congress_map <- function(cong=113) {
  tmp_file <- tempfile()
  tmp_dir  <- tempdir()
  zp <- sprintf("http://cdmaps.polisci.ucla.edu/shp/districts%03i.zip",cong)
  download.file(zp, tmp_file)
  unzip(zipfile = tmp_file, exdir = tmp_dir)
  fpath <- paste(tmp_dir, sprintf("districtShapes/districts%03i.shp",cong), sep = "/")
  st_read(fpath)
}


cd114 <- get_congress_map(114)

################ Create Congressional District ID ###################

State_Dictonary <- rbind(data.frame(state.abb, STATENAME= state.name), data.frame(state.abb= "DC", STATENAME= "District Of Columbia"))

#Add DC
cd114.1 <- merge(State_Dictonary, cd114, by = c("STATENAME"))
unique(cd114$STATENAME)
#**************** Paste Zero infront of Single Districts ****************
cd114.1$district_code <- as.character(cd114.1$DISTRICT)
cd114.1$district_code[cd114.1$district_code == 98] <- "01" #Account for the weird code for DC
cd114.1$district_code[cd114.1$district_code == 0] <- "01"  #Account for at large districts
cd114.1$district_code[cd114.1$district_code == 1] <- "01"
cd114.1$district_code[cd114.1$district_code == 2] <- "02"
cd114.1$district_code[cd114.1$district_code == 3] <- "03"
cd114.1$district_code[cd114.1$district_code == 4] <- "04"
cd114.1$district_code[cd114.1$district_code == 5] <- "05"
cd114.1$district_code[cd114.1$district_code == 6] <- "06"
cd114.1$district_code[cd114.1$district_code == 7] <- "07"
cd114.1$district_code[cd114.1$district_code == 8] <- "08"
cd114.1$district_code[cd114.1$district_code == 9] <- "09"

#************** Create CD in  Data ****************
cd114.1$CD <- paste(cd114.1$state.abb, cd114.1$district_code, sep = "")


################ Create Fake Data TO Simulate My Own ########################
CD <- unique(cd114.1$CD)
Values <- sample(1:100, 436, replace = T)

Df <- data.frame(CD, Values)


################### Merge Shape File to Donor #####################
#******************** Merge Descriptive Stats *********************
Df.2 <- merge(x = Df, y = cd114.1, by = c("CD"), all.y = T)

################### Make SF Object ####################
SF.DF.1 <- st_as_sf(Df.2)
SF.DF.2 <- st_make_valid(SF.DF.1)
class(SF.DF.2)

################### Make cartogram ####################
#Follow Proceedure Laid out: https://cran.r-project.org/web/packages/cartogram/readme/README.html

# I use st_transform instead of spTransform beause I'm using an SF Ojbect
SF.DF.3 <- st_transform(SF.DF.1, CRS("+init=epsg:3395")) 

cartogram_cont(SF.DF.3, "Value", itermax = 5)
#Error in Fij[distance <= radius[j]] <- Fbij[distance <= radius[j]] : 
#NAs are not allowed in subscripted assignments




0
Sharif Amlani 27 Mei 2021, 06:38

1 menjawab

Jawaban Terbaik

Entri geometri di baris 87 kosong (MULTIPOLYGON EMPTY). Anda dapat mengatasi masalah ini dengan:

library(dplyr)

SF.DF.3 <- st_transform(SF.DF.1, CRS("+init=epsg:3395")) %>% 
  filter(!st_is_empty(.))

Selain itu, tidak ada kolom Value yang disertakan dalam df Anda, melainkan Values ;)

sf_ncont <- cartogram_ncont(SF.DF.3, "Values")

tm_shape(sf_ncont) + tm_polygons("Values", style = "jenks", legend.show = FALSE) +
  tm_layout(frame = FALSE)

Catatan, saya menyaring Alaska dan Hawaii untuk plot berikut, serta menggunakan cartogram_ncont alih-alih cartogram_cont karena operasi sebelumnya sangat memakan waktu.

enter image description here

2
mgrund 27 Mei 2021, 08:25