I have a data set that uses both the line type and color aesthetic. The legend shows both aesthetics but I would only like one aesthetic (either color or line type) in the legend itself.
This also applies to another data set in which I want only one out of four lines larger than the rest which I do with an ifelse
statement in the geom_line aesthetic, but the actual ifelse
statement shows up in the legend.
I've taken an example from the mtcars
data set where I want only color to show up.
library(tidyverse)
mtcars <- as.tibble(mtcars)
mtcars$gear <- as.factor(mtcars$gear)
mtcars$cyl <- as.factor(mtcars$cyl)
mtcars1 <- mtcars %>%
arrange(gear) %>%
ggplot(aes(x = qsec, group = 1)) +
geom_point(aes(y=disp, group = cyl, color = cyl)) +
geom_line(aes(y=disp, group = cyl, color = cyl, linetype = cyl)) +
scale_color_manual(values=c("#000000", "#E69F00", "#56B4E9")) +
labs(title = "MTCARS Example", colour="Cylinder",x= "x",y="Disp")
print(mtcars1)
Is it possible to manipulate the legend to only show one aesthetic?