It looks like you're interested in a bar plot - similar to a histogram, but with user-defined categories instead of auto-generated divisions.
We use cut
and table
functions to calculate the total number in each category, then calculate 40+ separately.
library(ggplot2)
v <- data.frame(name=1:20, value=rnorm(20, mean = 20, sd=20))
cats <- table(cut(v$value, breaks = seq(0, 40, 5)))
fortyplus <- sum(v$value>40)
dat <- data.frame(range=c(names(cats), "40+"),
count=c(as.numeric(cats), fortyplus))
ggplot(dat) + geom_bar(aes(x=range, y=count), stat = "identity")
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…