Skip to content

R Studio crashes when using geom_sf() with POLYGON simple features #6750

@jcvdav

Description

@jcvdav

Hi all,

I found a problem when using geom_sf() when trying to plot POLYGON simple features: Rstudio crashes. The POLYGONs can be plotted with base plot() or mapview::mapview(), but not with geom_sf(). The problem only arrises in @el-rivera's computer, even though we have the same versions of R, R Studio, GDAL, PROJ...., ggplot2. The only difference is her Windows vs my MacOS platforms, which I am sure is not trivial. The code below reproduces parts that work and don't work.

## Load packages ---------------------------------------------------------------
library(tidyverse)
library(sf)

## Build data ------------------------------------------------------------------
# A tibble with raw coordinates
coords <- tribble(~"lon", ~"lat",
                  -1, -1,
                  -1, 1,
                  1, 1,
                  1, -1,
                  -1, -1)

# Build sf objects -------------------------------------------------------------
# Coordinates as points
pts <- st_as_sf(coords,
                coords = c("lon", "lat"),
                crs = 4326) |> 
  mutate(a = 1)

# Coordinates as a linestring
lns <- pts |> 
  group_by(a) |> 
  summarize(do_union = F) |> 
  st_cast("LINESTRING")

# Coordinates as a polygon
pol <- lns |> 
  st_cast("POLYGON")

# Now plot them ----------------------------------------------------------------
# Confirm that plotting with base plot works
par(mfrow = c(1, 3))
plot(pts, max.plot = 1, reset = F)
plot(lns, max.plot = 1, reset = F)
plot(pol)
# It does

# Now ggplot
# This works
ggplot() + 
  geom_sf(data = pts)
# It does

# This works
ggplot() + 
  geom_sf(data = lns)
# It does

# This doesn't work. RStudio crashed with a bomb
ggplot() + 
  geom_sf(data = pol)

# This works, p is created in the environment
p <- ggplot() + 
  geom_sf(data = pol)

# But exporting p crashes R studio and results in a corrupted test.png file of 0B
ggsave(plot = p, filename = "test.png")

@el-rivera has provided two screenshots with session info and system info below

Image Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions