当给定'polygon'数据类型的坐标时,如何在R中绘制纬度/经度六边形?

问题描述 投票:0回答:1

我用City of Austin's scooter dataset做了一些基本的数据分析。在这个数据集中,每辆踏板车都会被给予一个id of the geographical hexagon,其中骑行开始或结束。

我按六边形分组并总结了游乐设施的数量并制作了一个csv,你可以得到这样的:

austin_hexagon_SO <- read_csv("http://www.sharecsv.com/dl/229ad18b34ffb021189a821a3bcbd5a8/austin_hexagon_SO.csv")

glimpse(austin_hexagon_SO)

# Observations: 2,482
# Variables: 3
# $ orig_cell_id <dbl> 15186, 14864, 14706, 14707, 15019, 14714, 1502…
# $ n            <dbl> 10765, 8756, 8538, 8338, 8291, 8049, 7988, 778…
# $ polygon      <chr> "POLYGON ((-97.735143 30.283413000000003, -97.…

现在,我已经研究了一堆不同的软件包,特别是library(sp),但是我无法弥补差距,不能采用看起来像这样的数据框并将其转换为基于R plot,ggplot,ggmap或sp可以理解的东西和情节。

我很想从一个基本的热图开始,其中六边形的填充美学缩放到n

在此先感谢您的帮助!

r ggplot2 leaflet ggmap sp
1个回答
0
投票

读取数据框后,多边形仍然只是字符串(即类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)])

reprex package创建于2019-03-23(v0.2.1)

© www.soinside.com 2019 - 2024. All rights reserved.