2017-09-09 8 views
1

私はreshape2を使って作成したデータを溶かした棒グラフのエラーバーを作成しようとしています。私は以下のエラーメッセージが表示され、本当に理由を理解できません!私がgeom_errorbar行を追加するまで、プロットは素晴らしいです。ここでggplot2メルトデータにエラーバーを追加する - 美学は長さ1または同じ長さでなければなりません

gg <- melt(RootTree, id="Tr", na.rm = T) 
errors = aggregate(. ~ Tr, data=gg, FUN=sd) 
means = aggregate(. ~ Tr, data=gg, FUN=mean) 
x_melt <- melt(means, value.name="Mean") 
y_melt <- melt(errors, value.name="SD") 
datN1 <- merge(x_melt, y_melt) 
ggplot(gg, aes(x=variable, y=value, fill=factor(Tr))) + 
    stat_summary(fun.y=mean, geom="bar",position=position_dodge(0.7)) + 
    scale_color_discrete("Tr") + 
    xlab("Seedlings")+ 
    ylab("Mean of Root Weight (gr)") + 
    scale_fill_manual(values=cbPalette, name="Treatment") + 
    ggtitle(" Root Weight of Seedlings by Treatment") + 
    geom_errorbar(aes(ymin=datN1$Mean-datN1$SD, ymax=datN1$Mean+datN1$SD), width=0.1)` 

Error: Aesthetics must be either length 1 or the same as the data (128): ymin, ymax, x, y, fill

データのdput出力である:あなたのプロットとエラーの異なる2つのデータフレームを使用して

 structure(list(Tr = structure(c(5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
    5L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L), .Label = c("Control", "Gram", "Legu", "Mix", 
    "Topsoil"), class = "factor"), JP = c(2.59, 2.82, 2.8, 4.26, 
5.65, 7.01, 4.51, 3.27, 4.1, 4.73, 2.32, 5.09, NA, NA, NA, 1.47, 
2.26, 1.82, 1.31, 4.94, 2.64, 1.12, 2.51, 3.29, 1.83, 1.56, 3.05, 
1.98, 2.03, 2.95, 2.57, 2.1, 2.19, 2.92, 1.69, 2.24, 1.13, NA, 
NA, 1, 1.37, 1.65, 2.02, 2.47, 1.9), W = c(0.58, 0.4, 0.96, 0.47, 
0.69, 0.71, 0.61, 0.39, 0.34, 0.46, 0.13, 0.28, 0.31, 0.21, 0.14, 
0.65, 0.75, 0.19, 0.74, 1.8, 1.19, 1.74, 1.39, 0.6, 1.36, 1.91, 
0.5, 0.17, 0.25, 0.35, 0.77, 0.75, 0.33, 0.13, 0.14, 0.12, 0.18, 
0.29, 1.71, 1.18, 0.51, 0.99, 0.05, 0.36, NA), Ta = c(5.36, 2.8, 
4.46, 6.86, 2.05, 2.13, 2.94, 2.85, 3.26, 2.07, 2.5, 1.84, 2.07, 
2.5, 1.84, 3.66, 2.61, 1.8, 1.86, 2.89, 2.5, 1.8, 1.69, 1.83, 
4.97, 2.22, 3.93, 3.7, 2.58, 3.16, 4.01, 1.85, 2.91, 3.49, 2.96, 
NA, 1.53, 2.22, 2.53, 1.55, 1.58, 1.99, 0.54, 2.39, 0.85)), .Names = c("Tr", 
"JP", "W", "Ta"), class = "data.frame", row.names = c(NA, -45L 
)) 
+0

より速く、より正確な答えを受け取るためにサンプルデータを入力してください。データのdput()関数を使用して構造体を出力することができます。この構造体をコピーしてあなたの質問に貼り付けて、これを再現することができます。 – www

+0

'aes'で' $ 'を使うのは良い考えです。代わりに 'data'引数を使用してください。テストすることなく確認することはできませんが、 'inherit.aes = FALSE'が役に立ちます –

+0

あなたの両方の迅速な返信をありがとう!私は要求されたようにサンプルデータを追加しました。$の代わりにdata引数を使用することは助けに見えませんでした。 'inherit.aes = FALSE'を追加すると、エラーの単純なエラーになりましたが、美学は長さ1またはデータ(128):ymin、ymax'のいずれかでなければなりません。 – amb

答えて

0

は問題ではありません。問題は、その計算方法です。最初にmeltデータを入力すると128個のエントリが得られ、エラーの場合はTrで集計されて5個のエントリが得られますが、データには5x3のカテゴリがあります。

可能な解決策を以下に示します。

meanserrorsのみプロットすることができます。集約はTrvariableで行われます。

enter image description here

gg <- melt(RootTree, id="Tr", na.rm = T) 
errors = aggregate(. ~ Tr + variable, 
        data = gg, 
        FUN = sd) 
means = aggregate(. ~ Tr + variable, 
        data = gg, 
        FUN = mean) 

ggplot(means, aes(x = variable, 
        y = value, 
        fill = factor(Tr))) + 
    geom_bar(stat = 'identity', 
      position = position_dodge(0.7)) + 
    geom_errorbar(aes(ymin = means$value - errors$value, 
        ymax = means$value + errors$value), 
       width = 0.3, 
       position = position_dodge(0.7)) + 
    xlab("Seedlings") + 
    ylab("Mean of Root Weight (gr)") + 
    ggtitle("Root Weight of Seedlings by Treatment") 
関連する問題