4.5 C
London
Thursday, December 19, 2024
HomeTidyverse in Rggplot2 in RHow to Create a Log Scale in ggplot2

How to Create a Log Scale in ggplot2

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

Often you may want to convert the x-axis or y-axis scale of a ggplot2 plot into a log scale.

You can use one of the following two methods to do so using only ggplot2:

1. Use scale_y_continuous() or scale_x_continuous()

ggplot(df, aes(x=x, y=y)) +
  geom_point() +
  scale_y_continuous(trans='log10') +
  scale_x_continuous(trans='log10')

2. Use coord_trans()

ggplot(df, aes(x=x, y=y)) +
  geom_point() +
  coord_trans(y ='log10', x='log10')

If you’d like to format the axis labels to show exponents, you can use  functions from the scales package:

ggplot(df, aes(x=x, y=y)) +
  geom_point() +
  scale_y_continuous(trans='log10',
                     breaks=trans_breaks('log10', function(x) 10^x),
                     labels=trans_format('log10', math_format(10^.x)))

This tutorial shows examples of how to use these functions in practice.

Example 1: Log Scale Using scale_y_continuous()

The following code shows how to use the scale_y_continuous() function to create a log scale for the y-axis of a scatterplot:

library(ggplot2)

#create data frame
df #create scatterplot with log scale on y-axis
ggplot(df, aes(x=x, y=y)) +
  geom_point() +
  scale_y_continuous(trans='log10')

Log scale on y-axis of ggplot2

Example 2: Log Scale Using coord_trans()

The following code shows how to use the coord_trans() function to create a log scale for the y-axis of a scatterplot:

library(ggplot2)

#create data frame
df #create scatterplot with log scale on y-axis
ggplot(df, aes(x=x, y=y)) +
  geom_point() +
  coord_trans(y='log10')

Log scale ggplot2

Example 3: Custom Log Scale Labels

The following code shows how to use functions from the scales package function to create a log scale for the y-axis of a scatterplot and add custom labels with exponents:

library(ggplot2)
library(scales)

#create data frame
df #create scatterplot with log scale on y-axis and custom labels
ggplot(df, aes(x=x, y=y)) +
  geom_point() +
  scale_y_continuous(trans='log10',
                     breaks=trans_breaks('log10', function(x) 10^x),
                     labels=trans_format('log10', math_format(10^.x)))

Log scale with exponents in ggplot2

Notice that the y-axis labels have exponents, unlike the previous two plots.

Additional Resources

The Complete Guide to ggplot2 Titles
A Complete Guide to the Best ggplot2 Themes
How to Create Side-by-Side Plots in ggplot2

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories