2017-12-28 35 views
1

2つの要因を持つboxplotに要約統計量(例えば平均値)を追加したいと思います。私はこれを試してみました:何らかの形で各ボックス(ウィスカーの例えばトップ)のサンプルサイズを置くことも可能である2つの要因boxplotに要約統計量を追加する

enter image description here

:私はこの絵を持って

library(ggplot2) 
ggplot(ToothGrowth, aes(x = factor(dose), y = len)) + 
    stat_boxplot(geom = "errorbar", aes(col = supp, fill=supp), position = position_dodge(width = 0.85)) + 
    geom_boxplot(aes(col = supp, fill=supp), notch=T, notchwidth = 0.5, outlier.size=2, position = position_dodge(width = 0.85)) + 
    stat_summary(fun.y=mean, aes(supp,dose), geom="point", shape=20, size=7, color="violet", fill="violet") + 
    scale_color_manual(name = "SUPP", values = c("blue", "darkgreen")) + 
    scale_fill_manual(name = "SUPP", values = c("lightblue", "green")) 

?私はこれを試してみました:

ggplot(ToothGrowth, aes(x = factor(dose), y = len)) + 
    stat_boxplot(geom = "errorbar", aes(col = supp, fill=supp), position = position_dodge(width = 0.85)) + 
geom_boxplot(aes(col = supp, fill=supp), notch=T, notchwidth = 0.5, outlier.size=2, position = position_dodge(width = 0.85)) + 
stat_summary(fun.y=mean,aes(supp,dose),geom="point", shape=20, size=7, color="violet", fill="violet") + 
scale_color_manual(name = "SUPP", values = c("blue", "darkgreen")) + 
scale_fill_manual(name = "SUPP", values = c("lightblue", "green")) + 
geom_text(data = ToothGrowth, 
      group_by(dose, supp), 
      summarize(Count = n(), 
        q3 = quantile(ToothGrowth, 0.75), 
        iqr = IQR(ToothGrowth), 
        aes(x= dose, y = len,label = paste0("n = ",Count, "\n")), position = position_dodge(width = 0.75))) 

答えて

1

あなたがメインggplot呼び出しでそれらを置くことによって一度だけ美学を述べることができ、その後、彼らはGEOM層のすべてに適用されます:観測値の数についてggplot(ToothGrowth, aes(x = factor(dose), y = len, color=supp, fill=supp))

geom_textのデータサマリーステップは正しくコーディングされていません。また、テキスト配置のlen(y値)を設定するには、関数をlenの値で出力する必要があります。

x軸の正しい位置に平均値を加算するには、stat_summaryを他の幾何学および統計と全く同じ美しさで使用します。色を黄色に設定して、点のマーカーをボックスの塗りつぶしの色の上に表示するように、色の美学をオーバーライドしました。

プロットを実装するコードは以下の通りです:

library(tidyverse) 

pd = position_dodge(0.85) 

ggplot(ToothGrowth, aes(x = factor(dose), y = len, color=supp, fill=supp)) + 
    stat_boxplot(geom = "errorbar", position = pd) + 
    geom_boxplot(notch=TRUE, notchwidth=0.5, outlier.size=2, position=pd) + 
    stat_summary(fun.y=mean, geom="point", shape=3, size=2, colour="yellow", stroke=1.5, 
       position=pd, show.legend=FALSE) + 
    scale_color_manual(name = "SUPP", values = c("blue", "darkgreen")) + 
    scale_fill_manual(name = "SUPP", values = c("lightblue", "green")) + 
    geom_text(data = ToothGrowth %>% group_by(dose, supp) %>% 
       summarize(Count = n(), 
         len=max(len) + 0.05 * diff(range(ToothGrowth$len))), 
      aes(label = paste0("n = ", Count)), 
      position = pd, size=3, show.legend = FALSE) + 
    theme_bw() 

enter image description here

注ノッチは、ボックスプロットのすべてのためのヒンジの外に行くことに。また、各ボックスプロットの最大値をわずかに上回るサンプルサイズを持つことは、気を散らし、私には不要です。すべてのテキスト注釈をプロットの下部に次のように配置できます。

geom_text(data = ToothGrowth %>% group_by(dose, supp) %>% 
       summarize(Count = n()) %>% 
       ungroup %>% 
       mutate(len=min(ToothGrowth$len) - 0.05 * diff(range(ToothGrowth$len))), 
      aes(label = paste0("n = ", Count)), 
      position = pd, size=3, show.legend = FALSE) + 
関連する問題