ggplot不绘制SF对象的线串点

时间:2020-05-25 16:22:32

标签: r ggplot2 sf

给出以下数据:

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绘制包括LINESTRING的数据,其中plot(st_geometry(df_sf), lwd = 10) 在同一位置(行= 3)具有两个点:

ggplot

给予:

enter image description here

但是当我们使用ggplot() + geom_sf(data = df_sf, lwd = 8) 对其进行绘制时,该点将被丢弃:

ggplot

enter image description here

没有手动提取仅包含点的位置,是否有一种快速的方法告诉plot绘制这些点?我可以看到这些点在技术上不是一条线,因为它们之间没有距离,但是LINESTRING可以拾取它们。这个question似乎相关,但略有不同,我的foreach ($splited_sentences as $sentence_key => $sentence_value) { $html_dom = Html::load($sentence_value); $xpath = new DOMXPath($html_dom); foreach ($xpath->query('//span[not(@id) and not(@class)]') as $element_key => $element) { $just_text = strip_tags ($element); $splited_sentences[$sentence_key] = str_replace($element, $just_text); } } 已经创建。

谢谢

2 个答案:

答案 0 :(得分:2)

这是可以在总机表情符号派上用场的情况之一:

ggplot() +
  geom_sf(data = df_sf, lwd = 8, lineend = "round")

plot

在包装的vignette on aesthetic specs中,默认的行尾为“对接”,该行恰好在一行的终点处停止(因此长度为0的行不会显示任何内容),而“ “替代方法超出了终点:

illustration

TL; DR :没有删除长度为0的线串。我们只是看不到。

答案 1 :(得分:1)

我认为,如果修改长度等于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)

reprex package(v0.3.0)于2020-05-25创建

如果需要,还可以采用比purrr::map_if之类的for循环更复杂的方法。