R – change shape legend in ggplot2 (geom_col)

I am trying to change the shape of the legends for a geom_col plot. By default, the legend is square, and I would like to change to round (or triangle or anything else). Since the colors are controlled by fill, I would assume overwriting this parameter should do the trick:

library(ggplot2) data("Titanic") Titanic <- as.data.frame(Titanic)  ggplot(data = Titanic, aes(x = Class, y = Freq, fill = Survived)) + geom_col() +    guides(fill = guide_legend(override.aes = list(shape = 16)))  

I also tried to be more specific

ggplot(data = Titanic, aes(x = Class, y = Freq, fill = Survived)) + geom_col() +   scale_shape_manual(values = c("No" = 16, "Yes" = 17))  

But the legend doesn’t change. Any advice?

(I did have a look to the related question Changing shape in legend ggplot2 but it doesn’t seem to work either. I suppose because geom_point is not used?)

Add Comment
1 Answer(s)

This rather poorly documented, but one argument to layer function is the key_glyph argument which can specify what sort of stuff to place into the legend. If you have bars and want point-like legends, you can override the default. Subsequently you can override aesthetics in the fill legend to fit your need. Be sure to pick a shape that has a fill parameter though.

library(ggplot2) data("Titanic") Titanic <- as.data.frame(Titanic)  ggplot(Titanic, aes(x = Class, y = Freq, fill = Survived)) +    geom_col(key_glyph = draw_key_point) +   guides(fill = guide_legend(override.aes = list(shape = 21, size = 5)))  

Created on 2020-08-25 by the reprex package (v0.3.0)

Answered on August 30, 2020.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.