• 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
  • Python Viz
  • Seaborn
  • Altair
  • R Viz
  • ggplot2
  • About
    • Privacy Policy
  • 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()

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

Tags

Altair barplot Boxplot boxplot python boxplot with jiitered text labels Bubble Plot Color Palette Countplot Density Plot Facet Plot gganimate ggplot2 ggplot2 Boxplot ggplot2 error ggplot boxplot ggridges ggtext element_markdown() Grouped Barplot R heatmap heatmaps Histogram Histograms Horizontal boxplot Python lollipop plot Maps Matplotlib Pandas patchwork pheatmap Pyhon Python R RColorBrewer reorder boxplot ggplot Ridgeline plot Scatter Plot Scatter Plot Altair Seaborn Seaborn Boxplot Stock Price Over Time Stripplot UpSetR Violinplot Violin Plot World Map ggplot2

Buy Me a Coffee

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

Go to mobile version