[英]Cannot get alpha to work with ggplot2::geom_sf on sf linestring object
[英]ggplot not plotting linestring points from sf object
給定以下數據:
library(tidyverse)
library(sf)
df <- structure(list(geometry = c("LINESTRING (-85.76 38.34, -85.72 38.38)",
"LINESTRING (-85.46 38.76, -85.42 38.76)",
"LINESTRING (-85.89 38.31, -85.89 38.31)"
), var1 = c(4, 5, 6
), var2 = c(1, 2, 3
)), row.names = c(NA, -3L), class = c("tbl_df", "tbl", "data.frame"
))
df
df_sf <- sf::st_as_sf( df, wkt = "geometry" )
# Simple feature collection with 3 features and 2 fields
# geometry type: LINESTRING
# dimension: XY
# bbox: xmin: -85.89 ymin: 38.31 xmax: -85.42 ymax: 38.76
# CRS: NA
# # A tibble: 3 x 3
# geometry var1 var2
# <LINESTRING> <dbl> <dbl>
# 1 (-85.76 38.34, -85.72 38.38) 4 1
# 2 (-85.46 38.76, -85.42 38.76) 5 2
# 3 (-85.89 38.31, -85.89 38.31) 6 3
我們可以使用plot
到 plot 的數據,包括在同一位置(行 = 3)有兩個點的LINESTRING
:
plot(st_geometry(df_sf), lwd = 10)
給予:
但是當我們 plot 使用ggplot
時,該點被刪除:
ggplot() +
geom_sf(data = df_sf, lwd = 8)
如果不手動提取僅包含一個點的位置,有沒有一種快速的方法可以將 ggplot 告訴ggplot
這些? 我可以看到這些點在技術上不是一條線,因為它們之間沒有距離,但plot
能夠拾取它們。 這個問題似乎相關但略有不同,我的LINESTRING
已經創建。
謝謝
這是headdesk emoji可能派上用場的情況之一:
ggplot() +
geom_sf(data = df_sf, lwd = 8, lineend = "round")
從包裝的美學規格小插圖來看,默認的 lineend 是“butt”,它精確地停在一條線的終點(所以一條長度為 0 的線不會有任何顯示),而“round”替代品則超出終點:
TL;DR : 長度為 0 的線串沒有被丟棄。 我們只是看不到它。
我認為如果修改長度等於 0 的 LINESTRING 幾何並將它們轉換為 POINTS,則可以解決該問題。 例如:
# packages
library(ggplot2)
library(sf)
#> Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
# data
df <- structure(
list(
geometry = c(
"LINESTRING (-85.76 38.34, -85.72 38.38)",
"LINESTRING (-85.46 38.76, -85.42 38.76)",
"LINESTRING (-85.89 38.31, -85.89 38.31)"
),
var1 = c(4, 5, 6),
var2 = c(1, 2, 3)
),
row.names = c(NA, -3L),
class = c("tbl_df", "tbl", "data.frame")
)
df_sf <- st_as_sf( df, wkt = "geometry" )
# Rebuild the geometry column in such a way that the zero-length LINESTRINGS are
# actually POINTS:
new_df_sf_geometry <- st_geometry(df_sf)
idx <- which(st_length(new_df_sf_geometry) == 0)
for (i in idx) {
new_df_sf_geometry[i] <- unique(st_cast(new_df_sf_geometry[i], "POINT"))
}
# This is the result
new_df_sf_geometry
#> Geometry set for 3 features
#> geometry type: GEOMETRY
#> dimension: XY
#> bbox: xmin: -85.89 ymin: 38.31 xmax: -85.42 ymax: 38.76
#> CRS: NA
#> LINESTRING (-85.76 38.34, -85.72 38.38)
#> LINESTRING (-85.46 38.76, -85.42 38.76)
#> POINT (-85.89 38.31)
# Replace the geometry
st_geometry(df_sf) <- new_df_sf_geometry
# Plot
ggplot(df_sf) +
geom_sf(size = 3)
由代表 package (v0.3.0) 於 2020 年 5 月 25 日創建
如果需要,您還可以采用比 for 循環更復雜的方法,例如purrr::map_if
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.