获取由 stat_summary 和 mean_cl_boot 计算的值
Getting the values calculated by stat_summary with mean_cl_boot
我正在用 mean_cl_boot
绘制一些具有较大置信区间
的 X 值
如何导出每个组中 fun.y = mean
和 fun.data = mean_cl_boot
值的文本?
我在 mean_cl_boot
中有一个值区间,我想绘制它们并导出它们。
ggplot(iris, aes(x = Species, y = Petal.Length)) +
geom_jitter(width = 0.5) + stat_summary(fun.y = mean, geom = "point", color = "red") +
stat_summary(fun.data = mean_cl_boot, fun.args=(conf.int=0.9999), geom = "errorbar", width = 0.4)
我必须绘制平均值 (fun.y = mean
),其中:
stat_summary(fun.y=mean, geom="text", aes(label=sprintf("%1.1f", ..y..)),size=3, show.legend=FALSE
但是我不能和mean_cl_boot
一样。
您可以使用 ggplot_build
访问 stat_summary
的数据。
首先,将您的 ggplot 调用存储在一个对象中:
g <- ggplot(iris, aes(x = Species, y = Petal.Length)) +
geom_jitter(width = 0.5) +
stat_summary(fun.y = mean, geom = "point", color = "red") +
stat_summary(fun.data = mean_cl_boot, fun.args=(conf.int=0.9999), geom = "errorbar", width = 0.4)
然后,用:
ggplot_build(g)$data[[3]]
您将获得使用 mean_cl_boot
:
计算的值
x group y ymin ymax PANEL xmin xmax colour size linetype width alpha
1 1 1 1.462 1.386000 1.543501 1 0.8 1.2 black 0.5 1 0.4 NA
2 2 2 4.260 4.024899 4.462202 1 1.8 2.2 black 0.5 1 0.4 NA
3 3 3 5.552 5.337199 5.798202 1 2.8 3.2 black 0.5 1 0.4 NA
要正确设置标签,您可以这样做:
# extract the data
mcb <- ggplot_build(g)$data[[3]]
# add the labels to the plot
g + geom_text(data = mcb,
aes(x = group, y = ymin, label = round(ymin,2)),
color = "blue",
vjust = 1)
结果:
但可能更好的选择是使用 ggrepel 包:
library(ggrepel)
g + geom_label_repel(data = mcb,
aes(x = group, y = ymin, label = round(ymin,2)),
color = "blue",
nudge_x = 0.2,
nudge_y = -0.2)
结果:
我正在用 mean_cl_boot
绘制一些具有较大置信区间
如何导出每个组中 fun.y = mean
和 fun.data = mean_cl_boot
值的文本?
我在 mean_cl_boot
中有一个值区间,我想绘制它们并导出它们。
ggplot(iris, aes(x = Species, y = Petal.Length)) +
geom_jitter(width = 0.5) + stat_summary(fun.y = mean, geom = "point", color = "red") +
stat_summary(fun.data = mean_cl_boot, fun.args=(conf.int=0.9999), geom = "errorbar", width = 0.4)
我必须绘制平均值 (fun.y = mean
),其中:
stat_summary(fun.y=mean, geom="text", aes(label=sprintf("%1.1f", ..y..)),size=3, show.legend=FALSE
但是我不能和mean_cl_boot
一样。
您可以使用 ggplot_build
访问 stat_summary
的数据。
首先,将您的 ggplot 调用存储在一个对象中:
g <- ggplot(iris, aes(x = Species, y = Petal.Length)) +
geom_jitter(width = 0.5) +
stat_summary(fun.y = mean, geom = "point", color = "red") +
stat_summary(fun.data = mean_cl_boot, fun.args=(conf.int=0.9999), geom = "errorbar", width = 0.4)
然后,用:
ggplot_build(g)$data[[3]]
您将获得使用 mean_cl_boot
:
x group y ymin ymax PANEL xmin xmax colour size linetype width alpha 1 1 1 1.462 1.386000 1.543501 1 0.8 1.2 black 0.5 1 0.4 NA 2 2 2 4.260 4.024899 4.462202 1 1.8 2.2 black 0.5 1 0.4 NA 3 3 3 5.552 5.337199 5.798202 1 2.8 3.2 black 0.5 1 0.4 NA
要正确设置标签,您可以这样做:
# extract the data
mcb <- ggplot_build(g)$data[[3]]
# add the labels to the plot
g + geom_text(data = mcb,
aes(x = group, y = ymin, label = round(ymin,2)),
color = "blue",
vjust = 1)
结果:
但可能更好的选择是使用 ggrepel 包:
library(ggrepel)
g + geom_label_repel(data = mcb,
aes(x = group, y = ymin, label = round(ymin,2)),
color = "blue",
nudge_x = 0.2,
nudge_y = -0.2)
结果: