Skip to content
Prev 396564 / 398502 Next

ggplot two-factor legend

?s 17:43 de 18/07/2024, Rui Barradas escreveu:
Hello,

Here is a more complete an answer with the built-in data set mtcars.
Note that the group aesthetic is not used. This is because linetype is 
categorical (after mutate) and there's no need to group again by the 
same variable (am).

Remove labels from scale_linetype_manual and there are two legends but 
with the same labels the legends merge.


library(ggplot2)
library(dplyr)

mtcars %>%
   # linetype must be categorical
   mutate(am = factor(am)) %>%
   ggplot(aes(hp, disp, color = am, linetype = am)) +
   geom_line() +
   scale_color_manual(
     values = c("red","dark green"),
     labels = c("?LN", "BIO")
   ) +
   scale_linetype_manual(
     values = c("dashed", "solid"),
     labels = c("?LN", "BIO")
   ) +
   theme_bw()


Hope this helps,

Rui Barradas