How to Draw Histogram in R
Creating a histogram in R is a great way to explore and better understand your data. Histograms are a type of bar graph that shows the frequency, or number of occurrences of a particular value or range of values in a given set of data. Histograms are most commonly used to visually analyze the distribution of numerical data, though they can also be used for more categorical data, such as the distribution of gender or the prevalence of certain diseases in a population. In this tutorial, we will learn how to draw a histogram in R.
Before we begin, it is important to note that R does not have a built-in histogram function. However, there are several packages available for R that provide functions for creating histograms. The most popular packages for creating histograms in R are the ggplot2, lattice, and plotly packages. In this tutorial, we will use the ggplot2 package.
Installing ggplot2
In order to use the ggplot2 package, you must first install it. To do this, open the R Console and type the following command:
install.packages(“ggplot2”)
Once the package is installed, you can then call the library by typing the following command:
library(“ggplot2”)
Creating the Histogram
Once the ggplot2 library has been loaded, we can begin creating the histogram. The first step is to create a data frame containing the data that will be used to create the histogram. For this example, we will create a data frame containing the heights of 10 people.
people <- data.frame(height=c(170, 170, 180, 181, 181, 182, 183, 184, 186, 186)) Now that the data is in a data frame, we can create the histogram by using the ggplot2 package. The syntax for using ggplot2 to create a histogram is as follows: ggplot(data = people, aes(x = height)) + geom_histogram(binwidth = 1) This code creates a histogram with a binwidth of 1. The binwidth is the size of the groupings of the data. In this example, the data is grouped into intervals of 1. If you wanted to change the binwidth, you could do so by simply changing the number in the binwidth argument.
Customizing the Histogram
Once the basic histogram has been created, you can then customize it further. For example, you can change the color of the bars, add a title, and add labels to the x- and y-axes. To do this, we can add additional arguments to the ggplot2 code.
For example, to change the color of the bars, we can add the fill argument with a color of our choice. To add a title, we can use the ggtitle argument. To add labels to the x- and y-axes, we can use the xlab and ylab arguments.
ggplot(data = people, aes(x = height)) +
geom_histogram(binwidth = 1, fill =”blue”) +
ggtitle(“Histogram of Heights”) +
xlab(“Height (cm)”) +
ylab(“Frequency”)
This code will create a histogram with the bars colored blue, a title of “Histogram of Heights,” and labels for the x- and y-axes.
Conclusion
In this tutorial, we have learned how to draw a histogram in R using the ggplot2 package. We have also learned how to customize the histogram by changing the color of the bars, adding a title, and adding labels to the x- and y-axes. With these tools, you will be able to create custom histograms to explore and better understand your data.

Leave a Reply