Basic Application of plot Function in R. Figure 1 shows the output of the plot function: A scatterplot. The plot function in R is used to create the line graph. The basic syntax to create a line chart in R is − plot(v,type,col,xlab,ylab) Following is the description of the parameters used − v is a vector containing the numeric values.
In this article, you’ll learn to use hist() function to create histograms in R programming with the help of numerous examples.
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.
We will use the temperature parameter which has 154 observations in degree Fahrenheit.
We can see above that there are 9 cells with equally spaced breaks. In this case, the height of a cell is equal to the number of observation falling in that cell.
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.
Note that the y axis is labelled density instead of frequency. In this case, the total area of the histogram is equal to 1.
The hist()
function returns a list with 6 components.
We see that an object of class histogram
is returned which has:
breaks
-places where the breaks occur,counts
-the number of observations falling in that cell,density
-the density of cells, mids
-the midpoints of cells,xname
-the x argument name andequidist
-a logical value indicating if the breaks are equally spaced or not.We can use these values for further processing.
For example, in the following example we use the return values to place the counts on top of each cell using the text()
function.
With the breaks
argument we can specify the number of cells we want in the histogram. However, this number is just a suggestion.
R calculates the best number of cells, keeping this suggestion in mind. Following are two histograms on the same data with different number of cells.
In the above figure we see that the actual number of cells plotted is greater than we had specified.
We can also define breakpoints between the cells as a vector. This makes it possible to plot a histogram with unequal intervals. In such case, the area of the cell is proportional to the number of observations falling inside that cell.