1

I need to plot two scatter plots using ggplot. One in which all of the values are positive ranging between (0,0.02) and another in which all of the values are negative ranging between (0,-1500). I have tried to use sec_axis to convey the differing scales but I have been able to produce the following.

enter image description here

This was produced using

library(tidyverse)
library(ggplot2)
scl = with(use_deciles, max(abs(coef_navy))/max(abs(coef_maroon)))

ggplot(use_deciles) + 
  geom_point(aes(x = decile_navy, y = coef_navy, color = 'navy')) + 
  geom_point(aes(x = decile_maroon, y = coef_maroon*scl, color = 'maroon')) +    
  labs(x = "Group", y = "") +
  scale_color_manual(values = c('maroon', 'navy')) + 
  scale_y_continuous(sec.axis = sec_axis(~. /scl, name = "2nd axis"))

As you can see the scl provides the optimal re-scaling between the right and left axis but here is my question:

How can I adjust the ranges of the y axes so that I can put 0 at the top of the left axis but at the bottom of the right axis?

Given that I can only define sec_axis as a linear transformation of the left-axis, I'm not sure if this option can actually shift the labels. If I need to manually re-label the y-axis, that could be ok but I want to first understand how to overlay my scatter plots here.

Thank you in advance.

JRR
  • 578
  • 5
  • 21
  • 2
    Please make a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). `use_deciles` isn't defined. – Martin Gal Aug 30 '21 at 22:44

2 Answers2

3

Try something like this with @jared_mamrot data:

ggplot(use_deciles) + 
  geom_point(aes(x = count, y = coef_navy, color = 'navy')) + 
  geom_point(aes(x = count, y = coef_maroon*scl-1200, color = 'maroon')) +    
  labs(x = "Group", y = "") +
  scale_color_manual(values = c('maroon', 'navy')) + 
  scale_y_continuous(sec.axis = sec_axis(~(.+1200)/scl, name = "2nd axis"))

You will get this: enter image description here

Zhiqiang Wang
  • 6,206
  • 2
  • 13
  • 27
2

If I understand your question correctly, I don't believe it is possible without changing the values. I don't think you can change the limits on the left axis without affecting the right axis, and I don't believe you can change labels for each axis independently.

A potential alternative is to use the facet_wrap() function to plot them on top of each other, e.g.

library(tidyverse)

# Create example data
use_deciles <- data.frame(count = 1:10,
                          coef_maroon = c(0.005, 0.01, 0.015, 0.02, 0.03, 0.07, 0.09, 0.12, 0.15, 0.2),
                          coef_navy= c(0, -200, -400, -600, -800, -700, -900, -900, -1100, -1200))

# Original method
scl = with(use_deciles, max(abs(coef_navy))/max(abs(coef_maroon)))

ggplot(use_deciles) + 
  geom_point(aes(x = count, y = coef_navy, color = 'navy')) + 
  geom_point(aes(x = count, y = coef_maroon*scl, color = 'maroon')) +    
  labs(x = "Group", y = "") +
  scale_color_manual(values = c('maroon', 'navy')) + 
  scale_y_continuous(sec.axis = sec_axis(~. /scl, name = "2nd axis"))

# Facet_wrap method (on top of each other)
use_deciles %>% 
  pivot_longer(-count) %>%
  ggplot(aes(x = count, y = value, colour = name)) +
  geom_point() +
  scale_color_manual(values = c('maroon', 'navy')) +
  facet_wrap(~name, scales = "free_y", ncol = 1)

Created on 2021-08-31 by the reprex package (v2.0.1)

jared_mamrot
  • 22,354
  • 4
  • 21
  • 46