Histogram in R – Implementation and Customization

In this TechVidvan R Tutorial series, we will learn what is histogram in R, how to implement it. We will also look at how to plot them using the ggplot2 package. So, without any further ado, let’s get started.

Histogram in R

Keeping you updated with latest technology trends, Join TechVidvan on Telegram

Histogram In R

Histograms are very similar to bar charts. They represent the number of data points in a range. Histograms are used to display numerical variables in bins. The difference between the histograms and bar charts is that bar charts represent categorical variables while histograms represent numeric variables.

Making Histogram in R

Histograms in R are also similarly easy to make. We can use the hist() command to make histograms in R.

hist(airquality$Temp)

Output

Histograms in R

Customizing Histogram in R

1. Adding a name to the plot

The main argument can be used to add a title to the graph.

hist(airquality$Temp,
     main="Temperatures in NY city")

Output

R histograms

2. Adding labels to the axes

The xlab and the ylab arguments can be used to add labels to the X and Y axes of the graph.

hist(airquality$Temp,
     main="Temperatures in NY city",
     xlab="Temperature",
     ylab="Frequency")

Output

Histograms in R

3. Changing the colors of the bars

The col argument can help with changing the color of the bars inside the graph.

hist(airquality$Temp,
     main="Temperatures in NY city",
     xlab="Temperature",
     ylab="Frequency",
     col="green")

Output

R Histograms

4. Adding borders to the bars

To add borders to the bars in the graph, we can use the border argument.

hist(airquality$Temp,
     main="Temperatures in NY city",
     xlab="Temperature",
     ylab="Frequency",
     col="green",
     border="red")

Output

Histograms in R

Histogram in R Using the Ggplot2 Package

As we have learnt in previous article of bar ploat that Ggplot2 is probably the best graphics and visualization package available in R. In this section of histograms in R tutorial, we are going to take a look at how to make histograms in R using the ggplot2 package.

ggplot(airquality, aes(x = Ozone)) +
  geom_histogram(aes(y = ..count..), binwidth = 5,
                 colour = "goldenrod2", fill = "gold1") +
  scale_x_continuous(name = "Mean ozone in\nparts per billion",
                     breaks = seq(0, 175, 25),
                     limits=c(0, 175)) +
  scale_y_continuous(name = "Count") +
  ggtitle("Frequency histogram of mean ozone")

Output

R Histograms

Summary

To conclude, in this chapter of TechVidvan’s R tutorial series, we learned all about histogram in R. We looked at how to make and customize it in R. Finally, we learned how to make histograms in R using the ggplot2 package.

Hope the article was useful for you. Share your feedback in the comment section.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.