• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Data Viz with Python and R

Learn to Make Plots in Python and R

  • Home
  • Seaborn
  • Matplotlib
  • ggplot2
  • Altair
  • About
    • Privacy Policy
  • Visualizing Activation Functions in Neural Networks
  • Confusion Matrix Calculator
  • Visualizing Dropout Rate in Neural Network
  • Visualizing Loss Functions in Neural Networks
  • Show Search
Hide Search

How to duplicate y-axis in ggplot2

datavizpyr · October 20, 2023 ·

In this tutorial, we will learn how to duplicate the primary axis in a plot made with ggplot2. We can duplicate either y-axis or x-axis using ggplot2’s sec.axis() function.

Here we will first see an example of duplication y axis and then an example duplicating x-axis using sec.axis method.

Let us load tidyverse.

library(tidyverse)
theme_set(theme_bw(16))

How to duplicate primary axis in ggplot2

First, let us make a scatter plot with ggplot2. By default, ggplot2 places the x/y axies in their natural locations, i.e. x-axis at the bottom and y-axis on the left side.

diamonds %>%
  slice_sample(n=500) %>%
  ggplot(aes(x=carat, y=price))+
  geom_point()
ggsave("how_to_duplicate_y_axisin_ggplot2.png")  
How to duplicate y-axis in ggplot2
How to duplicate y-axis in ggplot2

Duplicate y-axis in ggplot2 with sec.axis

With sec.axis argument in ggplot2, we can duplicate any primary axis. In the example below we duplicate y-axis using sec.axis argument to to scale_y_continuous() layer. To duplicate we assign dup_axis() to sec.axis argument.

diamonds %>%
  slice_sample(n=500) %>%
  ggplot(aes(x=carat, y=price))+
  geom_point()+
  scale_y_continuous(sec.axis = dup_axis())
ggsave("duplicate_y_axis_with_sec_axis_ggplot2.png")  
Duplicate y-axis in ggplot2 with sec.axis()
Duplicate y-axis in ggplot2 with sec.axis()

Duplicate y-axis in ggplot2 with sec.axis

Similarly, we can duplicate x-axis using scale_x_continous() layer as shown below.

diamonds %>%
  slice_sample(n=500) %>%
  ggplot(aes(x=carat, y=price))+
  geom_point()+
  scale_x_continuous(sec.axis = dup_axis())
ggsave("duplicate_x_axis_with_sec_axis_ggplot2.png")  
Duplicate x-axis in ggplot2 with sec.axis()
Duplicate x-axis in ggplot2 with sec.axis()

Explore the Complete ggplot2 Guide

35+ tutorials with code: scatterplots, boxplots, themes, annotations, facets, and more—tested and beginner-friendly.

Visit the ggplot2 Hub → No fluff—just code and visuals.

Related posts:

Customizing Mean mark to boxplot with ggplot2How To Show Mean Value in Boxplots with ggplot2? Scatterplot with marginal multi-histogram with ggExtraHow To Make Scatterplot with Marginal Histograms in R? ggforce geom_circle(): Annotate with a circleHow To Annotate a Plot with Circle in R Default ThumbnailHow to Make Axis Text Bold in ggplot2

Filed Under: ggplot2, R Tagged With: duplicate y-axis in ggplot2

Primary Sidebar

Python & R Viz Hubs

  • Seaborn Guide & Cookbook
  • ggplot2 Guide & Cookbook
  • Matplotlib Guide & Cookbook
  • Confusion Matrix Calculator
  • Visualizing Activation Functions
  • Visualizing Dropout
  • Visualizing Loss Functions

Buy Me a Coffee

Copyright © 2026 · Daily Dish Pro on Genesis Framework · WordPress · Log in

Go to mobile version