Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
172 views
in Technique[技术] by (71.8m points)

r - Grouping Values In A Certain Range For a Plot

So I have a dataframe that looks like this.

name value
<int> <fct>
1    1
2    2.5
3    8
4    6
5    8
6    25
7    28
8    3
9    12
10   1.5
11   0.5
12   8
13   0.8
14   1.8
15   12
16   0.00594
17   1.2
18   3
19   1.25
20   5
21   300
22   38

I want to plot a graph where on the x-axis whole numbers are shown (0,5,10,15,...,40) and then a special group called 40+. On the y-axis it is how many values from my dataframe above are between those numbers. For example, between 35 and 40 should only be 1 value.

Initially, I thought a histogram would be suitable. However, my variables seemed to be discrete and histograms deals more with density and probability.

How can I go about this? I'd like to use ggplot2 if possible.

question from:https://stackoverflow.com/questions/65892247/grouping-values-in-a-certain-range-for-a-plot

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...