簡體   English   中英

帶有 ABS 人口普查數據的 R googleway

[英]R googleway with ABS census data

我打算使用googleway分析澳大利亞人口普查數據以生成熱圖。

我的方法是使用來自 googleway melbourne准備好的數據,其中包含列SA2_NAME並在使用rgdal (下面的代碼)轉換后將其與來自人口普查數據的 ESRI 形狀文件連接起來。 問題在於,通過SA2_NAME連接對於多段線並不是唯一的 - 一些 SA2 區域由多個“子”區域組成。 所以這似乎不是一個好方法。

更好的方法是將下面的 ESRI 形狀數據sa2_shape轉換為具有melbourne數據格式的折線。 這是怎么做的?

下面的代碼生成一個“橋接”數據框,用於將來自googleway melbourne數據與以SA2_MAIN作為關鍵字段的 ABS 數據連接SA2_MAIN - 如上所述,這種“hack”方法的問題在於,折線不是SA2_NAME唯一的

library(tidyverse)
library(googleway)
library(rgdal)

shape_path <- "abs_data/sa2_esri_shapefile"
shape_file <- "SA2_2016_AUST"
sa2_shape <- readOGR(shape_path, shape_file)
sa2_df <- data.frame(sa2_shape$SA2_MAIN, sa2_shape$SA2_NAME)
names(sa2_df) <- c("SA2_MAIN", "SA2_NAME")
sa2_df <- sa2_df %>% semi_join(melbourne, by = "SA2_NAME")

根據 SymbolixAU 注釋 - 使用sf加載數據,只要geometry不是空列表就可以工作 - 請參閱下面的代碼。

library(tidyverse)
library(googleway)
library(sf)

shape_path <- "abs_data/sa2_esri_shapefile"
shape_file <- "SA2_2016_AUST"

shape_file_path <- paste0(shape_path, "/", shape_file, '.shp')
sa2_shape <- sf::st_read(shape_file_path)
sa2_shape <- sa2_shape %>% 
  filter(STATE_NAME == "Victoria",
         AREA_SQKM > 0)# This is important - otherwise google_map() will crash!
google_map() %>% 
  googleway::add_polygons(data = sa2_shape, 
                          polyline = "geometry", 
                          fill_colour = "SA2_NAME")

維多利亞SA2地圖

> sa2_shape %>% head()
Simple feature collection with 6 features and 6 fields
geometry type:  MULTIPOLYGON
dimension:      XY
bbox:           xmin: 143.6849 ymin: -37.68153 xmax: 143.951 ymax: -37.46847
epsg (SRID):    4283
proj4string:    +proj=longlat +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +no_defs
   SA2_MAIN SA2_MAIN16         SA2_NAME STATE_CODE STATE_NAME AREA_SQKM                       geometry
1 201011001  201011001        Alfredton          2   Victoria   52.7111 MULTIPOLYGON (((143.7072 -3...
2 201011002  201011002         Ballarat          2   Victoria   12.3787 MULTIPOLYGON (((143.8675 -3...
3 201011003  201011003 Ballarat - North          2   Victoria   92.3577 MULTIPOLYGON (((143.853 -37...
4 201011004  201011004 Ballarat - South          2   Victoria   32.8541 MULTIPOLYGON (((143.8675 -3...
5 201011005  201011005        Buninyong          2   Victoria   51.5855 MULTIPOLYGON (((143.8533 -3...
6 201011006  201011006        Delacombe          2   Victoria   34.1608 MULTIPOLYGON (((143.7072 -3...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM