定义在ggplot2中与多个几何一起使用时要为geom_point使用的形状

索伦

之前,我曾在将点添加到线图,geom_point结合使用方面有所帮助,geom_linerange创建所需的精确效果(请参见此处的问题)。

带有数据集的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帖子中建议的各种选项,但是没有一个起作用,包括:

  • 使用[time_period == "<10 years"]etc仅使用组制作单独的geom_point图
  • 使用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))
巴勃罗·罗德(Pablo Rod)

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

  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)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

ggplot2:将geom_line和geom_point组合在一起的图形中的坐标图例

将 geom_line 和 geom_point 与 ggplotly 一起使用时的奇怪图例和工具提示文本

ggplot2:将geom_dotplot与facet_grid一起使用时,没有自由轴缩放

矩形ggplot2 geom_point形状

如何在ggplot2中将stat与geom_violin一起使用

ggplot2:一些geom_point形状导致颜色奇怪

在geom_point(而非标签)中增加气泡大小。使用ggplot2 + gganimate + ggrepel

ggplot中的自定义形状(geom_point)

ggplot2中不同的geom_point形状取决于条件

ggplot2:如何将geom_line和geom_point的图层与错误栏组合在一起

在ggplot2中与其他美学一起使用时,填充图例不会更新吗?

将gganimate与geom_point和geom_line一起使用

将 geom_point 与 geom_col 和 faceting 一起使用

ggplot2 | 只为一个变量自定义 geom_point

从图例中删除ggplot2的geom_point图标

ggplot2 2.0.0中的geom_point边界

ggplot2 中的振幅线 + geom_point

geom_point {ggplot2}中的alpha值

如何将ggplot2的geom_dotplot()与对称隔开的点一起使用

在ggplot2中使用泡泡换条形图(geom_point)创建条形图

使用geom_point()和geom_line()进行多个系列的ggplot中的图例错误

使用 ggplot2 geom_point () 绘制二元(存在/不存在)数据

如何制作具有多个几何形状的自定义ggplot2几何图形

使用facet_grid和多个数据点(R ggplot2)时,将geom_point和geom_line组合

ggplot2:geom_point() 躲避形状但不是颜色

在ggplot2中使用geom_smooth()和geom_point()时显示带有日期的x轴标签

如何將 ggplot2 geom_col 與 facet_grid 和 coord_flip 一起使用

ggplot2中带有geom_line和geom_point的图例

為 ggplot2 中的特定 geom_area 添加 geom_point