-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Plotting order: geom_sf()
vs geom_point()
#4340
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Technically, I'm not sure if this should be called a bug; because A simpler version of the reprex: library(sf)
#> Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
library(ggplot2)
library(patchwork)
pt <- st_sfc(
st_point(c(-1, 1)),
st_point(c( 0, 1)),
st_point(c( 1, 1)),
st_point(c( 1, 0)),
st_point(c( 1, -1)),
st_point(c( 0, -1)),
st_point(c(-1, -1)),
st_point(c(-1, 0))
)
sf <- st_sf(
g = rep(c("a", "b"), times = 4),
geometry = pt
)
non_sf <- dplyr::mutate(sf, tibble::as_tibble(st_coordinates(sf)))
p1 <- ggplot(non_sf) +
geom_point(aes(X, Y, colour = g), size = 65) +
guides(colour = "none") +
coord_equal() +
ggtitle("Expected")
p2 <- ggplot(sf) +
geom_sf(aes(colour = g), size = 65) +
guides(colour = "none") +
ggtitle("Actual")
p1 * p2 Created on 2021-02-25 by the reprex package (v1.0.0) |
A possible fix could be to wrap library(sf)
#> Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
devtools::load_all("~/repo/ggplot2")
#> Loading ggplot2
library(patchwork)
pt <- st_sfc(
st_point(c(-1, 1)),
st_point(c( 0, 1)),
st_point(c( 1, 1)),
st_point(c( 1, 0)),
st_point(c( 1, -1)),
st_point(c( 0, -1)),
st_point(c(-1, -1)),
st_point(c(-1, 0))
)
sf <- st_sf(
g = rep(c("a", "b"), times = 4),
geometry = pt
)
non_sf <- dplyr::mutate(sf, tibble::as_tibble(st_coordinates(sf)))
p1 <- ggplot(non_sf) +
geom_point(aes(X, Y, colour = g), size = 65) +
guides(colour = "none") +
coord_equal() +
ggtitle("Expected")
p2 <- ggplot(sf) +
geom_sf(aes(colour = g), size = 65) +
guides(colour = "none") +
ggtitle("Actual")
p1 * p2 Created on 2021-02-25 by the reprex package (v1.0.0) |
An workaround can also be to explicitly set a single group, so that the stat co-processes all points at the same time. The convention is to set the group to -1, but it can be any grouping. library(sf)
library(ggplot2)
library(tibble)
nc <- sf::st_read(
system.file("shape/nc.shp", package = "sf"),
quiet = TRUE)
N <- 10000
sampled_points <- st_sample(
st_transform(nc, 32119), # NC state plane, m
size = N)
labeled_points <- st_sf(
geometry = sampled_points,
fruit = sample(
c("Apple", "Banana", "Cherry"),
size = N,
replace = TRUE))
ggplot(data = labeled_points) +
geom_sf(aes(colour = fruit, group = -1), size = I(3)) Created on 2022-12-30 by the reprex package (v2.0.1) |
I'm trying to figure out how to get
geom_sf()
to behave likegeom_point()
when it comes to plotting order.I understand there's no longer an option to use
aes(order = ...)
. Is there another way to preventgeom_sf()
from re-ordering the points (rows) of its input, whilst still colouring by some attribute?When all the points belonging to one class are plotted on top of all the others, it's hard to form an "unbiased" picture of the relative spatial distributions of the different classes. (In this reprex, they're nothing special, but with real data, the differences are meaningful.)
This seems like a bug, insofar as the behavior of
geom_sf()
should be consistent with that ofgeom_point()
. But maybe I'm overlooking something? Thank you for your time and attention, and for this wonderfully useful package.Created on 2021-02-03 by the reprex package (v0.3.0)
Session info
The text was updated successfully, but these errors were encountered: