Histogram can be created using the hist() function in R programming language. This function takes in a vector of values for which the histogram is plotted. Let us use the built-in dataset airquality which has Daily air quality measurements in New York, May to September 1973.-R documentation.
str(airquality) $Ozone : int 41 36 18 NA 23 19 8 $Solar : int 190 118 149 313 NA NA 299 99 19 194 $Wind : num 7.4 8 12.6 11.5 14.3 14.9 8.6 13.8 $Temp : int 67 72 74 62 56 66 66 65 59 61 69 $Month : int 5 5 5 5 5 5 5 5 5 $Day : int 1 2 3 4 5 6 7 8 9 10
Temperature <- airquality$Temp
hist(Temperature)
We can pass in additional parameters to control the way our plot looks. You can read about them in the help section ?hist. Some of the frequently used ones are, main to give the title, xlab and ylab to provide labels for the axes, xlim and ylim to provide range of the axes, col to define color etc. Additionally, with the argument freq=FALSE we can get the probability distribution instead of the frequency.
#histogram with added parameters
str(airquality)
## 'data.frame': 153 obs. of 6 variables:
## $ Ozone : int 41 36 12 18 NA 28 23 19 8 NA ...
## $ Solar.R: int 190 118 149 313 NA NA 299 99 19 194 ...
## $ Wind : num 7.4 8 12.6 11.5 14.3 14.9 8.6 13.8 20.1 8.6 ...
## $ Temp : int 67 72 74 62 56 66 65 59 61 69 ...
## $ Month : int 5 5 5 5 5 5 5 5 5 5 ...
## $ Day : int 1 2 3 4 5 6 7 8 9 10 ...
Temprature <- airquality$Temp
hist(Temprature,
main ="Maximum Daily temp at La guardia Airport ",
xlab ="Temprature in Degree Fah ",
xlim =c(50,100),
border="green",
col="yellow",
freq=TRUE
)
Note that the y axis is labelled density instead of frequency. In this case, the total area of the histogram is equal to 1.