Can I change the symbol used in the legend for a boxplot? I would like to use a square instead of the boxplot symbol that is the default. I've tried the following after some searching, however the last line does not seem to have an effect:
df = data.frame(x = rnorm(10), y = sample(letters[1:2], 10, TRUE))
library(ggplot2)
ggplot(df, aes(y, x)) +
geom_boxplot(aes(color = y)) +
guides(fill = guide_legend(override.aes = list(shape = 22)))
The reason is that there is another graph left of this one that uses the same colors but points instead of a boxplot, so I would like a legend that works for both.
I guess there are multiple ways to achieve what you want, but what I usually do is:
geom_point layer with size 0geom_point and NOT for geom_boxplotCode:
df <- data.frame(x = rnorm(10),
y = sample(letters[1:2], 10, TRUE))
library(ggplot2)
ggplot(df, aes(y, x, color = y)) +
# Add dummy point layer with invisible points (size 0)
geom_point(size = 0, shape = 22) +
# Don't show legend
geom_boxplot(show.legend = FALSE) +
# Increase point size
guides(color = guide_legend(override.aes = list(size = 10)))
Result:

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With