簡體   English   中英

使用 mean_cl_boot 獲取 stat_summary 計算的值

[英]Getting the values calculated by stat_summary with mean_cl_boot

我正在用mean_cl_boot繪制一些具有較大置信區間的 X 值

如何導出每個組中fun.y = meanfun.data = mean_cl_boot的值的文本?

我在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)

結果:

在此處輸入圖片說明

但可能更好的選擇是使用包:

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)

結果:

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM