Home > Net >  Remove specific parts of a legend on a hull graph
Remove specific parts of a legend on a hull graph

Time:03-31

I have these data (edited to add dput):

> dput(idL12)
structure(list(date = structure(c(13371, 13371, 13371, 13371, 
13715, 13715, 14825, 14825, 16323, 16323, 16674, 16674, 16997, 
17065, 17065, 17065), class = "Date"), year = c(2006L, 2006L, 
2006L, 2006L, 2007L, 2007L, 2010L, 2010L, 2014L, 2014L, 2015L, 
2015L, 2016L, 2016L, 2016L, 2016L), event.id = c(33L, 33L, 33L, 
33L, 35L, 35L, 45L, 45L, 56L, 56L, 59L, 59L, 63L, 67L, 67L, 67L
), id = structure(c(44L, 49L, 60L, 66L, 49L, 60L, 66L, 41L, 66L, 
41L, 60L, 43L, 43L, 60L, 43L, 41L), .Label = 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"), class = "factor"), sex = structure(c(1L, 
2L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label =                 
c("0", "1"), class = "factor"), age = c(73L, 29L, 19L, 11L, 30L, 20L, 
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), .Label = 
c("J2", "J4", "J7", "J9", "K11", "K18", "K4", "K8", "L12", "L2", "L21", 
"L26", "L32", "L35", "L37", "L4", "L4 ", "L45", "L66", "L9"), class = 
"factor"), matralive = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("0", "1"), class = "factor"), 
pod = structure(c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L), .Label = c("J", "K", "L"), class = "factor")), 
row.names = c(NA, -16L), class = c("tbl_df", "tbl", "data.frame"))

> idL12
# A tibble: 16 x 9
date        year event.id id    sex     age matr  matralive pod  
<date>     <int>    <int> <fct> <fct> <int> <fct> <fct>     <fct>
 1 2006-08-11  2006       33 L12   0        73 L12   1         L    
 2 2006-08-11  2006       33 L41   1        29 L12   1         L    
 3 2006-08-11  2006       33 L77   0        19 L12   1         L    
 4 2006-08-11  2006       33 L94   0        11 L12   1         L    
 5 2007-07-21  2007       35 L41   1        30 L12   1         L    
 6 2007-07-21  2007       35 L77   0        20 L12   1         L    
 7 2010-08-04  2010       45 L94   0        15 L12   1         L    
 8 2010-08-04  2010       45 L113  0         1 L12   1         L    
 9 2014-09-10  2014       56 L94   0        19 L12   0         L    
10 2014-09-10  2014       56 L113  0         5 L12   0         L    
11 2015-08-27  2015       59 L77   0        28 L12   0         L    
12 2015-08-27  2015       59 L119  0         3 L12   0         L    
13 2016-07-15  2016       63 L119  0         4 L12   0         L    
14 2016-09-21  2016       67 L77   0        29 L12   0         L    
15 2016-09-21  2016       67 L119  0         4 L12   0         L    
16 2016-09-21  2016       67 L113  0         7 L12   0         L    

And I made a hull graph using this code:

ggplot(idL12,aes(x = date, y = age ))  
geom_point(aes(shape = sex,color=id, size=3))  
geom_mark_hull(aes(fill=matr,label = matr),concavity=2.8)  
scale_x_date()   theme_classic()   labs(x="Year",y="Age")

enter image description here

I want to 1) remove the "size" and "matr" from the legend; and 2) have the x axis show the first and last year, and similarly, the y axis to show the last age.

Any tips is greatly appreciated. Thank you!

CodePudding user response:

This is how I would've done this:

1. Change legend

  • Add show.legend = FALSE to geom_mark_hull to remove matr.
  • Supply a named logical vector to show.legend for the geom_point().

2. Change axis

There are many ways of doing this. I think the easiest here is just to add the expand option to the scale_** functions, to make the axis a bit longer in both directions.

Reprex:

library(dplyr)
library(ggplot2)
library(ggforce)

idL12 %>%
  ggplot(aes(x = date, y = age))  
  geom_point(
    aes(
      shape = sex,
      color = id,
      size = 3
    ), 
    show.legend = c(
      shape = FALSE,
      color = TRUE,
      size = FALSE
    )
  )  
  geom_mark_hull(aes(fill = matr, label = matr), concavity = 2.8, show.legend = 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 = "Age") 

Created on 2022-03-30 by the reprex package (v2.0.1)

  • Related