How to Make a Histogram using ggplot2

Ankur Pandey

2020-08-15

How to Make a Histogram using ggplot2

What does this package do?

The R ggplot2 package comes handy to visualize the statistical values that can arrange bins (breaks, range or classes). Though, it looks like a Bar plot, R ggplot2 display data in equal intervals histogram. Let’s dive into ggplot2 usage and steps, we will learn how to change theme, alter the axis, using R ggplot2 with example, if you want to shine at this visualization technique, it’s a good idea to learn ggplot2,ggplot2 is widely used structure.

Step1.

Check That You Have ggplot2 Installed

First, go to the tab ‘packages’ in RStudio, an IDE to work efficiently with R, look for ggplot2 and tick the checkbox. Alternatively, you may need to install the package. In such scenario, remain in the same tab, and you click on “Install”. Enter ggplot2, hit enter key and let it compile. You can also install ggplot2 effectively from the console area with the install Packages () function: install. Packages(“ggplot2”)

To successfully load the ggplot2 package, run the following command:

CODE&OUTPUT

# Importing the ggplot2 library
library(ggplot2)

Step 2.

The Data

We Need dataset, we can import the required file or use one that is built into R. This example illustrates steps, how to create a Histogram using the ggplot2 package. In this example, we are fetching diamonds data set provided by the R Studio. Load the data, as depicted below

CODE&OUTPUT

ggplot(data = diamonds,aes(x=price))+ geom_histogram()
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Step 3.

Making Your Histogram With ggplot2 and changing theme

We require data and graphical elements to make your plots. feed data to a plot as x and y elements and you also need to play with details, such as colours, markers, etc as graphical elements, which are added as layers. In this plot besides the data argument that you point to, you also add aes to describe how variables in the data are mapped to visual properties of geoms (geom_histogram ().set up mappings aesthetically in ggplot2 as well as individual layers. Aesthetic Mapping explains how variables in the data are mapped to visual properties of geoms. fill, alpha, line, colour,type,size,weight are some of the attributes of aes.

time to learn to change the existing theme in ggplot2 of histogram. themes come in handy for customising and consistancy

functions to change theme

theme dark (): This function changes the default theme, it basically makes it dark. Type theme_ in the R Studio then it shows the options list. For example, theme grey()’to Change theme of a R ggplot Histogram

CODE&OUTPUT

# Importing the ggplot2 library
library(ggplot2)
# Loading data
ggplot(data = diamonds,aes(x=price))+ geom_histogram()
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.


# Create a Histogram

  ggplot (data = diamonds, aes(x = price, fill = cut)) + 
  geom_histogram (binwidth = 500, aes(y=..density..), colour = "midnight blue") +
  facet_wrap(~ cut, scale = "free") +
  geom_density(color = "red", lwd = 0.9, alpha = 0.4) +
  labs(title="GGPLOT Histogram", x="Price in Dollars", y="Count") +
  theme_dark()

lets look at Axis and how to change it in ggplot histogram

Let us edit the default axis values in a ggplot histogram in R

xlim: X-Axis limits are changed with this argument ylim: It helps to specify the Y-Axis limits. To illustrate, lets change the default x-axis limit to (0, 20000), and y-axis limit to (0, 8000)

Let us edit the default axis values in a ggplot histogram in R

CODE&OUTPUT

# Changing Axis in a R ggplot Histogram

# Importing the ggplot2 library
library(ggplot2)
# Loading data
ggplot(data = diamonds,aes(x=price))+ geom_histogram()
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

# Create a Histogram
ggplot (data = diamonds, aes (x = price, fill = cut)) + 
  geom_histogram (binwidth = 250, colour = "midnight blue") +
  labs (title="GGPLOT Histogram", x="Price in Dollars", y="Count") + xlim (0, 20000) + ylim (0, 8000)
#> Warning: Removed 10 rows containing missing values (geom_bar).

CONCLUSION

It creates graphs that represent both univariate and multivariate numerical and categorical data in an easy and simple manner.

REFERENCE

1 “Tutorial gateway

2 “Data camp

3 “dummies

4 “t-redactyl