對于每種處理tmt
,我想在 ggplot2 中使用不同顏色大小的 stat_summary 繪制均值。我發現在當前點上繪制了多種方法。不知道如何糾正它。
df <- data.frame(x = rnorm(12, 4,1), y = rnorm(12, 6,4), tmt = rep(c("A","B","C"), each = 4))
ggplot(aes(x = x, y = y, fill = tmt), data = df)
geom_point(shape=21, size=5, alpha = 0.6)
scale_fill_manual(values=c("pink","blue", "purple"))
stat_summary(aes(fill = tmt), fun = 'mean', geom = 'point', size = 5)
scale_fill_manual(values=c("pink","blue", "purple"))
沒有最后兩行代碼的繪圖
繪制整個代碼
uj5u.com熱心網友回復:
使用stat_summary
您計算y
每對x
和的平均值tmt
。如果你想要 x 的平均值和 y 的平均值,tmt
我建議在 ggplot 之外手動計算平均值并使用第二個geom_point
來繪制平均值。在下面的代碼中,我增加了大小并使用矩形作為手段:
df <- data.frame(x = rnorm(12, 4,1), y = rnorm(12, 6,4), tmt = rep(c("A","B","C"), each = 4))
library(ggplot2)
library(dplyr)
df_mean <- df |>
group_by(tmt) |>
summarise(across(c(x, y), mean))
ggplot(aes(x = x, y = y, fill = tmt), data = df)
geom_point(shape=21, size=5, alpha = 0.6)
geom_point(data = df_mean, shape=22, size=8, alpha = 0.6)
scale_fill_manual(values=c("pink","blue", "purple"))
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/498309.html