После того, как вы прочитали фрейм данных, полигоны по-прежнему будут просто строками (то есть класса character
). R еще не знает, что эти строки имеют очень специальный формат, известный как WKT
.
library("readr")
library("tidyverse")
austin_hexagon <- read_csv(
"http://www.sharecsv.com/dl/229ad18b34ffb021189a821a3bcbd5a8/austin_hexagon_SO.csv",
# 10 polygons are sufficient for an example
n_max = 10)
#> Parsed with column specification:
#> cols(
#> orig_cell_id = col_double(),
#> n = col_double(),
#> polygon = col_character()
#> )
glimpse(austin_hexagon)
#> Observations: 10
#> Variables: 3
#> $ orig_cell_id <dbl> 15186, 14864, 14706, 14707, 15019, 14714, 15029, ...
#> $ n <dbl> 10765, 8756, 8538, 8338, 8291, 8049, 7988, 7787, ...
#> $ polygon <chr> "POLYGON ((-97.735143 30.283413000000003, -97.735...
# This package contains a function that can handle the conversion from
# WKT polygons to a SpatialPolygons data frame
library("rangeMapper")
#> Warning: package 'rangeMapper' was built under R version 3.5.3
#> Loading required package: RSQLite
#> This is rangeMapper 0.3-4
X <- WKT2SpatialPolygonsDataFrame(austin_hexagon, "polygon", "orig_cell_id")
class(X)
#> [1] "SpatialPolygonsDataFrame"
#> attr(,"package")
#> [1] "sp"
plot(X)
library("sp")
# If you want to color by n, first add n to the SpatialPolygons DF
X <- merge(X, austin_hexagon, by = "orig_cell_id")
# There a number of ways to plot spatial polygons; let's use base graphics
# Create a color palette
maxColorValue <- 255
palette <- colorRampPalette(c("white", "red"))(maxColorValue)
plot(X, col = palette[cut(X$n, maxColorValue)])
Создано в 2019-03-23 пакетом представ. (v0.2.1)