2017-12-15 11 views
-4

私は、次のデータフレームbwplotに平均値をテキストとして追加しますか?

df <- data.frame(y = c(1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000), 
       x = c('1', '1', '2', '2', '1', '1', '2', '2'), 
       panel = c('A', 'A', 'A', 'A', 'B', 'B', 'B', 'B')) 

を持っている私は、次のパネルチャート

bwplot(df$y ~ df$x | df$panel, 
     ylab = "bla bla", xlab = "bla bla bla", 
     main = "example", 
     layout = (c(1, 2)), 
) 

をしました。しかし、平均で、テキストとして平均値を追加します。どのように私はこれを解決するのですか?

+0

[この](のhttp:// r.789695.n4.nabble.com/how-to-add-point-and-label-to-boxplot-using-bwplot-td801211.html)は –

+0

を助けるかもしれませんが、グラフは単なる軸です。私のプロットには2とパネルがあります。だから、panel.textを使うのは難しいようです。 –

+1

あなたの例は動作しません –

答えて

0

通常、私はggplot2パッケージを使用すると思いますが、この質問は、格子をタグ付けされているので、ここでは(hereから適応)可能な解決策があります:

library(lattice) 

bwplot(y ~ x | panel, 
     groups = paste(panel, x), # treat each combination of panel 
           # & x values as a distinct group 
     data = df, 
     fill = "white", 
     # col = "white",   # uncomment if you want to hide the dot for mean 
     ylab = "bla bla", 
     xlab = "bla bla bla", 
     layout = c(1, 2), 
     panel = panel.superpose, 
     panel.groups = function(x, y, ...){ 
     panel.bwplot(x, y, ...) 
     xt <- x     # label's x-axis position 
     yt <- mean(y)   # label's x-axis position 
     panel.text(xt, yt, labels = yt) 
     } 
     ) 

plot

関連する問題