英文:
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<-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)
and then plot it
ggplot(abc)+
geom_boxplot(aes(x=group, y=value))+
geom_point(aes(x=group, y=value, color=special))
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
)
英文:
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
)
答案2
得分: 2
这种方法将一个文本几何对象添加到每个组的最大值上,label
是 special
变量。
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')
创建于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<-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')
<!-- -->
<sup>Created on 2023-07-12 with reprex v2.0.2</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论