我正在嘗試根據條件更改顏色 geom_label_repel。例如:
res <- data.frame(group = c("Group 1", "Group 2", "Group 3", "Group 4", "Group 5"),
correlation = c(0.37,0.33,0.31,0.30,0.30))
res <- res %>%
arrange(desc(correlation))
res
group correlation
1 Group 1 0.37
2 Group 2 0.33
3 Group 3 0.31
4 Group 4 0.30
5 Group 5 0.30
我想標記具有最高相關值的三個組。然后如果后續組之間的差異> 0.03,我想更改具有最高相關值的點的顏色。我的代碼現在給了我這個情節
p <- res %>%
ggplot(aes(x = reorder(group, -correlation), y = correlation))
geom_point()
geom_label_repel(data = slice_max(res, n = 3, correlation),
aes(label = group))

現在,我想更改第 1 組標簽的顏色,因為它的相關值比第 2 組的相關值 > 0.03。任何幫助,將不勝感激
uj5u.com熱心網友回復:
我們可以通過根據條件添加幫助列來做到這一點:
library(tidyverse); library(ggrepel)
res <- res %>%
arrange(desc(correlation)) %>%
mutate(my_col = if_else(
row_number() == 1 & correlation > lead(correlation) 0.03,
"highlight",
"other"
))
res %>%
ggplot(aes(x = group, y = correlation, color = my_col))
geom_point()
geom_label_repel(data = slice_max(res, n = 3, correlation),
aes(label = group))
scale_color_manual(values = c("highlight" = "red", "other" = "black"))
guides(color = "none") # optional, hides legend

轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/471612.html
上一篇:在x軸下方添加箱線圖
