0

Here is the issue, I want to plot (I use tidyverse, GGPLOT packages) a second y-axis on the right of the plot but from what I've understood sec.axis only allows you to put a transformation of the first axis (eg: first axis * 19/4). That I don't want.

I want to express two variables that have various units of measure (scales) and I'd like to have the left y axis in the unit of the first variable and the right y axis in the unit of the second (with the appropriate scales). In essence, I'd like to use scale="free" but for both y axis.

Any idea? Thank's in advance.

Sorry, here's a very simple code to illustrate this.

library(tidyverse)
Year <- c(2010:2014)

OilPrice <- c(60:64)
GasPrice <- c(6789,6802,6580, 6890, 7020)
DATASET <- data.frame(Year, OilPrice, GasPrice) %>% as_tibble(DATASET) %>%
  gather(key = "Prices", value = "value", -Year)
ggplot(DATASET, aes(Year, value, color=Prices)) + geom_point() + geom_line(aes(group=Prices)) +
  labs(
    title = "Prices evolution over time",
    y="Prices") + theme(legend.position = "bottom") 

As you can see the units are very disparate. I don't want to apply an arithmetic transformation rather I'd like to have Oil Prices on the Y-axis, ranging from 60 to 64 let's say and on the right Y-axis having the Gas Prices ranging from 6500 to 7200 eg. How can I do it?

dreamR
  • 25
  • 4
  • 1
    Hi dreamR, please share the code you have tried since now or make a reproducible question – Scipione Sarlo Jun 08 '21 at 07:34
  • Please see my code below. – dreamR Jun 08 '21 at 08:49
  • Does [this](https://stackoverflow.com/questions/3099219/ggplot-with-2-y-axes-on-each-side-and-different-scales) answer your question? As you point out, `ggplot2` insists on using transformations and not separate scales. – Timon Jun 08 '21 at 08:52
  • as @Timon pointed out, check https://stackoverflow.com/questions/3099219/ggplot-with-2-y-axes-on-each-side-and-different-scales. This is one of the most frequently asked questions in the ggplot2 tag and pops up on google on first position when googling your exact question. check www.google.com - it's a useful site – tjebo Jun 14 '21 at 13:19

0 Answers0