R Markdown

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 airqualitywhich has Daily air quality measurements in New York, May to September 1973.-R documentation.

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 ...

Simple Histogram

We can pass in additional parameters to control the way our plot looks.

Some of the frequently used ones are, main to give the title, xlaband ylabto provide labels for the axes, xlimand ylimto 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.

hist(temperature,
     main = "Maximum daily temperature at La Guardia
Airport",
     xlab = "Temperature in degrees Fahrenheit",
     xlim = c(50,100),
     col = "darkmagenta",
     freq = FALSE)

Note that the y axis is labelled density instead of frequency.

Additionally, with the argument freq=TRUE we can get the probability distribution instead of the frequency.

hist(temperature,
     main = "Maximum daily temperature at La Guardia
Airport",
     xlab = "Temperature in degrees Fahrenheit",
     xlim = c(50,100),
     col = "darkmagenta",
     freq = TRUE)

Note that the y axis is frequency.