tidyverse / tidyverse/ggplot2

geom_sf(show.legend = "polygon") leads to error "Error in `vec_slice()`"

Open
#6,804 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug guides :straight_ruler:
Dominant language
R
Stars
7k
Forks
2.1k
Avg merge
59m
Merged PRs (30d)
2

Description

Using geom_sf(..., show.legend = "polygon") throws an error, while the same code with show.legend = "point" or show.legend = "line" will work well (see reprex below)?

The error is:

#> Error in vec_slice():
#> ! x must be a vector, not NULL.
#> ℹ Read our FAQ about scalar types (?vctrs::faq_error_scalar_type) to learn more.

library(ggplot2)
nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)

nc$const_var <- "a"

ggplot(nc) +
  geom_sf(aes(fill = const_var), show.legend = "polygon")
#> Error in `vec_slice()`:
#> ! `x` must be a vector, not `NULL`.
#> ℹ Read our FAQ about scalar types (`?vctrs::faq_error_scalar_type`) to learn more.
ggplot(nc) +
  geom_sf(aes(fill = const_var), show.legend = "point")

ggplot(nc) +
  geom_sf(aes(fill = const_var), show.legend = "line")

Created on 2026-02-09 with reprex v2.1.1

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the supplied R reprex with sf and geom_sf, then trace the geom_sf legend handling for show.legend = "polygon" and compare it with the working point and line cases. Done means the polygon example no longer raises the vec_slice() error while the existing point and line behavior remains working.

Written by the indexing model from the issue text.

Assessment

Tech stack
r
Domain
data-visualization
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.