ggplot2legendaesthetics

Merging shape and color in ggplot legend


I apologize that this might be a repost of another question. FWIW I've tried solutions from multiple threads but haven't been successful in my efforts.

How do I get my legend to include both shape and color?

library(tidyverse)
library(ggforce)

idL12 <- structure(list(date = structure(c(13371, 13371, 13371, 13371, 
13715, 13715, 13715, 14825, 14825, 16323, 16323, 16674, 16674, 
16997, 17065, 17065, 17065), class = "Date"), year = c(2006L, 
2006L, 2006L, 2006L, 2007L, 2007L, 2007L, 2010L, 2010L, 2014L, 
2014L, 2015L, 2015L, 2016L, 2016L, 2016L, 2016L), event.id = c(33L, 
33L, 33L, 33L, 35L, 35L, 35L, 47L, 47L, 58L, 58L, 61L, 61L, 65L, 
69L, 69L, 69L), ID = structure(c(44L, 49L, 60L, 66L, 49L, 60L, 
66L, 66L, 41L, 66L, 41L, 60L, 43L, 43L, 60L, 43L, 41L), levels = c("J11", 
"J16", "J17", "J2", "J22", "J26", "J27", "J30", "J31", "J35", 
"J36", "J37", "J38", "J39", "J40", "J41", "J42", "J46", "J47", 
"J49", "J56", "K16", "K21", "K22", "K25", "K26", "K33", "K35", 
"K36", "K37", "K40", "K42", "L100", "L101", "L103", "L105", "L106", 
"L109", "L110", "L111", "L113", "L115", "L119", "L12", "L123", 
"L21", "L22", "L26", "L41", "L47", "L5", "L54", "L55", "L57", 
"L58", "L67", "L7", "L72", "L73", "L77", "L82", "L83", "L86", 
"L87", "L91", "L94", "L95", "LU"), class = "factor"), Sex = structure(c(1L, 
2L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L
), levels = c("Female", "Male"), class = "factor"), age = c(73L, 
29L, 19L, 11L, 30L, 20L, 12L, 15L, 1L, 19L, 5L, 28L, 3L, 4L, 
29L, 4L, 7L), matr = structure(c(9L, 9L, 9L, 9L, 9L, 9L, 9L, 
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L), levels = c("J2", "J4", 
"J7", "J9", "K11", "K18", "K4", "K8", "L12", "L2", "L21", "L26", 
"L32", "L35", "L37", "L4", "L4 ", "L66", "L9"), class = "factor"), 
    matralive = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), levels = c("0", "1"), class = "factor"), 
    pod = structure(c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L), levels = c("J", "K", "L"), class = "factor")), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -17L))

cbPalette <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")

L12 <- ggplot(idL12,aes(x = date, y = age)) +
  geom_point(size=7,
    aes(
      shape = Sex,
      color = ID
    ), 
    show.legend = c(
      shape = FALSE,
      color = TRUE,
      size = FALSE
    )
  ) + 
  scale_x_date(expand = c(0.1, 0.1)) + 
  scale_y_continuous(expand = c(0.1, 0.1)) + 
  theme_classic() + 
  labs(x = "Year", y = "") +
  ggtitle("Matriline L12") + theme(plot.title = element_text(hjust = 0.5)) +
  scale_colour_manual(values=cbPalette)+
  theme(text=element_text(size=27))+
  guides(colour = guide_legend(override.aes = list(size=6))) +
  geom_mark_rect(aes(group=interaction(date))) + 
  labs(color = "Individual")
print(L12)

Here is what the plot looks like now:

enter image description here

Essentially I want the green circle in the legend to be a triangle. I have to do this for many more graphs each with varying numbers of circles/triangles.

Any help is greatly appreciated. Thank you in advance!


Solution

  • I ignored the Sex column entirely, and just set shape manually:

    idL12 %>%
        rename(Individual= ID) %>% 
        ggplot(aes(x = date, y = age)) +
        geom_point(size=7,
            aes(
            shape = Individual,
            color = Individual
            )
        ) + 
        scale_x_date(expand = c(0.1, 0.1)) + 
        scale_y_continuous(expand = c(0.1, 0.1)) + 
        theme_classic() + 
        labs(x = "Year", y = "") +
        ggtitle("Matriline L12") + theme(plot.title = element_text(hjust = 0.5)) +
        scale_shape_manual(values=c(16,16, 16, 17, 16,16))+
        scale_colour_manual(values=cbPalette)+
        theme(text=element_text(size=27))+
        geom_mark_rect(aes(group=interaction(date)))
    

    bar graph of some kind I couldn't get labs(ID = "Individual") to work, hence having to rename the column beforehand.