How to remove the legend key border in ggplot's geom_sf?

dufei

By default geom_sf draws a legend whose keys (in the fill color) are surrounded by a frame in the border color (i.e., the color aesethetic). Is there a way to remove this frame? Apparently, this frame is inside the key rectangle so it cannot be overwritten by modifying legend.key in the example below.

library(tidyverse)
library(sf)
library(spData)

spData::world %>% 
  ggplot(aes(fill = continent)) +
  geom_sf() +
  theme(legend.key = element_rect(colour = "red"))

Created on 2022-08-11 by the reprex package (v2.0.1)

Ideally, I would not only remove the frames but also remvoe the vertical space between the legend keys entirely, similar to the appearance of guide_colorsteps.

My hacky attempt here succeeds in removing the frames but the rectangles are not perfectly adjacent yet. Is there a) a less hacky way to remove the frames, and b) a way to remove the vertical space between the rectangles altogether?

library(tidyverse)
library(sf)
library(spData)

spData::world %>% 
  ggplot() +
  geom_sf(aes(fill = continent, color = continent)) +
  geom_sf(data = st_geometry(spData::world), fill = NA, color = "black") + 
  theme(legend.spacing.y = unit(0, 'cm')) +
  guides(fill = guide_legend(byrow = TRUE))

Created on 2022-08-11 by the reprex package (v2.0.1)

stefan

One possible option which does not require to use theme or guides would be to set the key_glyph to "rect":

library(tidyverse)
library(sf)
#> Linking to GEOS 3.9.1, GDAL 3.4.3, PROJ 7.2.1; sf_use_s2() is TRUE
library(spData)
#> To access larger datasets in this package, install the spDataLarge
#> package with: `install.packages('spDataLarge',
#> repos='https://nowosad.github.io/drat/', type='source')`

spData::world %>% 
  ggplot(aes(fill = continent)) +
  geom_sf(key_glyph = "rect", color = "red")

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I remove border lines when using geom_sf?

add commas to colorbar legend in geom_sf (R ggplot)

How to change legend shapes when using geom_sf?

r-ggplot2 divide legend into 2 colums: geom_sf

geom_sf and geom_point, how to assign colors based on the points inserted on the map to the corresponding types in the legend?

How can I completely remove legend.key from ggplot2 legend?

How to remove the gap in the ggplot legend?

Remove white margin surrounding geom_sf() objects in R Shiny ggplot with renderPlot()

Adjust thickness of border surrounding legend key in ggplot2

Add line legend to geom_sf

Remove fill around legend key for geom_ribbon in ggplot2

ggplot2 remove legend.key box after combining multiple geom layers

How to Layer two geom_sf layers in ggplot with two different scale_fill_gradientn?

How to get ride of polygon borders using geom_sf in ggplot2

How to color the line different between 2 points on borders in maps made with geom_sf and ggplot2

ggplot removes geom_label border from legend

How to remove random diagonal lines from star map vizualizations produced by geom_sf?

How to remove scientific notation from this legend ggplot

How to change the orientation of the "Key" of a Legend in ggplot?

How to align the legend key and text in ggplot?

ggplot2: how to remove slash from geom_density legend

How to remove JInternalFrame's Border?

How to get vertical lines in legend key using ggplot2 for geom_pointrange() type graphic

remove legend title in ggplot

Remove legend in ggplot

How to add a legend for geom_vline in ggplot if there is already a legend?

Draw legend with geom_sf when no aesthetic is specified

How to flip y axis in geom_sf()?

Remove legend key in matplotlib