Legends are only created for variables that appear in aes
. So you need to add shape
to it. There are many different ways how to change your color for a variable that fulfils one condition, and the probably easiest is what you have done, to plot different subsets. If you decide so, best practice is not to use aes
within the main call, because it can mess up your plot quite a bit.
I have used a conditional statement for your shape and format the shape with scale_shape
. I personally find the added difference in color unnecessary for the plot, and I would probably not have done so, but because you have asked for it - here we go.
I also transformed carb to characters - you can also transform it to a factor. Because it is a categorical variable, it makes sense to also show it as such.
edit as per comment
As usual, there are plenty of ways of achieving things. Below two different ways of changing/ removing the titles of the legends and only showing the shape and value for 4
. Comments in the code.
Option 1: remove shape from the subset without '4', change legend title e.g. using labs
.
library(ggplot2)
ggplot() +
geom_point(data = subset(mtcars, carb !=4), aes(x = mpg, y = wt, color = as.character(carb)), shape = 1) +
geom_point(data = subset(mtcars, carb ==4), aes(x = mpg, y = wt, shape = carb == 4), color = 'black')+
scale_shape_manual(values = c(`TRUE` = 4, `FALSE` = 1)) +
labs(shape = NULL, color = 'Model prediction') +
guides(color = guide_legend(order = 1), shape = guide_legend(order = 2)) +
## I had to change the legend order, because the shape legend would draw on top otherwise. See link below!
facet_wrap(~ gear, nrow = 1)
Now - the legends are a bit separated, and believe me, it's quite tricky to bring them closer together.
Therefore:
option 2 Factorise your categorical data, subset your data, use drop = FALSE
in the scale_color
function, and change the legend aesthetic with override.aes
. Sounds like a hack, but still easier and safer than trying to reduce the distance between two legends.
mtcars2 <- mtcars
mtcars2$carb2 <- factor(mtcars$carb, levels = c(1:3,6,8,4))
ggplot() +
geom_point(data = subset(mtcars2, carb != 4), aes(x = mpg, y = wt, color= carb2), shape = 1) +
geom_point(data = subset(mtcars, carb == 4), aes(x = mpg, y = wt), shape = 4, color = 'black') +
facet_wrap(~ gear, nrow = 1) +
scale_color_discrete(drop = FALSE) +
labs(color = 'Model prediction') +
guides(color = guide_legend(override.aes = list(shape = c(rep(1, 5), 4),
color = c(RColorBrewer::brewer.pal(5, 'Dark2'), 'black'))))

Created on 2020-02-03 by the reprex package (v0.3.0)
Some links to learn from
Legend order is weird
change shapes in legend