showkey
showkey

Reputation: 338

How to set the hist graph properly?

Here is my data file in (update 2021: link is dead... http://s.yunio.com/87HT7f), please download it and save it as mydata.

y<-scan("mydata")
hist(y,breaks=c(0,60,70,80,90,100),freq=TRUE)
axis(2,at=seq(0,20,length.out=5),labels=c(0,5,10,15,20))

There are two problems:

1.Warning message:  
In plot.histogram(r, freq = freq1, col = col, border = border, angle = angle,  :  
  the AREAS in the plot are wrong -- rather use freq=FALSE  

I just want freq not probability, times counted on the y axis, how to make the warning message vanish?

2.When run

axis(2,at=seq(0,20,length.out=5),labels=c(0,5,10,15,20))  

There no 20 on y axis.

Upvotes: 2

Views: 1587

Answers (2)

liuminzhao
liuminzhao

Reputation: 2455

Check the manual for hist:

freq:
     Defaults to 'TRUE' _if and only if_ 'breaks' are equidistant
     (and 'probability' is not specified).

Upvotes: 1

Didzis Elferts
Didzis Elferts

Reputation: 98419

For the first problem, it is a warning, not the error. This warning says that visually areas of each bar do not correspond to their actual frequency - you can see it from the first bar that has the largest area but frequency is only 5.

For the second problem, you have to set ylim=c(0,20) inside hist() to see also number 20 because y axis is shorter than 20. Function axis() plots just labels, it doesn't change length of axis (originally there is no space for number 20).

hist(y,breaks=c(0,60,70,80,90,100),freq=TRUE,ylim=c(0,20))
axis(2,at=seq(0,20,length.out=5),labels=c(0,5,10,15,20))

Upvotes: 2

Related Questions