geom_histogram根据理论不提供直方图

时间:2018-05-18 06:36:58

标签: r ggplot2 histogram

histogram of 120 students

我使用以下r代码生成如图所示的直方图

ggplot(Book2, aes(x=weight, fill=..count..))+
  geom_histogram(bins=11, binwidth = 5)+
  scale_x_continuous(breaks = seq(45, 100, by = 5),
                     limits = c(45, 100))+
  scale_y_continuous(breaks = seq(0, 30, by = 5),
                     limits = c(0, 30))+
  theme_classic()

r所示的曲线给出为 histogram produced in R 数据以图片形式给出 the data 及其频率分布

its frequency distribution

1 个答案:

答案 0 :(得分:0)

对我来说效果很好,我得到的直方图是this。请使用以下代码。

休息时间有时会发生变化。根据您的需要,使用geom_histogram中的特定命令“break”指定它。

library(ggplot2)
##Creating your data according to the picture
seq<- seq(47,97,5)
repvect<- c(1,4,17,28,25,18,13,6,5,2,1)
weight<-c()

for(i in 1:length(seq)){
  vect<-rep(seq[i], repvect[i])
  weight<-c(vect, weight)
}

Book2<- data.frame(weight) #TRansforming it to a data frame

ggplot(Book2, aes(x=weight, fill=..count..))+
  geom_histogram(bins=11, binwidth = 5)+
  scale_x_continuous(breaks = seq(45, 100, by = 5),
                     limits = c(45, 100))+
  scale_y_continuous(breaks = seq(0, 30, by = 5),
                     limits = c(0, 30))+
  theme_classic()

P.D:记住你需要data.frame格式的数据来获得直方图。

干杯!