R中的直方图具有“更多”类别,类似于MS Excel
考虑以下频率数据:
> table(income) income 3 5 6 7 8 5000 2 7 2 2 2 1
当我input
>hist(income)
我得到以下直方图
所以你可以看到,大多数收入价值都集中在5左右,而且有一个值与其他值相差很远,这使得直方图看起来不太好。 MS Excel可以将5000值视为另一个类别,所以数据会改为这样:
> table(income) income 3 5 6 7 8 more 2 7 2 2 2 1
因此,将其绘制为直方图会更好,因此您可以在较短的范围内看到频率:
有无论如何用hist()
函数或grid或ggplot2的其他函数做这个? 不过,我不想覆盖超过一定门槛的价值,所以我也没有任何信息。
非常感谢!
数据生成:
income <- c(rep(3,2), rep(5,7), rep(6,2), rep(7,2), rep(8,2), 5000)
为绘图准备数据的function:
nice.data <- function(x, threshold=10){ x[x>threshold] <- "More" x }
绘图:
library(ggplot2) ggplot() + geom_histogram(aes(x=nice.data(income))) + xlab("Income")
结果: