Проблема с отображением спроецированных точек: неожиданный тип координат ggplot и линии сетки - PullRequest
2 голосов
/ 10 октября 2019

Я пытаюсь построить перепроектированный набор данных, и хотя координаты были перепроецированы, ggplot продолжает использовать систему координат градус-долг-лат.

Вот воспроизводимый пример:

library(sf)
library(dplyr)
library(ggplot2)

set.seed(42)
df <- tibble(lon = runif(20, min = -73, max = -68),
             lat = runif(20, min = 41, max = 46))
sf <- st_as_sf(df, coords = c("lon", "lat"), crs = 4326, agr = "constant")

# This plot has the expected lon-lat degree coords
ggplot(sf) + geom_sf()

This plot has the expected lon-lat degree coords

# Lambert equal-area projection
laea_centered <- "+proj=laea +lat_0=43.15268 +lon_0=-70.30744"
coord_shift <- "+x_0=12019341.4 +y_0=1730136"
proj_ref <- " +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs"
laea_proj4 <- paste(laea_centered, coord_shift, proj_ref, sep = " ")

sf <- st_transform(sf, crs = laea_proj4)

# Glancing at the data, the transformation appears correct.
sample_n(sf, 5)
# Simple feature collection with 5 features and 0 fields
# geometry type:  POINT
# dimension:      XY
# bbox:           xmin: 75403.74 ymin: 1109827 xmax: 248543.9 ymax: 1660612
# epsg (SRID):    NA
# proj4string:    +proj=laea +lat_0=-70.30744 +lon_0=43.15268 +x_0=12019341.4 +y_0=1730136 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs
# # A tibble: 5 x 1
# geometry
# <POINT [m]>
# 1 (248543.9 1153107)
# 2 (88341.34 1109827)
# 3 (166449.3 1138656)
# 4 (87832.83 1660612)
# 5 (75403.74 1430964)

# Still has geographic coordinates and reticule
ggplot(sf) + geom_sf()

Точки были спроектированы, но на графике все еще используются географические координаты для осей и сетки.

enter image description here

# Same as above, but I wouldn't expect it to be different
# because ggplot takes the crs from the initial data argument.
ggplot(sf) +
  geom_sf() +
  coord_sf(crs = laea_proj4)

enter image description here

1 Ответ

1 голос
/ 10 октября 2019

Используйте coord_sf() и установите datum как у объекта:

library(sf)
#> Linking to GEOS 3.7.1, GDAL 2.4.2, PROJ 5.2.0
library(dplyr)

library(ggplot2)

set.seed(42)
df <- tibble(lon = runif(20, min = -73, max = -68),
             lat = runif(20, min = 41, max = 46))
sf <- st_as_sf(df, coords = c("lon", "lat"), crs = 4326, agr = "constant")
# Lambert equal-area projection
laea_centered <- "+proj=laea +lat_0=-70.30744 +lon_0=43.15268"
coord_shift <- "+x_0=12019341.4 +y_0=1730136"
proj_ref <- " +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs"
laea_proj4 <- paste(laea_centered, coord_shift, proj_ref, sep = " ")

sf <- st_transform(sf, crs = laea_proj4)

ggplot(sf) + geom_sf() + coord_sf(datum = st_crs(sf))

Создано в 2019-10-10 представляет пакет (v0.3.0)

...