我知道你通常应该使用条形图来表示分类变量,但在我的情况下,有人会将连续变量分成几组,无论如何都会有一个直方图。
这是我想要的(除了作为直方图):
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轴之间没有空格)
数据:
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, 5L, 6L, 8L, 5L, 6L, 5L, 5L, 7L, 6L, 5L, 5L, 6L, 10L, 3L, 6L, 6L, 3L, 2L, 4L, 9L, 2L, 6L, 7L, 1L, 5L, 6L, 5L, 4L, 7L, 5L, 2L, 6L, 3L, 3L, 2L, 7L, 6L, 6L, 5L, 7L, 6L, 1L, 7L, 3L, 2L, 5L, 5L, 3L, 3L, 3L, 4L, 1L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, 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")
答案 0 :(得分:3)
感谢李哲元,我已经得到了答案。我可以简单地创建一个像直方图一样兴奋的条形图,而不是强制直方图:
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()
这样也可以添加正常的干扰曲线:
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)