Open In App

Draw Histogram with Logarithmic Scale in R

Last Updated : 21 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

R is a programming language, mainly used for statistical computing and graphics. It implements various statistical techniques including linear modeling, classification, time series analysis, and clustering. A Histogram is commonly used to depict the frequency distribution of variables. It also is often used to summarize both continuous and discrete data. Unlike the bar chart where the frequency is indicated by the height of the bars, here the frequency is indicated by the area of the bar.

In this article, we will see how to draw a histogram with a Logarithmic scale in R Programming Language.

Logarithmic Scale

A logarithmic scale compresses the data at higher values and expands it at lower ones. When there is a requirement to display data in a way that highlights relative changes across a range and the data encompasses a wide range of values, logarithmic scales are employed in data visualization. In order to create a more balanced depiction of the data.

Method 1: Using Base R

The hist() function creates histograms. 

Syntax:

Syntax : hist( v, main, xlab, xlim, ylim, breaks, col, border)

If we want to convert the values of our histogram to a logarithmic scale, we can use the log() function within the hist() function as shown below:

hist(log(v))

  • v : The vector containing the numeric values to be used in histogram.

Creating a Normal Histogram

A vector ‘A’ is taken which contains the numerical values to be used by the histogram. The vector ‘A‘ is plotted using the hist() function, and the title of the plot is set to “Histogram” using the main parameter.

R




# Code for Plotting A Normal Histogram
# Create data for the Histogram
A <- c(10, 10, 10, 20, 15,
       15, 20, 20, 20, 20)
 
# Plot the histogram
hist(A, main = "Histogram")


Output:

Histogram with Logarithmic Scale in R

Creating a Histogram with Logarithmic Scale in R

A vector ‘A‘ is taken which contains the numerical values to be used by the histogram, and the vector ‘A‘ is plotted using the log() function inside the hist() function.

The title of the plot is set to “Histogram”, the color of the bar is set to “green”, border color of the bar is set to “red” and the xlabel is set to “Logarithmic Scale” using the main, col, border, xlab parameters respectively.

R




# Code for Plotting A Histogram in
# the Logarithmic Scale
 
# Create data for the Histogram
A <- c(10, 10, 10, 20, 20,
       20, 20, 20, 30, 30)
 
# Plot the histogram
hist(log(A), main = "Histogram",
     col = "grey", border = "red",
     xlab = "Logarithmic Scale")


Output:

ing

Histogram with Logarithmic scale in R

Using ggplot2 Package

Install the ggplot2 package using the install.packages() and Load the ggplot2 package using the library().

# Install & load ggplot2 package
install.packages("ggplot2")
library("ggplot2")

The ggplot2 is an R package dedicated to data visualization. With the ggplot2 package, we can plot almost any type of chart. 

Syntax: ggplot(data.frame(x), aes(x)) + geom_histogram()

  • x is the vector which contains the numerical values to be used by the histogram.
  • data.frame(x) creates a data frame from the given data
  • aes(x) is often used within other graphing elements to specify the desired aesthetics
  • geom_histogram() is used to plot the histogram

Creating a Normal Histogram:

  1. A vector “A” is taken which contains the numerical values to be used by the histogram.
  2. The histogram is plotted using the function ggplot() function and the geom_histogram() function

R




library("ggplot2")
 
# Code for Plotting A Histogram in
# the Logarithmic Scale
 
# Create data for the Histogram
A <- c(10, 10, 10, 20, 15,
       15, 20, 20, 20, 20)
 
# Plot the histogram
# The bins parameter divides the plot
# into 5 parts/bars
ggplot(data.frame(A), aes(A)) + geom_histogram(bins = 5)


Output:

ing

Histogram with Logarithmic scale in R

Using ggplot2 Package and log() function

If we want to convert the values of our histogram to a logarithmic scale, we can use the log() function within the ggplot() and geom_histogram() function as shown below:

Syntax: ggplot(data.frame(log(x)), aes(log(x))) + geom_histogram()

  • x is the vector which contains the numerical values to be used by the histogram.
  • data.frame(log(x)) creates a data frame from log(x)
  • aes(log(x)) is often used within other graphing elements to specify the desired aesthetics
  • geom_histogram() is used to plot the histogram

A vector ‘A‘ is taken which contains the numerical values to be used by the histogram. Transform the x-axis to a and logarithmic scale using the log() function, and, plot the vector ‘A‘ using the ggplot() function, log() function, the and geom_histogram() function. The color of the bars is set to green and the border color is set to red using fill and color respectively.

R




# Create data for the Histogram
data <- c(10, 10, 10, 20, 15,
       15, 20, 20, 20, 20)
 
# Calculate the logarithm of the data
log_data <- log(data)
 
# Create a histogram of the logarithmic data
hist(log_data, breaks = 6, col = "grey", border = "red",
     xlab = "Logarithmic Data", ylab = "Frequency",
     main = "Histogram of Logarithmic Data")


Output:

ing

Histogram with Logarithmic Scale in R

Using ggplot2 Package and scale_x_log10() function

If we want to convert the values of our histogram to a logarithmic scale, we can use the scale_x_log10() function along with the ggplot() and geom_histogram() function as shown below:

Syntax: ggplot(data.frame(x), aes(x)) + geom_histogram() + scale_x_log10()

Parameters:

  • x is the vector which contains the numerical values to be used by the histogram.
  • data.frame(x) creates a data frame from x.
  • aes(x) is often used within other graphing elements to specify the desired aesthetics
  • geom_histogram() is used to plot the histogram
  • scale_x_log10() is used to convert the x-axis to logarithmic scale

A vector ‘A‘ is taken which contains the numerical values to be used by the histogram. Plot the vector ‘A‘ using the ggplot() function, scale_x_log10() function and geom_histogram() function, The color of the bars is set to green and the border colour is set to red using fill and color respectively.

R




library("ggplot2")
 
 
# Code for Plotting A Histogram in
# the Logarithmic Scale
 
# Create data for the Histogram
A <- c(10, 10, 10, 20, 15,
       15, 20, 20, 20, 20)
 
# Plot the histogram
# The bins parameter divides the
# plot into 6 parts/bars
ggplot(data.frame(A), aes(A)) +
geom_histogram(color = "red", fill = "grey", bins = 6) +
scale_x_log10()


Output:

ing

Histogram with Logarithmic Scale in R



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads