简体   繁体   English

使用 mean_cl_boot 获取 stat_summary 计算的值

[英]Getting the values calculated by stat_summary with mean_cl_boot

I'm plotting some X values with mean_cl_boot with large confidence intervals我正在用mean_cl_boot绘制一些具有较大置信区间的 X 值

How can I export the text for both the value of the fun.y = mean and fun.data = mean_cl_boot in each group?如何导出每个组中fun.y = meanfun.data = mean_cl_boot的值的文本?

I have an interval of values in mean_cl_boot , and I would like to plot them and export them.我在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)

I got to plot the mean ( fun.y = mean ) value, with:我必须绘制平均值( fun.y = mean )值,其中:

stat_summary(fun.y=mean, geom="text", aes(label=sprintf("%1.1f", ..y..)),size=3, show.legend=FALSE

But I can't to the same with mean_cl_boot .但我不能和mean_cl_boot

You can get access to the data of stat_summary with ggplot_build .您可以使用ggplot_build访问stat_summary的数据。

First, store your ggplot call in an object:首先,将您的 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)

Then, with:然后,与:

ggplot_build(g)$data[[3]]

You get the values calculated with mean_cl_boot :您将获得使用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

For getting the labels right, you could do:为了获得正确的标签,您可以执行以下操作:

# 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)

the result:结果:

在此处输入图片说明

But probably an even better alternative is using the package:但可能更好的选择是使用包:

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)

the result of that:结果:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM