r - 定义在 ggplot2 中与多个 geom 一起使用时用于 geom_point 的形状

标签 r ggplot2

我以前曾帮助我向折线图添加一个点,结合 geom_pointgeom_linerange ,创建我想要的确切效果(参见问题 here )。

带有 dataset is here 的 csv 文件.

但是,我想定义确切的形状geom_point用于不同的组 - 目前他们都使用 pch = 21 , 这是一个根据 fill = time_period 取色的点.相反,我想为每个 time_period成为不同的形状(本质上只突出特定时间段):

  • <10 years应该有 pch = 32
  • 10-20 years应该有 pch = 1
  • >20 years应该有 pch = 16

这是@aaumai 提供的工作图代码

library(tidyverse)
library(wesanderson) 

df%>%
  ggplot() +
  geom_linerange( aes(ymin = minyear, ymax = maxyear, 
                                                colour = factor(diet),
                                               x = fct_reorder(factor(id), desc(sort))),
                 size = 1) +
  geom_point(aes(x = fct_reorder(factor(id), desc(sort)), y = max(maxyear) + 1, fill = time_period), 
show.legend = TRUE, pch = 21, color = "white", size = 1)+
  scale_colour_manual(values = wes_palette("Cavalcanti1")) +
  labs(x = NULL, y = NULL) +
  theme_bw() +
  coord_flip() +
  guides(colour = F) +
  theme(panel.grid.minor = element_blank(),
        panel.grid.major.y = element_blank(),
        panel.grid.major.x = element_line(),
        axis.ticks = element_blank(),
        legend.position = "bottom", 
        panel.border = element_blank(),
        legend.title = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        plot.title = element_text(size = 20, vjust = 1, hjust = 0),
        axis.text = element_text(size = 16), 
        axis.title = element_text(size = 20))

我已经尝试了其他 stackoverflow 帖子中建议的各种选项,但都没有奏效,包括:

  • 仅使用组制作单独的 geom_point 图 [time_period == "<10 years"]等等
  • 使用 scale_shape_manual(values = c(31, 1, 16))

编辑: 这是数据集的代码:

> dput(df)
structure(list(minyear = c(1970, 1980, 1983, 1980, 1974, 1974, 
1974, 1974, 1974, 1988, 2000, 1981, 1974, 1975, 1974, 1974, 1981, 
1981, 1981, 1981, 1981, 1993, 1990, 1982, 1982, 1982, 1982, 1982, 
1982, 1982, 1982, 1982, 1982, 1982, 1978, 1974, 1974, 1974, 1974, 
1974, 1974, 1974, 1974, 1981, 2001, 2001, 1994, 1994, 1994, 1994, 
1994, 1994, 1994, 1994, 1994, 1994, 1994, 1994, 1994, 1994, 1990, 
1994, 1990, 1990, 1990, 1990, 1990, 1990, 1990, 1990, 1990, 1990, 
1990, 1990, 1990, 1990, 1990, 1990, 2001, 1989, 1979, 1994, 1996, 
1996, 1996, 1996, 1992, 1997, 1989, 1981, 1981, 1993, 1990, 1992, 
1991, 1995, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 
2000, 1988, 1981, 1981, 1982, 1982, 1970, 1981, 1981, 1981, 1982, 
1970, 1998, 1977, 1977, 1977, 1977, 1977, 1977, 1977, 1977, 1977, 
1977, 1977, 1977, 1977, 1977, 1995, 1995, 1996, 1981, 1996, 1996, 
1996, 1996, 1996, 1996, 1996, 1982, 1982, 1985, 1977, 1977, 1977, 
1977, 1977, 1977, 1999, 1983, 1982, 1982, 1982, 1989, 2004, 2004, 
2004, 1996, 1996, 1996, 1996, 1996, 1984, 1972, 1972, 1975, 1983, 
1983, 1983, 1983, 1983, 1983, 1987, 1987, 1987, 1987, 1979, 1993, 
1999, 1999, 1989, 2004, 2004, 1970, 2004, 2001, 2001, 2001, 2001, 
2001, 2001, 2001, 1992, 1994, 1994, 1980), maxyear = c(1980, 
2008, 2008, 2008, 2007, 2008, 2008, 2008, 2008, 2000, 2006, 2000, 
2011, 2012, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 1999, 2007, 
2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 
2008, 2008, 2008, 2008, 2008, 2008, 2008, 2007, 2008, 1999, 2006, 
2006, 2008, 2007, 2008, 2007, 2008, 2008, 2008, 2008, 2007, 2007, 
2008, 2008, 2008, 2007, 2007, 2008, 2007, 2007, 2007, 2007, 2007, 
2008, 2007, 2007, 2008, 2008, 2008, 2007, 2008, 2007, 2008, 2007, 
2008, 2007, 1995, 2008, 2007, 2007, 2007, 2007, 2005, 2008, 2007, 
2008, 2008, 2007, 2007, 2007, 2007, 2004, 2007, 2008, 2007, 2007, 
2008, 2008, 2008, 2008, 2008, 2008, 2005, 2003, 2003, 2003, 2003, 
2007, 2007, 2007, 2007, 2007, 1994, 2008, 2005, 2005, 2005, 2005, 
2005, 2005, 2005, 2005, 2005, 2005, 2005, 2005, 2005, 2005, 2007, 
2007, 2008, 2009, 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2008, 
2008, 2005, 2006, 2006, 2006, 2006, 2006, 2006, 2007, 2000, 1999, 
2000, 2000, 2007, 2011, 2011, 2011, 2008, 2008, 2008, 2008, 2008, 
1989, 2007, 2007, 2007, 2006, 2006, 2006, 2006, 2006, 2006, 2003, 
2003, 2003, 2003, 1998, 2008, 2006, 2006, 2007, 2010, 2010, 1986, 
2010, 2008, 2008, 2008, 2008, 2008, 2008, 2008, 2007, 2007, 2007, 
1988), id = structure(c(59L, 176L, 178L, 177L, 164L, 166L, 163L, 
165L, 162L, 8L, 188L, 6L, 35L, 33L, 36L, 14L, 37L, 24L, 34L, 
28L, 15L, 7L, 110L, 42L, 48L, 43L, 44L, 45L, 49L, 12L, 50L, 47L, 
13L, 46L, 67L, 156L, 155L, 158L, 161L, 160L, 157L, 154L, 159L, 
26L, 1L, 2L, 109L, 96L, 88L, 103L, 112L, 104L, 92L, 89L, 98L, 
101L, 105L, 91L, 90L, 94L, 95L, 111L, 108L, 106L, 102L, 107L, 
79L, 85L, 97L, 100L, 87L, 86L, 84L, 99L, 83L, 81L, 82L, 93L, 
190L, 191L, 58L, 194L, 115L, 80L, 114L, 113L, 193L, 192L, 184L, 
57L, 56L, 185L, 68L, 187L, 189L, 195L, 199L, 197L, 200L, 198L, 
196L, 149L, 150L, 151L, 153L, 152L, 74L, 167L, 168L, 169L, 170L, 
71L, 75L, 76L, 78L, 77L, 3L, 186L, 134L, 123L, 128L, 130L, 129L, 
122L, 126L, 125L, 135L, 124L, 133L, 131L, 132L, 127L, 72L, 73L, 
69L, 52L, 142L, 138L, 139L, 143L, 137L, 136L, 140L, 173L, 172L, 
171L, 118L, 116L, 120L, 117L, 121L, 119L, 183L, 29L, 25L, 31L, 
38L, 182L, 51L, 21L, 18L, 146L, 148L, 147L, 145L, 144L, 175L, 
27L, 41L, 141L, 61L, 64L, 66L, 65L, 63L, 62L, 32L, 23L, 22L, 
30L, 4L, 5L, 39L, 40L, 70L, 11L, 9L, 174L, 10L, 55L, 20L, 19L, 
53L, 17L, 16L, 54L, 181L, 179L, 180L, 60L), .Label = c("10192", 
"10193", "10415", "11751", "12015", "12016", "12019", "12022", 
"12077", "12079", "12080", "12112", "12129", "12138", "12141", 
"12169", "12171", "12175", "12176", "12178", "12180", "12249", 
"12268", "12269", "12277", "12280", "12283", "12290", "12302", 
"12348", "12355", "12362", "12363", "12364", "12369", "12377", 
"12382", "12392", "12397", "12398", "12402", "12425", "12427", 
"12431", "12432", "12437", "12440", "12444", "12447", "12451", 
"12453", "12477", "12479", "12482", "12485", "12491", "12525", 
"190", "2283", "5140", "6459", "6460", "6462", "6465", "6466", 
"6467", "8157", "8550", "8562", "8563", "8564", "8572", "8574", 
"8579", "8580", "8581", "8582", "8583", "8585", "8588", "8591", 
"8592", "8593", "8594", "8599", "8600", "8601", "8605", "8606", 
"8607", "8608", "8609", "8612", "8613", "8616", "8617", "8629", 
"8630", "8631", "8649", "8650", "8651", "8652", "8653", "8655", 
"8659", "8662", "8663", "8664", "8665", "8666", "8667", "8668", 
"8669", "8677", "8683", "8685", "8687", "8689", "8691", "8692", 
"8700", "8723", "8724", "8725", "8726", "8727", "8728", "8742", 
"8743", "8761", "8763", "8764", "8765", "8766", "8767", "8772", 
"8773", "8775", "8776", "8779", "8782", "8783", "8784", "8785", 
"8786", "8787", "8788", "8793", "8794", "8795", "8797", "8799", 
"8817", "8818", "8835", "8836", "8838", "8928", "8929", "8930", 
"8938", "8939", "8940", "8941", "8942", "8943", "8944", "8948", 
"8949", "8958", "8962", "8963", "8967", "8968", "8990", "8991", 
"8992", "8993", "8994", "8995", "8999", "9000", "9001", "9012", 
"9013", "9014", "9015", "9016", "9017", "9018", "9019", "9023", 
"9024", "9086", "9541", "9542", "9543", "9544", "9545"), class = "factor"), 
    diet = structure(c(1L, 1L, 3L, 1L, 4L, 4L, 4L, 4L, 4L, 4L, 
    2L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 4L, 4L, 
    4L, 4L, 4L, 4L, 3L, 4L, 4L, 4L, 4L, 1L, 4L, 4L, 3L, 1L, 4L, 
    4L, 1L, 3L, 4L, 1L, 1L, 4L, 3L, 4L, 5L, 5L, 5L, 2L, 2L, 5L, 
    5L, 4L, 3L, 2L, 3L, 3L, 5L, 4L, 4L, 5L, 5L, 5L, 2L, 5L, 5L, 
    4L, 4L, 3L, 5L, 2L, 4L, 2L, 3L, 3L, 3L, 5L, 4L, 4L, 3L, 4L, 
    4L, 4L, 4L, 5L, 4L, 4L, 5L, 2L, 5L, 4L, 2L, 5L, 4L, 5L, 1L, 
    4L, 4L, 4L, 4L, 4L, 1L, 4L, 5L, 4L, 5L, 4L, 4L, 5L, 5L, 4L, 
    5L, 4L, 3L, 4L, 4L, 2L, 1L, 5L, 4L, 3L, 4L, 4L, 4L, 4L, 4L, 
    4L, 3L, 5L, 4L, 5L, 4L, 4L, 4L, 4L, 2L, 5L, 4L, 5L, 4L, 4L, 
    4L, 4L, 1L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
    4L, 4L, 2L, 5L, 4L, 5L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
    3L, 4L, 4L, 4L, 4L, 4L, 3L, 4L, 4L, 3L, 4L, 4L, 2L, 4L, 4L, 
    4L, 4L, 4L, 3L, 4L, 4L, 1L, 3L, 3L, 5L), .Label = c("VertFishScav", 
    "FruiNect", "Omnivore", "Invertebrate", "PlantSeed"), class = "factor"), 
    sort = c(11970, 11980, 31983, 11980, 41974, 41974, 41974, 
    41974, 41974, 41988, 22000, 41981, 41974, 41975, 41974, 41974, 
    41981, 41981, 41981, 41981, 41981, 41993, 51990, 41982, 41982, 
    41982, 41982, 41982, 41982, 31982, 41982, 41982, 41982, 41982, 
    11978, 41974, 41974, 31974, 11974, 41974, 41974, 11974, 31974, 
    41981, 12001, 12001, 41994, 31994, 41994, 51994, 51994, 51994, 
    21994, 21994, 51994, 51994, 41994, 31994, 21994, 31994, 31990, 
    51994, 41990, 41990, 51990, 51990, 51990, 21990, 51990, 51990, 
    41990, 41990, 31990, 51990, 21990, 41990, 21990, 31990, 32001, 
    31989, 51979, 41994, 41996, 31996, 41996, 41996, 41992, 41997, 
    51989, 41981, 41981, 51993, 21990, 51992, 41991, 21995, 52000, 
    42000, 52000, 12000, 42000, 42000, 42000, 42000, 42000, 12000, 
    41988, 51981, 41981, 51982, 41982, 41970, 51981, 51981, 41981, 
    51982, 41970, 31998, 41977, 41977, 21977, 11977, 51977, 41977, 
    31977, 41977, 41977, 41977, 41977, 41977, 41977, 31977, 51995, 
    41995, 51996, 41981, 41996, 41996, 41996, 21996, 51996, 41996, 
    51996, 41982, 41982, 41985, 41977, 11977, 31977, 41977, 41977, 
    41977, 41999, 41983, 41982, 41982, 41982, 41989, 42004, 42004, 
    42004, 41996, 21996, 51996, 41996, 51996, 31984, 41972, 41972, 
    41975, 41983, 41983, 41983, 41983, 41983, 31983, 41987, 41987, 
    41987, 41987, 41979, 31993, 41999, 41999, 31989, 42004, 42004, 
    21970, 42004, 42001, 42001, 42001, 42001, 32001, 42001, 42001, 
    11992, 31994, 31994, 51980), time_period = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L), .Label = c("<10 years", "10-20 years", ">20 years"
    ), class = "factor")), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -200L))

最佳答案

尝试在数据集中添加一个新列,根据 time_period 分配形状值,然后将 geom_point 的形状美学设置到该新列并添加 scale_shape_identity() 以便 ggplot 知道形状列的值已经代表美学

  df %>% 
      mutate(shape = 
               case_when(
                 time_period == "<10 years" ~ 2,
                 time_period == "10-20 years" ~ 1,
                 time_period == ">20 years" ~ 16
               )) %>% 
      ggplot() +
      geom_linerange( aes(ymin = minyear, ymax = maxyear, 
                          colour = factor(diet),
                          x = fct_reorder(factor(id), desc(sort))),
                      size = 1) +
      geom_point(aes(x = fct_reorder(factor(id), desc(sort)), 
      y = max(maxyear) + 1, 
      shape = shape), 
      show.legend = TRUE, size = 1) +
      scale_shape_identity() +
      scale_colour_manual(values = wes_palette("Cavalcanti1")) +
      labs(x = NULL, y = NULL) +
      theme_bw() +
      coord_flip() +
      guides(colour = F)

获取图例的解决方法:

afafa %>% 
  ggplot() +
  geom_linerange( aes(ymin = minyear, ymax = maxyear, 
                      colour = factor(diet),
                      x = fct_reorder(factor(id), desc(sort))),
                  size = 1) +
  geom_point(aes(x = fct_reorder(factor(id), desc(sort)), 
                 y = max(maxyear) + 1, 
                 shape = time_period), 
             show.legend = TRUE, size = 1) +
  scale_shape_manual(values = c("<10 years" = 2, "10-20 years" = 1, ">20 years" = 16)) +
  labs(x = NULL, y = NULL) +
  theme_bw() +
  coord_flip() +
  guides(colour = F)

关于r - 定义在 ggplot2 中与多个 geom 一起使用时用于 geom_point 的形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57216054/

相关文章:

r - ggplot2-如何在离散刻度轴值中绘制连续值?

r - ggplot2 geom_line() 应该指向指定的值

R 为 fn 和 gr 优化相同的函数

R:使用 rgl 封装减少眩光

r - 当行包含相同字符时删除列

r - 使 R Markdown 输出可访问

python - 具有可变宽度元素的堆​​积条形图?

r - ggplotly 和 lubridate : Hoover shows seconds, 不是分钟

r - 序列覆盖 - 需要解决的难题

r - 如何将刻度标签移近轴?