通常、ラベル付きのヒストグラムを作成する必要がある場合は、 を使用しますhist(rnorm(100),labels=TRUE)
。ただし、データが要因である場合は、 を使用する必要がありますplot(as.factor(c("a","a","b")))
。これに関する問題は、でlabels=TRUE
動作しないことplot
です; どうすればこれを修正できますか?
ファンシーなパッケージをロードする必要のないソリューションが望ましいです。
2番目の例では、実際に棒グラフを作成しています
以下は動作します
# your variable
fact <- as.factor(c('a','a','b'))
#
b <- plot(fact)
text(x=b,y=c(table(fact)), label = c(table(fact)),xpd=TRUE,col='blue')
function としてラップできますplot.factor
。
plot.factor <- function(x ,..., label=TRUE) {
cc <- table(x)
b <- barplot(cc,...)
if (label){
text(x = b, y = c(cc), label = c(cc), xpd = TRUE, col = 'blue')
}
return(invisible(b))
}
# Then
plot(fact)
# would produce the same result