簡體   English   中英

用R中的sf將美學映射到LINESTRING幾何

[英]Map aesthetics to LINESTRING geometries with sf in R

新十歲上下sf包裝的R使得它很容易處理R中的地理數據,以及的發展探析版本ggplot2有一個新的geom_sf()層密謀SF風格的地理數據。

在使用數據的sf范例內,是否可以將ggplot美學映射到LINESTRING幾何?

例如,使用標准的ggplot,可以在1812年使用ggplot和這些數據重建Minard着名的拿破侖格蘭德軍隊幸存者的情節,並根據幸存者的數量確定軍隊的路徑:

# Install the dev version of ggplot2 for geom_sf()
# devtools::install_github("tidyverse/ggplot2")
library(tidyverse)

troops <- read_csv("https://gist.githubusercontent.com/andrewheiss/69b9dffb7cca392eb7f9bdf56789140f/raw/3e2a48635ae44837955765b5e7747c429b0b5d71/troops.csv")

ggplot(troops) +
  geom_path(aes(x = long, y = lat, color = direction, 
                group = group, size = survivors),
            lineend = "round")

我們可以通過創建一個新的geometry列來將此部隊數據作為sf對象使用,如下所示:

library(sf)
#> Linking to GEOS 3.6.1, GDAL 2.1.3, proj.4 4.9.3

troops_with_geometry <- troops %>%
  st_as_sf(coords = c("long", "lat"))

head(troops_with_geometry)
#> Simple feature collection with 6 features and 3 fields
#> geometry type:  POINT
#> dimension:      XY
#> bbox:           xmin: 24 ymin: 54.5 xmax: 28 ymax: 55
#> epsg (SRID):    NA
#> proj4string:    NA
#> # A tibble: 6 x 4
#>   survivors direction group          geometry
#>       <int>     <chr> <int>  <simple_feature>
#> 1    340000         A     1 <POINT (24 54.9)>
#> 2    340000         A     1 <POINT (24.5 55)>
#> 3    340000         A     1 <POINT (25.5 ...>
#> 4    320000         A     1 <POINT (26 54.7)>
#> 5    300000         A     1 <POINT (27 54.8)>
#> 6    280000         A     1 <POINT (28 54.9)>

如果我們用geom_sf繪制它,ggplot將使用點:

ggplot(troops_with_geometry) +
  geom_sf(aes(color = direction, group = group))

我們可以通過分組,匯總和轉換為每個組和方向創建線串。

troops_lines <- troops_with_geometry %>%
  group_by(direction, group) %>% 
  summarize() %>% 
  st_cast("LINESTRING")

head(troops_lines)
#> Simple feature collection with 6 features and 2 fields
#> geometry type:  LINESTRING
#> dimension:      XY
#> bbox:           xmin: 24 ymin: 54.1 xmax: 37.7 ymax: 55.8
#> epsg (SRID):    NA
#> proj4string:    NA
#>   direction group                       geometry
#> 1         A     1 LINESTRING (24 54.9, 24.5 5...
#> 2         A     2 LINESTRING (24 55.1, 24.5 5...
#> 3         A     3 LINESTRING (24 55.2, 24.5 5...
#> 4         R     1 LINESTRING (24.1 54.4, 24.2...
#> 5         R     2 LINESTRING (28.3 54.2, 28.5...
#> 6         R     3 LINESTRING (24.1 54.4, 24.2...

ggplot然后可以繪制這六條連接線並正確地着色它們:

ggplot(troops_lines) +
  geom_sf(aes(color = direction, group = group))

然而,幸存者的數據現在已經消失,並且沒有辦法將尺寸美學映射到新的線條。

有沒有辦法將其他美學(如大小)與基於sfLINESTRING數據相關聯? 或者,換句話說,有沒有辦法使用geom_sf()和使用地理數據的sf范例重新創建ggplot(...) + geom_path(aes(x = long, y = lat, size = something))

在此輸入圖像描述 您需要在每個組中的每對點創建一個線串。 結果不是那么漂亮,因為我不知道如何給出圓形端點。

# within each group repeat each point 
# then slice the first and last out and 
# add a variable called linegroup, which provides grouping for start and endpoints of each line
troops %<>% group_by(group) %>%
  slice(rep(1:n(), each = 2)) %>%
  slice(-c(1, n())) %>%
  mutate(linegroup = lapply(1:(n()/2), function(x) rep(x, 2)) %>% unlist) %>% 
  ungroup

# create linestring sf object by summarizing the points, 
# grab the last survivor and direction value of each group (i.e. the 'endpoint' value)
troops_line <- st_as_sf(troops, coords = c("long", "lat"), crs = 4326) %>%
  group_by(group, linegroup) %>%
  summarise(survivors = last(survivors), direction = last(direction), do_union = FALSE) %>%
  st_cast("LINESTRING")

gp <- ggplot(troops_line) +
  geom_sf(aes(color = direction, size = survivors), show.legend = "line") 

暫無
暫無

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

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