Geom_errorbar 在 ggplot 中没有显示正确的标准错误
Geom_errorbar in ggplot doesn't show the correct standard error
我想在我的 geom_col ggplot 中添加错误栏。错误栏确实添加到我的情节中,但错误栏不正确。有人可以帮我弄清楚如何调整我的代码吗?我认为 y 值是错误的。它可能不是长度,而是我的数据框中“数字”的实际值。
如果这是一个非常简单的问题,我深表歉意,我是 R 的新手。
提前谢谢你。
number_of_sprout <- data.frame(
Condition = c("RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl"),
Number = c(8, 11, 11, 17, 18, 13, 16, 4, 11, 9, 12, 11, 15, 11, 17, 8, 18, 9, 7, 11, 11, 12, 8, 12, 6, 8, 10, 19, 9, 14, 7, 9, 12, 11, 8, 12, 6, 8, 10, 8, 11, 11, 10, 7, 6, 6, 6, 9, 11, 10, 11, 12, 8, 14, 7, 8, 11, 9, 9, 12, 10, 7, 13, 17, 15, 9, 14, 11, 11, 8, 14, 13, 7, 8, 6, 7, 9, 7, 10, 7),
timepoint = c(24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48)
)
sph1 <- ggplot(data = number_of_sprout, aes(x = factor(timepoint, ordered = TRUE), y=Number, fill = Condition)) +
stat_summary(fun = mean, geom = "col", position = position_dodge()) +
scale_fill_manual(values = c("slategray4", "thistle3")) +
geom_point(position = position_dodge(width = 0.9)) +
labs(x = "Hours", y = "Number of sprouts") +
theme_classic() +
ggtitle("Spheroids - Donor 37") +
theme_bw() +
theme(text=element_text(family="Times New Roman", face="bold")) +
theme(aspect.ratio = 1.5/1)
se5 <- function(y) sd(y)/length(y) # to calculate standard error in the mean
sph1+stat_summary(geom="errorbar",position=position_dodge(width=0.9),
fun.data=function(y)c(ymin=mean(y)-se5(y),ymax=mean(y)+se5(y)), width=0.2)
我假设您的均值标准误差公式有误。我把它保存在我的个人实用程序包中,所以我不需要查找它 - 在某些时候厚颜无耻地从 this thread 偷来的。
另外一个注意事项,也许检查 http://data-to-viz.com/caveat/error_bar.html
对于更一般的警告,为什么错误栏可能不理想(如果绘制整个数据,一般来说不是很有必要)
# replace your function
se5 <- function(x, na.rm=FALSE) {
if (na.rm) x <- na.omit(x)
sqrt(var(x)/length(x))
} # to calculate standard error in the mean
sph1+stat_summary(geom="errorbar",position=position_dodge(width=0.9),
fun.data=function(y)c(ymin=mean(y)-se5(y),ymax=mean(y)+se5(y)), width=0.2)
作为不同的答案 - 不同的主题。您是否考虑过配对数据的不同可视化形式?我将其显示为关系散点图,第一个测量值在 x 轴上,第二个测量值在 y 轴上。
如您所见,与条形图相比,这种方式可以更清楚地显示两个测量值之间的关系,并且您还可以更好地看到差异(或本例中的相似性)。
library(tidyverse)
number_of_sprout <- data.frame(
Condition = c("RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl"),
Number = c(8, 11, 11, 17, 18, 13, 16, 4, 11, 9, 12, 11, 15, 11, 17, 8, 18, 9, 7, 11, 11, 12, 8, 12, 6, 8, 10, 19, 9, 14, 7, 9, 12, 11, 8, 12, 6, 8, 10, 8, 11, 11, 10, 7, 6, 6, 6, 9, 11, 10, 11, 12, 8, 14, 7, 8, 11, 9, 9, 12, 10, 7, 13, 17, 15, 9, 14, 11, 11, 8, 14, 13, 7, 8, 6, 7, 9, 7, 10, 7),
timepoint = c(24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48)
)
number_of_sprout %>%
pivot_wider(names_from = timepoint, values_from = Number,
names_prefix = "t", values_fn = list) %>%
unnest(c(t24, t48)) %>%
ggplot(aes(t24, t48)) +
geom_abline(intercept = 0, slope = 1, color = "grey") +
geom_point(aes(color = Condition)) +
scale_color_brewer(palette = "Set1", direction = -1) +
coord_equal(xlim = c(0,NA), ylim = c(0, NA)) +
cowplot::theme_cowplot()
由 reprex package (v2.0.0)
于 2021-04-29 创建
我想在我的 geom_col ggplot 中添加错误栏。错误栏确实添加到我的情节中,但错误栏不正确。有人可以帮我弄清楚如何调整我的代码吗?我认为 y 值是错误的。它可能不是长度,而是我的数据框中“数字”的实际值。 如果这是一个非常简单的问题,我深表歉意,我是 R 的新手。 提前谢谢你。
number_of_sprout <- data.frame(
Condition = c("RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl"),
Number = c(8, 11, 11, 17, 18, 13, 16, 4, 11, 9, 12, 11, 15, 11, 17, 8, 18, 9, 7, 11, 11, 12, 8, 12, 6, 8, 10, 19, 9, 14, 7, 9, 12, 11, 8, 12, 6, 8, 10, 8, 11, 11, 10, 7, 6, 6, 6, 9, 11, 10, 11, 12, 8, 14, 7, 8, 11, 9, 9, 12, 10, 7, 13, 17, 15, 9, 14, 11, 11, 8, 14, 13, 7, 8, 6, 7, 9, 7, 10, 7),
timepoint = c(24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48)
)
sph1 <- ggplot(data = number_of_sprout, aes(x = factor(timepoint, ordered = TRUE), y=Number, fill = Condition)) +
stat_summary(fun = mean, geom = "col", position = position_dodge()) +
scale_fill_manual(values = c("slategray4", "thistle3")) +
geom_point(position = position_dodge(width = 0.9)) +
labs(x = "Hours", y = "Number of sprouts") +
theme_classic() +
ggtitle("Spheroids - Donor 37") +
theme_bw() +
theme(text=element_text(family="Times New Roman", face="bold")) +
theme(aspect.ratio = 1.5/1)
se5 <- function(y) sd(y)/length(y) # to calculate standard error in the mean
sph1+stat_summary(geom="errorbar",position=position_dodge(width=0.9),
fun.data=function(y)c(ymin=mean(y)-se5(y),ymax=mean(y)+se5(y)), width=0.2)
我假设您的均值标准误差公式有误。我把它保存在我的个人实用程序包中,所以我不需要查找它 - 在某些时候厚颜无耻地从 this thread 偷来的。
另外一个注意事项,也许检查 http://data-to-viz.com/caveat/error_bar.html 对于更一般的警告,为什么错误栏可能不理想(如果绘制整个数据,一般来说不是很有必要)
# replace your function
se5 <- function(x, na.rm=FALSE) {
if (na.rm) x <- na.omit(x)
sqrt(var(x)/length(x))
} # to calculate standard error in the mean
sph1+stat_summary(geom="errorbar",position=position_dodge(width=0.9),
fun.data=function(y)c(ymin=mean(y)-se5(y),ymax=mean(y)+se5(y)), width=0.2)
作为不同的答案 - 不同的主题。您是否考虑过配对数据的不同可视化形式?我将其显示为关系散点图,第一个测量值在 x 轴上,第二个测量值在 y 轴上。
如您所见,与条形图相比,这种方式可以更清楚地显示两个测量值之间的关系,并且您还可以更好地看到差异(或本例中的相似性)。
library(tidyverse)
number_of_sprout <- data.frame(
Condition = c("RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "RBP7 KO", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl", "Ctrl"),
Number = c(8, 11, 11, 17, 18, 13, 16, 4, 11, 9, 12, 11, 15, 11, 17, 8, 18, 9, 7, 11, 11, 12, 8, 12, 6, 8, 10, 19, 9, 14, 7, 9, 12, 11, 8, 12, 6, 8, 10, 8, 11, 11, 10, 7, 6, 6, 6, 9, 11, 10, 11, 12, 8, 14, 7, 8, 11, 9, 9, 12, 10, 7, 13, 17, 15, 9, 14, 11, 11, 8, 14, 13, 7, 8, 6, 7, 9, 7, 10, 7),
timepoint = c(24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48)
)
number_of_sprout %>%
pivot_wider(names_from = timepoint, values_from = Number,
names_prefix = "t", values_fn = list) %>%
unnest(c(t24, t48)) %>%
ggplot(aes(t24, t48)) +
geom_abline(intercept = 0, slope = 1, color = "grey") +
geom_point(aes(color = Condition)) +
scale_color_brewer(palette = "Set1", direction = -1) +
coord_equal(xlim = c(0,NA), ylim = c(0, NA)) +
cowplot::theme_cowplot()
由 reprex package (v2.0.0)
于 2021-04-29 创建