繁体   English   中英

R - 如何使用 ggplot2 为我的散点图的最大值和最小值点着色和 label?

[英]R - How can I color and label the max and min values points of my scatterplot using ggplot2?

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

   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()并且我想制作一个散点 plot ,其中ENE的最大值和最小值分别以红色和蓝色着色,除了我想要 label 这两个点及其各自的FechaENE值(一个低于另一个)。

这是我最好的尝试:

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)

散点 plot

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

有几种方法可以实现这一点,具体取决于您要生成的 output。 ggplot2的情况下,您可以使用geom_textgeom_label 如果您想要显示两条数据,我想说的最好方法是首先将它们组合起来,但是您可以对 function 进行两次调用,将每列作为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
      )
   )

The geom_label function can be used to replace the point with the actual label, while the geom_text function adds the text to the plot. 默认情况下,文本将使用数据中的原始xy坐标定位,因此请务必轻推它。

你快到了。 我建议使用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))

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM