我已经使用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绘图,ggplot的数据的差距,ggmap或sp可以理解和绘制。
我很乐意从一个基本的热图开始,其中六角形的填充美感被缩放为n
。
在此先感谢您的帮助!
答案 0 :(得分: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(v0.2.1)于2019-03-23创建