Points in legend do not show if layers are named

I find very convenient to name layers of my graph. It permits to remove a layer easily by its names. For example:

g <- ggplot() + geom_point(aes(x=c(0, 1), y=c(2, 3), size = c(2, 5)), shape=19)
names(g$layers)[length(g$layers)] <- "Circles"
g <- g + geom_point(aes(x=c(4, 6), y=c(2, 7), size = c(2, 6)), 
                    shape=15, show.legend = TRUE)
names(g$layers)[length(g$layers)] <- "Squares"
# Both are displayed
g
# I remove the circles
g$layers[names(g$layers) == "Circles"] <- NULL
g

So it seems to work fine.
But here the points in the legend do not appear if I name the first layer. I do something wrong ?
Thanks

# It works
g <- ggplot() +  geom_segment(aes(x=27, y=0, xend = 27, yend = 1), linewidth = 1)
g + geom_point(aes(x=c(28, 30), y=c(0.3, 0.4), size = c(2, 3)), 
               show.legend = TRUE)
# It does no work
g <- ggplot() +  geom_segment(aes(x=27, y=0, xend = 27, yend = 1), linewidth = 1)
names(g$layers)[length(g$layers)] <- "Base"
g + geom_point(aes(x=c(28, 30), y=c(0.3, 0.4), size = c(2, 3)), 
               show.legend = TRUE)

The code that you say doesn't work, does work here.


packageVersion("ggplot2")
[1] ‘3.5.1’

sessionInfo()
R version 4.4.0 (2024-04-24)
Platform: aarch64-apple-darwin20
Running under: macOS Sonoma 14.4.1

Here my session infos:

packageVersion("ggplot2")
[1] ‘3.5.1’
sessionInfo()
R Under development (unstable) (2024-02-29 r86017)
Platform: aarch64-apple-darwin20
Running under: macOS Sonoma 14.5

or

packageVersion("ggplot2")
[1] ‘3.5.1’
sessionInfo()
R version 4.4.0 (2024-04-24)
Platform: x86_64-pc-linux-gnu
Running under: Ubuntu 22.04.4 LTS

The points do not show in the legend. They appear correctly in the graph itself.

# Points do not show in legend
g <- ggplot() +  geom_segment(aes(x=27, y=0, xend = 27, yend = 1), linewidth = 1)
names(g$layers)[length(g$layers)] <- "Base"
g + geom_point(aes(x=c(28, 30), y=c(0.3, 0.4), size = c(2, 3)), 
               show.legend = TRUE)

It should be easy to fix, if you just name the second layer, but yes, the legend does not render correctly. I'm unsure what's going on, but the only difference I can find is that in the broken layer, computed_geom_params, computed_mapping and computed_stat_params are NULL in the layer that doesn't work.

g <- ggplot() + geom_segment(aes(x=27, y=0, xend = 27, yend = 1), linewidth = 1)
names(g$layers)[length(g$layers)] <- "Base"

# this does not render the legend correctly
g + geom_point(aes(x=c(28, 30), y=c(0.3, 0.4), size = c(2, 3)), show.legend = TRUE)

g2 <- g + geom_point(aes(x=c(28, 30), y=c(0.3, 0.4), size = c(2, 3)), show.legend = TRUE) 
names(g2$layers)[length(g2$layers)] <- "Points"

# this renders the legend correctly
g2

# show the difference between the broken and working layers

str((g+ geom_point(aes(x=c(28, 30), y=c(0.3, 0.4), size = c(2, 3)), show.legend = TRUE))$layers[[2]])
# note this gives:
# computed_geom_params: NULL
# computed_mapping: NULL
# computed_stat_params: NULL

str(g2$layers[["Points"]])
# note that this gives 
# computed_geom_params: list
# computed_mapping: uneval
# computed_stat_params: list

Right. It works if the layer is named. Thanks for the fix