2

I want to set 5 and 7 intervals for my histogram...

My info

abc <- c(38,40,30,35,39,40,48,36,31,36,47,35,34,43,41,36,41,43,48,40,32,34,
       41,30,46,35,40,30,46,37,55,39,33,32,32,45,42,41,36,50,42,50,37,39,
       33,45,38,46,36,31)
h = hist(abc)
h$density = h$counts/sum(h$counts)
plot(h,freq=FALSE,ylab = "Freq Rel")

Thanks.

Colonel G
  • 29
  • 6

2 Answers2

1
h=hist(abc, breaks=seq(30,55,l=6),
       freq=FALSE,main="Histogram",
       xlab="x",ylab="f(x)",yaxs="i",xaxs="i")
h$density = h$counts/sum(h$counts)
plot(h,freq=FALSE,ylab = "Freq rel",col="orange")

this is my solution, what do you think?

Colonel G
  • 29
  • 6
1

You can modify your own code to get this:

h1=hist(abc, breaks = seq(min(abc), max(abc), length=6))
h1$density = h1$counts/sum(h1$counts)
h2=hist(abc, breaks = seq(min(abc), max(abc), length=8), col='red')
h2$density = h2$counts/sum(h2$counts)
par(mfrow=c(2,1))
plot(h1,freq=FALSE,ylab = "Freq Rel", col='red')
plot(h2,freq=FALSE,ylab = "Freq Rel", col='blue')

enter image description here

or use ggplot2

library(gridExtra)
grid.arrange(ggplot(as.data.frame(abc), aes(abc)) +
               geom_bar(aes(y = ..count.. / sum(..count..)), fill='red', binwidth=(max(abc)-min(abc))/4),
             ggplot(as.data.frame(abc), aes(abc)) +
               geom_bar(aes(y = ..count.. / sum(..count..)), fill='blue', binwidth=(max(abc)-min(abc))/6))

enter image description here

Sandipan Dey
  • 21,482
  • 2
  • 51
  • 63