在ggplot中突出显示具有最高值的因子。

huangapple go评论87阅读模式
英文:

highlight factor with highest value in ggplot R

问题

让我们模拟一些数据:

library(stringi)
set.seed(12)

value <- round(rnorm(25, 5, 5))
group <- rep(LETTERS[1:5], 5)
special <- stri_rand_strings(25, 5, pattern = "[A-Z,]")
abc <- cbind.data.frame(group, value, special)

然后绘制图表:

ggplot(abc) +
  geom_boxplot(aes(x = group, y = value)) +
  geom_point(aes(x = group, y = value, color = special))

我希望在每个“group”的点旁边显示具有每个“group”中最大“value”的“special”列的标签,就像图像上绘制的那样。

谢谢您的时间!

英文:

Lets simulate some data:

library(stringi)
set.seed(12)

value&lt;-round(rnorm(25,5,5))
group&lt;-rep(LETTERS[1:5],5)
special&lt;-stri_rand_strings(25, 5, pattern = &quot;[A-Z,]&quot;)
abc&lt;-cbind.data.frame(group, value,special)

and then plot it

ggplot(abc)+
  geom_boxplot(aes(x=group, y=value))+
  geom_point(aes(x=group, y=value, color=special))

在ggplot中突出显示具有最高值的因子。

I would like to have labels for the "special" column where "special" has max "value" per group displayed next to the points for each "group", as drawn on the image.

Thank you for your time!

答案1

得分: 4

一种选择是按组筛选数据以获取最大值。然后,可以在 geom_text 中使用这个筛选后的数据集。对于“筛选”,您可以使用例如 dplyr::slice_max

library(ggplot2)
library(dplyr)

ggplot(abc) +
  geom_boxplot(aes(x = group, y = value)) +
  geom_point(aes(x = group, y = value, color = special)) +
  geom_text(
    data = dplyr::slice_max(abc, value, n = 1, by = group),
    aes(x = group, y = value, label = special), vjust = 0, nudge_y = .25
  )

在ggplot中突出显示具有最高值的因子。

英文:

One option would be to filter your data for the max value per group. This filtered dataset can then be used in geom_text. For the "filtering" you could e.g. use dplyr::slice_max:

library(ggplot2)
library(dplyr)

ggplot(abc) +
  geom_boxplot(aes(x = group, y = value)) +
  geom_point(aes(x = group, y = value, color = special)) +
  geom_text(
    data = dplyr::slice_max(abc, value, n = 1, by = group),
    aes(x = group, y = value, label = special), vjust = 0, nudge_y = .25
  )

在ggplot中突出显示具有最高值的因子。

答案2

得分: 2

这种方法将一个文本几何对象添加到每个组的最大值上,labelspecial 变量。

library(stringi)
library(tidyverse)
set.seed(12)

value <- round(rnorm(25, 5, 5))
group <- rep(LETTERS[1:5], 5)
special <- stri_rand_strings(25, 5, pattern = "[A-Z,]")
abc <- cbind.data.frame(group, value, special)
glimpse(abc)
#> Rows: 25
#> Columns: 3
#> $ group   <chr> "A", "B", "C", "D", "E", "A", "B", "C", "D", "E", "A", "B", "C…
#> $ value   <dbl> -2, 13, 0, 0, -5, 4, 3, 2, 4, 7, 1, -1, 1, 5, 4, 1, 11, 7, 8, …
#> $ special <chr> "JIKHN", "ZOOQV", "TKZOG", "DCTIT", "HSPJH", "YURDV", "NWCFS",…

ggplot(abc) +
  geom_boxplot(aes(x = group, y = value)) +
  geom_point(aes(x = group, y = value, color = special)) +
  geom_text(data = abc %>%
              filter(value == max(value), .by = group),
            aes(label = special,
                x = group,
                y = value),
            nudge_y = 0.5
  ) +
  theme(legend.position = 'none')

在ggplot中突出显示具有最高值的因子。

创建于2023年7月12日,使用 reprex v2.0.2

英文:

This approach adds a text geom to the max value per group, and label is the special variable

library(stringi)
library(tidyverse)
set.seed(12)

value&lt;-round(rnorm(25,5,5))
group&lt;-rep(LETTERS[1:5],5)
special&lt;-stri_rand_strings(25, 5, pattern = &quot;[A-Z,]&quot;)
abc&lt;-cbind.data.frame(group, value,special)
glimpse(abc)
#&gt; Rows: 25
#&gt; Columns: 3
#&gt; $ group   &lt;chr&gt; &quot;A&quot;, &quot;B&quot;, &quot;C&quot;, &quot;D&quot;, &quot;E&quot;, &quot;A&quot;, &quot;B&quot;, &quot;C&quot;, &quot;D&quot;, &quot;E&quot;, &quot;A&quot;, &quot;B&quot;, &quot;C…
#&gt; $ value   &lt;dbl&gt; -2, 13, 0, 0, -5, 4, 3, 2, 4, 7, 1, -1, 1, 5, 4, 1, 11, 7, 8, …
#&gt; $ special &lt;chr&gt; &quot;JIKHN&quot;, &quot;ZOOQV&quot;, &quot;TKZOG&quot;, &quot;DCTIT&quot;, &quot;HSPJH&quot;, &quot;YURDV&quot;, &quot;NWCFS&quot;,…

ggplot(abc)+
  geom_boxplot(aes(x=group, y=value))+
  geom_point(aes(x=group, y=value, color=special)) +
  geom_text(data = abc %&gt;% filter(value == max(value), .by = group),
            aes(label = special,
                x = group,
                y = value),
            nudge_y = 0.5
            ) +
  theme(legend.position = &#39;none&#39;)

在ggplot中突出显示具有最高值的因子。<!-- -->

<sup>Created on 2023-07-12 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年7月12日 20:54:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76670804.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定