【问题标题】:R - How can I color and label the max and min values points of my scatterplot using ggplot2?R - 如何使用 ggplot2 对散点图的最大值和最小值点进行着色和标记?
【发布时间】:2021-02-16 06:36:32
【问题描述】:

我有一个简单的 data.frame (piez_df) 有 2 列 (Fecha Vs E.N.E.):

   Fecha  E.N.E.
1   1993 1871.40
2   1994      NA
3   1995 1869.40
4   1996 1866.85
5   1997 1857.94
6   1998 1849.43
7   1999 1849.59
8   2000 1858.61
9   2001 1850.30
10  2002 1852.50
11  2003      NA
12  2004 1850.70
13  2005      NA
14  2006 1850.80
15  2007 1850.82
16  2008      NA
17  2009 1832.30
18  2010      NA
19  2011      NA
20  2012      NA
21  2013      NA
22  2014      NA
23  2015      NA
24  2016      NA
25  2017      NA
26  2018 1809.30
27  2019 1809.30
28  2020 1808.70

我正在使用ggplot(),我想制作一个散点图,其中E.N.E. 的最大值和最小值分别用红色和蓝色着色,除了我想用各自的Fecha 和标记这两个点E.N.E. 值(一个低于另一个)。

这是我最好的尝试:

ggplot(data = piez_df, aes(x = Fecha, y = E.N.E.)) +
      geom_point(color="black", size=2) +
      geom_point(data = piez_df[which.min(piez_df$E.N.E.), ], color="blue", 
                 size=3) +
      geom_point(data = piez_df[which.max(piez_df$E.N.E.), ], color="red", 
                 size=3)

Scatter plot

我是 R 新手,已经花了几天时间想知道该怎么做,但仍然无法按照我想要的方式添加标签。希望有人可以帮助我。

【问题讨论】:

    标签: r label scatter-plot points minimax


    【解决方案1】:

    你快到了。我建议使用geom_text() 添加文本如下:

    cars$lab <- paste(cars$speed,cars$dist, sep="\n")
    
    ggplot(data = cars, aes(x = speed, y = dist)) +
        geom_point(color="black", size=2) +
        geom_point(data = cars[which.min(cars$dist), ], color="blue", 
                   size=3) +
        geom_point(data = cars[which.max(cars$dist), ], color="red", 
                   size=3) +
        geom_text(data = rbind(cars[which.min(cars$dist), ], cars[which.max(cars$dist),]), aes(speed+1,dist, label=lab))
    

    【讨论】:

    • 非常感谢@pascal。这对我有用,我只需将geom_text() 更改为geom_text_repel() 即可调整标签位置。我还有很多东西要学。你知道如何编辑这部分:paste(cars$speed,cars$dist, sep="\n"),所以我可以在每个值之后添加单位,避免线路制动,例如(paste(cars$speed+"mph",cars$dist+"mi", sep="\n"))。
    • 不用想太多,一个简单的解决方案就是使用嵌套粘贴命令:cars$lab &lt;- paste(paste(cars$speed,"mph"),paste(cars$dist,"mi"), sep="\n")
    【解决方案2】:

    有几种方法可以实现这一点,具体取决于您要生成的输出。对于ggplot2,您可以使用geom_textgeom_label。如果您希望显示两条数据,我想说的最好方法是首先将它们组合起来,但是您可以对任一函数进行两次调用,将每一列作为label

    library(magrittr)
    
    piez_df <- tibble::tribble(
       ~year, ~ene,
       1993, 1871.40,
       1994, NA,
       1995, 1869.4,
       1996, 1866.85,
       1997, 1857.94,
       1998, 1849.43,
       1999, 1849.59,
       2000, 1858.61,
       2001, 1850.30,
       2002, 1852.5,
       2003, NA,
       2004, 1850.7
    ) %>% 
       dplyr::mutate(
          plot_text = glue::glue("{year}\n{ene}")
       )
    
    ggplot2::ggplot(
       data = piez_df,
       ggplot2::aes(
          x = year,
          y = ene
       )
    ) +
       ggplot2::geom_point(
          color = "black",
          size = 2
       ) +
       ggplot2::geom_point(
          data = piez_df[base::which.min(piez_df[["ene"]]),],
          color = "blue",
          size = 3
       ) +
       ggplot2::geom_point(
          data = piez_df[base::which.max(piez_df[["ene"]]), ],
          color = "red",
          size = 3
       ) +
       ggplot2::geom_text(
          data = piez_df[base::which.min(piez_df[["ene"]]),],
          ggplot2::aes(
             label = plot_text
          ),
          check_overlap = TRUE,
          nudge_y = 2
       ) +
       ggplot2::geom_label(
          data = piez_df[base::which.max(piez_df[["ene"]]),],
          ggplot2::aes(
             label = plot_text
          )
       )
    

    geom_label 函数可用于将点替换为实际标签,而geom_text 函数将文本添加到绘图中。默认情况下,文本将使用数据中的原始xy 坐标定位,因此请务必轻推它。

    【讨论】:

    • 非常感谢您的回答@Daniel。我运行了你的代码并得到了一个可以接受的情节,这与我以前的情节不同,因为tribble() 内部缺少数据,正如你所说,它需要轻推它。为什么我必须使用tribble() 并用不同的列名(~year~ene)重新定义我以前的数据框?因为我必须为很多不同的样本做这种图,所以我想最后避免这种情况,并使用我以前的数据框和列名。有可能吗?
    • @JorgeMona 你不需要重新定义你的df。我根据您发布的原始图像创建了一个。我只是一个tribble,因为我打算一次手动输入一个值。至于您对列名的问题,没有必须遵循的标准,但对于每种语言,都有社区倾向于遵循的样式指南。你可以阅读所有关于 R 的 tidyverse 风格指南 here
    • 请记住,df 中的列名称不一定会显示在您的绘图或表格中。例如,如果您希望标签显示列的格式化名称,您可以使用 df %&gt;% dplyr::mutate(plot_text = glue::glue("Fecha: {year}\nE.N.E: {ene}"))
    猜你喜欢
    • 1970-01-01
    • 2020-10-28
    • 1970-01-01
    • 2013-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-01
    • 2014-08-07
    相关资源
    最近更新 更多