top of page
Writer's pictureBernard Kilonzo

Histogram in R (Highlighting the Mean, Mode, and Median)


How to create histogram graph in R

Introduction

In the previous article, I demonstrated how to create a Histogram graph in Tableau highlighting some of the basic statistics such as Mean, Mode, and Median. Histogram is a graphical representation that shows the distribution of numerical data. It is the type of bar chart that shows the frequency or number of observations within different numerical ranges called bins. The bins can either be fixed size or varying bin sizes.

Check out this previous article here!

In this article, I intend to recreate the same visualization using R (a programming language for statistical computing and graphics).

I’ll be using the same sample dataset used in the previous article, check-out the screenshot below.

screenshot of the sample dataset

To create histogram graph in R,

  • Open r script in RStudio.

  • Load the necessary libraries and dataset.

In this case, I have loaded 4 libraries, set my working directory, and loaded the data file ‘data.csv’

loading libraries and datasets, and setting working a directory in R

(Note: The library DescTools is helpful when computing the Mode in R)

To create the Histogram graph, use the following line of code.

Note: a is the data frame that contains the dataset we’re working on.

The code.

R code for creating histogram graph

(In this example, I have set the histogram bin size as 100, and added the statistics Mean, Median, and Mode as vertical reference lines)

Executing the code, we’ve.

sample histogram graph created in R

Note, the resulting view doesn’t differ from what we got in the previous article using Tableau.

I hope this article was helpful to you. If you wish to receive more Tableau tips and tricks, kindly join our mailing list by subscribing below.

If you like the work we do and would like to work with us, drop us an email on our contacts page and we’ll reach out!

Thank you for reading!

Blog.png
bottom of page