我有一個情節,我已將每個組的方面分開。在 Y 軸的最高點我想放置相應的 Y 值,為此我使用了ggtext包和geom_richtext()函式。雖然我group = Group在geom_richtext()函式中使用,但標簽并沒有像我預期的那樣出現。
在 MWE 中,我希望 A、B 和 C 組的方面分別具有 100、500 和 1000 的值,出現在每個峰的頂部。有人可以幫我提出建議嗎?
library(ggplot2)
library(ggtext)
Group <- c("A", "B", "C")
Time <- 0:10
DF <- expand.grid(Time = Time,
Group = Group)
DF$Y <- c(rep(1,5), 100, rep(1,5),
rep(1,5), 500, rep(1,5),
rep(1,5), 1000, rep(1,5))
ggplot(data = DF,
aes(x = Time,
y = Y))
geom_line()
facet_grid(Group ~ .)
geom_richtext(aes(label = max(Y),
group = Group))
theme_bw()

uj5u.com熱心網友回復:
一種選擇是傳遞一個過濾的資料集,geom_richtext其中只包含 per 的最大值Group:
library(ggplot2)
library(ggtext)
library(dplyr)
ggplot(data = DF,
aes(x = Time,
y = Y))
geom_line()
facet_grid(Group ~ .)
geom_richtext(data = DF %>% group_by(Group) %>% slice_max(Y, n = 1), aes(label = Y,
group = Group))
theme_bw()

轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/397795.html
上一篇:使用R在指定行數后插入NA
下一篇:為成對t檢驗撰寫函式
