删除船体图上图例的特定部分

Remove specific parts of a legend on a hull graph

我有这些数据(编辑以添加 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    

我用这段代码制作了一个船体图:

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")

我想 1) 从图例中删除“size”和“matr”;和 2) x 轴显示第一年和最后一年,同样,y 轴显示最后一个年龄。

非常感谢任何提示。谢谢!

我会这样做:

1。更改图例

  • show.legend = FALSE 添加到 geom_mark_hull 以删除 matr
  • show.legend 提供命名逻辑向量 geom_point()

2。换轴

有很多方法可以做到这一点。我认为这里最简单的方法就是将 expand 选项添加到 scale_** 函数中,使轴在两个方向上都更长一些。

代表:

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") 

reprex package (v2.0.1)

于 2022-03-30 创建