从分类变量创建直方图(非条形图) [英] Create histogram (not barplot) from categorical variable

查看:293
本文介绍了从分类变量创建直方图(非条形图)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道您通常应该对分类变量使用barplot,但是在我的情况下,有人将连续变量分成几组,总之有一个直方图会很好.

I know you usually should use a barplot for categorical variables but in my case somebody split the continuous variable into groups and it would be nice to have a histogram anyway.

这就是我想要得到的(除非是直方图):

This is what I want to get (except as a histogram):

par(oma=c(2,0,0,0))  #so labels are not cut off  
barplot(table(hhincome),ylab = "Frequency", main = "Netto houshold income",
          border="black", col="grey",las=2)

(注意:直方图在条形图和x轴之间没有空格)

(Note: A histogram has no space between bars and a x-axis)

数据:

hhincome <- structure(c(4L, 4L, 1L, 6L, 8L, 1L, 4L, 5L, 2L, 3L, 1L, 5L, 1L, 7L, 6L, 7L, 3L, 2L, 6L, 7L, 8L, 4L, 7L, 8L, 7L, 4L, 5L, 5L, 5L, 9L, 7L, 5L, 8L, 8L, 6L, 5L, 5L, 3L, 5L, 4L, 3L, 5L, 3L, 5L, 4L, 4L, 5L, 7L, 6L, 7L, 2L, 6L, 1L, 7L, 4L, 4L, 5L, 2L, 4L, 6L, 6L, 8L, 6L, 7L, 4L, 7L, 9L, 1L, 4L, 6L, 2L, 6L, 8L, 6L, 5L, 8L, 7L, 9L, 7L, 9L, 8L, 5L, 5L, 7L, 6L, 2L, 7L, 6L, 6L, 1L, 7L, 7L, 2L, 6L, 6L, 6L, 7L, 5L, 2L, 2L, 9L, 6L, 7L, 7L, 5L, 6L, 6L, 5L, 5L, 7L, 8L, 6L, 6L, 3L, 7L, 6L, 4L, 5L, 5L, 4L, 8L, 3L, 4L, 6L, 5L, 7L, 3L, 4L, 7L, 5L, 3L, 6L, 2L, 2L, 5L, 2L, 4L, 8L, 4L, 3L, 2L, 7L, 2L, 5L, 2L, 1L, 8L, 7L, 3L, 6L, 6L, 7L, 2L, 9L, 3L, 3L, 5L, 7L, 7L, 5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L, 6L, 7L, 1L, 1L, 4L, 3L, 11L, 3L, 6L, 5L, 2L, 7L, 5L, 6L, 8L, 8L, 3L, 4L, 9L, 6L, 5L, 7L, 8L, 8L, 6L, 8L, 1L, 3L, 5L, 8L, 1L, 6L, 7L, 9L, 8L, 4L, 4L, 6L, 5L, 7L, 6L, 7L, 7L, 3L, 9L, 5L, 8L, 11L, 3L, 7L, 6L, 7L, 8L, 8L, 2L, 2L, 3L, 2L, 5L, 6L, 5L, 7L, 4L, 7L, 2L, 7L, 2L, 2L, 4L, 7L, 6L, 9L, 8L, 5L, 1L, 6L, 3L, 10L, 1L, 7L, 4L, 7L, 5L, 6L, 8L, 4L, 8L, 4L, 5L, 8L, 6L, 7L, 7L, 8L, 7L, 7L, 6L, 7L, 5L, 7L, 9L, 5L, 7L, 4L, 2L, 7L, 3L, 6L, 3L, 8L, 5L, 2L, 6L, 7L, 7L), .Label = c("Less than 500 €", "500-900 €", "900-1300 €", "1300-1500 €", "1500-2000 €", "2000-2600 €", "2600-3500 €", "3500-4500 €", "4500-6000 €", "6000-8000 €", "8000€ or more"), class = "factor")

推荐答案

感谢李哲远我已经有了我的答案.除了强制直方图之外,我还可以创建一个看起来像直方图的条形图:

Thanks to Zheyuan Li I already have my answer. Instead of forcing a histogram, I can simply create a barplot that looks excatly like a histogram:

par(oma=c(2,0,0,0))   #so labels are not cut off   
barplot(table(hhincome2), space = 0, # set space between bars to zero
              ylab = "Frequency", main = "Netto houshold income",
              border="black", col="grey",las=2) 
axis(1, at =  hhincome,labels = FALSE) # at x-axis at category borders
box()

我刚刚找到了另一种方法:

I just found an alternative way:

h <- hist(as.numeric(hhincome2) #as.numeric converst factor levels to numeric values
          , xlab = "", ylab = "Frequency", main = "Netto houshold income \n(with normal disttribution curve)",
             border="black", col="grey",las=2, 
          xaxt='n') #this supresses the x-axis which would disply levels instead values
axis(1, at =  hhincome2, labels = hhincome2, las=2) #just add factor level labels as labels
box()

这样,还可以添加正态分布曲线:

This way it's possible to add a normal disttribution curve as well:

xfit<-seq(min(as.numeric(hhincome2)),max(as.numeric(hhincome2)),length=1100) 
yfit<-dnorm(xfit,mean=mean(as.numeric(hhincome2)),sd=sd(as.numeric(hhincome2))) 
yfit <- yfit*diff(h$mids[1:2])*length(as.numeric(hhincome2)) 
lines(xfit, yfit, col="black", lwd=2)

这篇关于从分类变量创建直方图(非条形图)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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