R 从分类变量创建直方图(不是条形图)

R 从分类变量创建直方图(不是条形图),r,histogram,R,Histogram,我知道你通常应该使用分类变量的柱状图,但在我的例子中,有人把连续变量分成几个组,不管怎样,有一个柱状图是很好的 这是我想要得到的(除了直方图): (注意:直方图在条形图和x轴之间没有间距) 数据: hh多亏了李哲远,我已经有了答案。我可以简单地创建一个看起来非常像直方图的条形图,而不是强制使用直方图: par(oma=c(2,0,0,0)) #so labels are not cut off barplot(table(hhincome),ylab = "Frequency", mai

我知道你通常应该使用分类变量的柱状图,但在我的例子中,有人把连续变量分成几个组,不管怎样,有一个柱状图是很好的

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

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

数据:


hh多亏了李哲远,我已经有了答案。我可以简单地创建一个看起来非常像直方图的条形图,而不是强制使用直方图:

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)
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()

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

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()

h多亏了李哲远,我已经有了答案。我可以简单地创建一个看起来非常像直方图的条形图,而不是强制使用直方图:

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)
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()

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

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()

h您没有使用
space=0
参数吗?这真是个好主意!我把太多的精力放在了让历史发挥作用上,我没有想到要寻找这个选择。另外
轴(1,labels=FALSE)
添加轴:)非常感谢!你没有使用
space=0
参数吗?这真是个好主意!我把太多的精力放在了让历史发挥作用上,我没有想到要寻找这个选择。另外
轴(1,labels=FALSE)
添加轴:)非常感谢!