频率柱上带有标签的因子直方图 [英] Histogram of factor with labels on frequency bars

查看:67
本文介绍了频率柱上带有标签的因子直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常,如果我需要制作带有标签的直方图,我将使用hist(rnorm(100),labels=TRUE).但是,如果我的数据是因素,那么我需要使用plot(as.factor(c("a","a","b"))).问题是labels=TRUE不能与plot一起使用. 我该如何解决?

Typically if I need to make a histogram with labels I will use hist(rnorm(100),labels=TRUE). However if my data is factors then I need to use plot(as.factor(c("a","a","b"))). The issue with this is that labels=TRUE will not work with plot; how do I fix this?

我最好希望找到一种解决方案,而无需加载精美的软件包.

I preferably want a solution without needing to load fancy packages.

推荐答案

您实际上是在第二个示例中创建条形图

You are actually creating a bar plot in the second example

以下将起作用

 # 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')

您可以将其包装为函数plot.factor.

You could wrap it as 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

这篇关于频率柱上带有标签的因子直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆