ボックス(どこか)で「N =(サンプルサイズ)」を表示するボックスプロットをRで作成する方法はありますか?varwidth 論理は、サンプル サイズに基づいてボックスの幅を調整しますが、異なるプロット間の比較はできません。
FWIW、次の方法で boxplot コマンドを使用しています。ここで、「f1」は要因です。
boxplot(xvar ~ f1, data=frame, xlab="input values", horizontal=TRUE)
ここにいくつかのggplot2コードがあります。サンプル平均でサンプルサイズを表示し、ラベルを多機能にします!
まず、簡単な関数fun.data
give.n <- function(x){
return(c(y = mean(x), label = length(x)))
}
では、ダイヤモンドのデータを使って説明します
ggplot(diamonds, aes(cut, price)) +
geom_boxplot() +
stat_summary(fun.data = give.n, geom = "text")
見栄えを良くするためにテキストのサイズをいじる必要があるかもしれませんが、これでサンプル サイズのラベルができ、これもゆがみを感じさせます。
パラメーターを使用して、各因子名names
の次に書き込むことができます。n
自分で計算したくない場合は、次のn
小さなトリックを使用できます。
# Do the boxplot but do not show it
b <- boxplot(xvar ~ f1, data=frame, plot=0)
# Now b$n holds the counts for each factor, we're going to write them in names
boxplot(xvar ~ f1, data=frame, xlab="input values", names=paste(b$names, "(n=", b$n, ")"))
n
バーの上を取得するには、次のように boxplot によって提供される詳細text
を使用できます。stat
b <- boxplot(xvar ~ f1, data=frame, plot=0)
text(1:length(b$n), b$stats[5,]+1, paste("n=", b$n))
b の stats フィールドは行列です。各列には、1 つのグループ/プロットについて、下ヒゲの極値、下ヒンジ、中央値、上ヒンジ、および上ヒゲの極値が含まれます。
Envstats パッケージを使用して回避策を見つけました。このパッケージは、次を使用してダウンロード、ロード、およびアクティブ化する必要があります。
library(Envstats)
stripChart (stripchart とは異なります) は、n 値などのいくつかの値をグラフに追加します。まず、箱ひげ図をプロットしました。次に、stripChart で add=T を使用しました。明らかに、stripChart コードには多くのものが隠されているため、箱ひげ図には表示されません。これは、stripChart でほとんどの項目を非表示にするために使用したコードです。
n 値を表示するために統合された stripChart を使用した箱ひげ図:
stripChart(data.frame(T0_G1,T24h_G1,T96h_G1,T7d_G1,T11d_G1,T15d_G1,T30d_G1), show.ci=F,axes=F,points.cex=0,n.text.line=1.6,n.text.cex=0.7,add=T,location.scale.text="none")
箱ひげ図
boxplot(data.frame(T0_G1,T24h_G1,T96h_G1,T7d_G1,T11d_G1,T15d_G1,T30d_G1),main="All Rheometry Tests on Egg Plasma at All Time Points at 0.1Hz,0.1% and 37 Set 1,2,3", names=c("0h","24h","96h","7d ", "11d", "15d", "30d"),boxwex=0.6,par(mar=c(8,4,4,2)))
次に、stripChart
stripChart(data.frame(T0_G1,T24h_G1,T96h_G1,T7d_G1,T11d_G1,T15d_G1,T30d_G1), show.ci=F,axes=F,points.cex=0,n.text.line=1.6,n.text.cex=0.7,add=T,location.scale.text="none")
数値の上限 (n 値) をいつでも調整して、必要な場所に収まるようにすることができます。