I'm having trouble making a histogram in R. The problem is that I tell it to make 5 bins but it makes 4 and I tell to make 5 and it makes 8 of them.
我在r中做直方图有困难,问题是我告诉它做5个箱子,但它做4个,我告诉它做5个,它做8个。
data <- c(5.28, 14.64, 37.25, 78.9, 44.92, 8.96, 19.22, 34.81, 33.89, 24.28, 6.5, 4.32, 2.77, 17.6, 33.26, 52.78, 5.98, 22.48, 20.11, 65.74, 35.73, 56.95, 30.61, 29.82);
hist(data, nclass = 5,freq=FALSE,col="orange",main="Histogram",xlab="x",ylab="f(x)",yaxs="i",xaxs="i")
Any ideas on how to fix it?
有什么办法解决吗?
5 个解决方案
#1
18
Use the breaks argument:
用休息时间参数:
hist(data, breaks=seq(0,80,l=6),
freq=FALSE,col="orange",main="Histogram",
xlab="x",ylab="f(x)",yaxs="i",xaxs="i")
#2
10
The integer specified as argument for nclass
is used as a suggestion:
指定为nclass参数的整数作为建议:
the number is a suggestion only
这个数字只是一个建议。
An alternative solution is to cut
your vector into a specified number of groups and plot the result:
另一种解决方案是将你的向量切成一定数量的组,并绘制结果:
plot(cut(data, breaks = 4))
#3
8
Building on the answer from Rob Hyndman:
根据Rob Hyndman的回答:
Maybe a more generic solution would be to make the breaks considering the minimun and maximun values of the data, and the number of breaks = number_of_bins+1.
也许更通用的解决方案是考虑数据的最小值和最大值,以及断点的数量= number_of_bin +1。
hist(data,breaks=seq(min(data),max(data),l=number_of_bins+1),
freq=FALSE,col="orange",
main="Histogram",xlab="x",ylab="f(x)",yaxs="i",xaxs="i")
#4
2
I like to be quite accurate about my data points:
我喜欢对我的数据点非常准确:
hist(data,breaks = seq(min(data),max(data),by=((max(data) - min(data))/(length(data)-1))))
This should automate the process with little manual input.
这将使流程自动化,无需人工输入。
#5
1
If you are not opposed to using something other than base graphics, there is always the ggplot2 way of doing things:
如果你不反对使用基础图形以外的东西,总有ggplot2做事方式:
library(ggplot2)
库(ggplot2)
data <- data.frame(x=data)
数据< - data.frame(x =数据)
ggplot(data, aes(x=x))+
geom_histogram(binwidth=18,color="black", fill="grey")+
scale_x_continuous(breaks=c(0,20,40,60,80)
ggplot2 has great documentation at: docs.ggplot2.org/current/
ggplot2有很好的文档:docs.ggplot2.org/current/
For histogram specific examples: http://docs.ggplot2.org/current/geom_histogram.html
对于直方图的特定示例:http://docs.ggplot2.org/current/geom_histogram.html
#1
18
Use the breaks argument:
用休息时间参数:
hist(data, breaks=seq(0,80,l=6),
freq=FALSE,col="orange",main="Histogram",
xlab="x",ylab="f(x)",yaxs="i",xaxs="i")
#2
10
The integer specified as argument for nclass
is used as a suggestion:
指定为nclass参数的整数作为建议:
the number is a suggestion only
这个数字只是一个建议。
An alternative solution is to cut
your vector into a specified number of groups and plot the result:
另一种解决方案是将你的向量切成一定数量的组,并绘制结果:
plot(cut(data, breaks = 4))
#3
8
Building on the answer from Rob Hyndman:
根据Rob Hyndman的回答:
Maybe a more generic solution would be to make the breaks considering the minimun and maximun values of the data, and the number of breaks = number_of_bins+1.
也许更通用的解决方案是考虑数据的最小值和最大值,以及断点的数量= number_of_bin +1。
hist(data,breaks=seq(min(data),max(data),l=number_of_bins+1),
freq=FALSE,col="orange",
main="Histogram",xlab="x",ylab="f(x)",yaxs="i",xaxs="i")
#4
2
I like to be quite accurate about my data points:
我喜欢对我的数据点非常准确:
hist(data,breaks = seq(min(data),max(data),by=((max(data) - min(data))/(length(data)-1))))
This should automate the process with little manual input.
这将使流程自动化,无需人工输入。
#5
1
If you are not opposed to using something other than base graphics, there is always the ggplot2 way of doing things:
如果你不反对使用基础图形以外的东西,总有ggplot2做事方式:
library(ggplot2)
库(ggplot2)
data <- data.frame(x=data)
数据< - data.frame(x =数据)
ggplot(data, aes(x=x))+
geom_histogram(binwidth=18,color="black", fill="grey")+
scale_x_continuous(breaks=c(0,20,40,60,80)
ggplot2 has great documentation at: docs.ggplot2.org/current/
ggplot2有很好的文档:docs.ggplot2.org/current/
For histogram specific examples: http://docs.ggplot2.org/current/geom_histogram.html
对于直方图的特定示例:http://docs.ggplot2.org/current/geom_histogram.html