これは、いくつかのカスタム エラー バーの取得に関する前の質問の続きです。
- プロットの外観は私が必要としているものなので、それに関してだけコメントすることを心配しないでください (ただし、他のヘルプに添付された意見を聞いてうれしいです)
- これらのプロットはループで生成され、エラー バーは実際には条件が満たされた場合にのみ追加されるため、前もってすべてのデータを単純にマージすることはできません。 dfs.
ggplot
別のデータフレームを使用していくつかのエラーバーを追加しようとしています。プロットを呼び出すと、新しいデータを使用して誤差範囲を追加しようとしているだけなのに、親プロットから y 値が見つからないと表示されます。これは構文エラーに違いないことはわかっていますが、困惑しています...
まず、データとプロットを生成しましょう
library(ggplot2)
library(scales)
# some data
data.2015 = data.frame(score = c(-50,20,15,-40,-10,60),
area = c("first","second","third","first","second","third"),
group = c("Findings","Findings","Findings","Benchmark","Benchmark","Benchmark"))
data.2014 = data.frame(score = c(-30,40,-15),
area = c("first","second","third"),
group = c("Findings","Findings","Findings"))
# breaks and limits
breaks.major = c(-60,-40,-22.5,-10, 0,10, 22.5, 40, 60)
breaks.minor = c(-50,-30,-15,-5,0, 5, 15,30,50)
limits =c(-70,70)
# plot 2015 data
ggplot(data.2015, aes(x = area, y = score, fill = group)) +
geom_bar(stat = "identity", position = position_dodge(width = 0.9)) +
coord_flip() +
scale_y_continuous(limit = limits, oob = squish, minor_breaks = breaks.minor,
breaks = breaks.major)
プロット (c) を呼び出すと、期待どおりに優れたプロットが生成されます。エラー バーを設定して、プロット "c" に新しいレイヤーとして追加してみましょう。
# get the error bar values
alldat = merge(data.2015, data.2014, all = TRUE, by = c("area", "group"),
suffixes = c(".2015", ".2014"))
alldat$plotscore = with(alldat, ifelse(is.na(score.2014), NA, score.2015))
alldat$direction = with(alldat, ifelse(score.2015 < score.2014, "dec", "inc"))
alldat$direction[is.na(alldat$score.2014)] = "absent"
#add error bars to original plot
c <- c+
geom_errorbar(data=alldat, aes(ymin = plotscore, ymax = score.2014, color = direction),
position = position_dodge(width = .9), lwd = 1.5, show.legend = FALSE)
今cを呼び出すと、
"Error in eval(expr, envir, enclos) : object 'score' not found"
geom_errorbar
2番目のalldatデータフレームを使用してオーバーレイしたいだけなのに、なぜdata.2015$scoreを探すのですか?
編集* alldata$plotscore と alldat$score.2014 を使用してエラーバーの ymin/ymax 値を指定しようとしました (これは悪い習慣だと確信しています)。プロットされますが、バーは間違った位置/アウトにありますプロットとの順序の違い (例: 代わりにベンチマーク バーで入れ替えるなど)