ggplot2::ggplot()
で作成したプロットに少しのサマリーテーブルを追加しようとしました。テーブルはgridExtra::tableGrob()
を介して保存されたggplotオブジェクトに追加されます。gridExtraとannotation_custom()でggplotにテーブルを追加するとy軸の制限が変わる
私の問題は、これが私の元のプロットのy制限を変えているようだということです。 ylim()
を介して再度制限を指定しなくてもそれを回避する手段はありますか?ここで
がChickWeightデータセットを使用して問題のための最小限の例です。
# load packages
require(ggplot2)
require(gridExtra)
# create plot
plot1 = ggplot(data = ChickWeight, aes(x = Time, y = weight, color = Diet)) +
stat_summary(fun.data = "mean_cl_boot", size = 1, alpha = .5)
plot1
# create table to add to the plot
sum_table = aggregate(ChickWeight$weight,
by=list(ChickWeight$Diet),
FUN = mean)
names(sum_table) = c('Diet', 'Mean')
sum_table = tableGrob(sum_table)
# insert table into plot
plot1 + annotation_custom(sum_table)
EDIT: 私はちょうどstat_summary()
の問題であると思われることを考え出しました。別のジオム/レイヤーを使用すると、元のプロットと同じように制限が残ります。そのため他の実施例:
plot2 = ggplot(data = ChickWeight, aes(x = Time, y = weight, color = Diet)) +
geom_jitter()
plot2
plot2 + annotation_custom(sum_table)
私はこの問題についてはよく分かっていませんが、 'stat_summary'の問題ではないと思います。' plot2 + stat_summary(fun.data = "mean_cl_boot"、size = 1、alpha = .5) + annotation_custom(sum_table) 'あなたのylimは保存されます。 – George
それは面白いです。 'geom = 'pointrange''(デフォルトで' stat_summary'が使用する)の代わりにデータ範囲全体のy制限を設定します。だから私が正しく見れば、私の最初の例では、ylimは(pointrangeからの)要約され表示された値の範囲に調整されますが、 'annotation_custom'を追加すると再びデータ全体の範囲が使用されます。 – abel