
我需要一些幫助來使用以下 R 腳本來估算標準誤差:
library(ggplot2)
library(ggpubr)
library(Hmisc)
data("ToothGrowth")
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
head(ToothGrowth, 4)
theme_set(
theme_classic()
theme(legend.position = "top")
)
# Initiate a ggplot
e <- ggplot(ToothGrowth, aes(x = dose, y = len))
# Add mean points /- SD
# Use geom = "pointrange" or geom = "crossbar"
e geom_violin(trim = FALSE)
stat_summary(
fun.data = "mean_sdl", fun.args = list(mult = 1),
geom = "pointrange", color = "black"
)
# Combine with box plot to add median and quartiles
# Change fill color by groups, remove legend
e geom_violin(aes(fill = dose), trim = FALSE)
geom_boxplot(width = 0.2)
scale_fill_manual(values = c("#00AFBB", "#E7B800", "#FC4E07"))
theme(legend.position = "none")
非常感謝您的幫助 親切的問候
uj5u.com熱心網友回復:
幾件事。首先,您需要e在添加geom_violin和時重新分配stat_summary。否則,當您在下一步中添加箱線圖時,它不會繼續進行這些更改。其次,當您最后添加箱線圖時,它會映射到點和誤差條上,stat_summary因此看起來它們正在消失。如果您先添加箱線圖,然后stat_summary點和誤差線將放置在箱線圖的頂部。下面是一個例子:
library(ggplot2)
library(ggpubr)
library(Hmisc)
data("ToothGrowth")
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
theme_set(
theme_classic()
theme(legend.position = "top")
)
# Initiate a ggplot
e <- ggplot(ToothGrowth, aes(x = dose, y = len))
# Add violin plot
e <- e geom_violin(trim = FALSE)
# Combine with box plot to add median and quartiles
# Change fill color by groups, remove legend
e <- e geom_violin(aes(fill = dose), trim = FALSE)
geom_boxplot(width = 0.2)
scale_fill_manual(values = c("#00AFBB", "#E7B800", "#FC4E07"))
theme(legend.position = "none")
# Add mean points /- SE
# Use geom = "pointrange" or geom = "crossbar"
e
stat_summary(
fun.data = "mean_se", fun.args = list(mult = 1),
geom = "pointrange", color = "black"
)
您在評論中說,當您嘗試mean_se和時,您看不到任何更改mean_cl_normal。也許上述解決方案已經解決了問題,但您應該會看到不同之處。這是一個僅比較mean_se和的示例mean_sdl。您應該注意到誤差線更小了mean_se。
ggplot(ToothGrowth, aes(x = dose, y = len))
stat_summary(
fun.data = "mean_sdl", fun.args = list(mult = 1),
geom = "pointrange", color = "black"
)
ggplot(ToothGrowth, aes(x = dose, y = len))
stat_summary(
fun.data = "mean_se", fun.args = list(mult = 1),
geom = "pointrange", color = "black"
)
如果您不想在每一步重新分配,這里有一個簡化的解決方案:
ggplot(ToothGrowth, aes(x = dose, y = len))
geom_violin(aes(fill = dose), trim = FALSE)
geom_boxplot(width = 0.2)
stat_summary(fun.data = "mean_se", fun.args = list(mult = 1),
geom = "pointrange", color = "black")
scale_fill_manual(values = c("#00AFBB", "#E7B800", "#FC4E07"))
theme(legend.position = "none")
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/394499.html
上一篇:在R中的圖表中設定列寬
