0

I want to overlay two sets of ggplot panels (each panel is a different country) into one, single ggplot panel, without any rescaling of any of the two plots, but ggplot rescales either one or the other.

I have tried using only one ggplot to include both variables, by doing ggplot(df, aes(x=t, y=a)), and, within that ggplot, then using geom_point and geom_smooth for the second variable (y=b), but this rescales variable a.

# plot 1
g <-ggplot(df, aes(x=year, y=a))
p <-g + geom_point(alpha=0.7) + geom_smooth(method="auto") + facet_wrap(~country, scales="free") + theme_bw() +
  xlab("Year") + ylab(bquote('a')) +
  scale_x_continuous(breaks=seq(1960, 2020, 15))

# plot 2
a <-ggplot(df, aes(x=year, y=b))
b <-a + geom_point(alpha=0.7, color="green") + geom_smooth(method="auto", color="darkgreen") +
  facet_wrap(~country, scales="free") + theme_bw() +
  xlab("Year") + ylab(bquote('b')) +
  scale_x_continuous(breaks=seq(1960, 2020, 15))

I expect to be able to overlay these two ggplots into a single set of panels, with both y-axes appearing exactly as they appear when they're plotted alone (including units). I would then need to somehow make one of the y-axis appear to the right of the panels, so I have two y-axes, one at each side.

Image 1. ggplot rescales left y-axis. I don't want this to happen.

Image 2. What I want instead is to be able to somehow merge each of these images to get a single panel per country, displaying both the green and the blue lines with the scales that appear here.

M.B.
  • 1
  • 1
  • Welcome to Stack Overflow! Could you make your problem reproducible by sharing a sample of your data so others can help (please do not use `str()`, `head()` or screenshot)? You can use the [`reprex`](https://reprex.tidyverse.org/articles/articles/magic-reprex.html) and [`datapasta`](https://cran.r-project.org/web/packages/datapasta/vignettes/how-to-datapasta.html) packages to assist you with that. See also [Help me Help you](https://speakerdeck.com/jennybc/reprex-help-me-help-you?slide=5) & [How to make a great R reproducible example?](https://stackoverflow.com/q/5963269) – Tung May 16 '19 at 05:30
  • Hi there, would specifying a second y-axis through `scale_y_continuous(sec.axis = sec_axis(...))` be helpful? You might have to pre-scale some data if they are not in the same range. – teunbrand May 16 '19 at 07:21
  • `ggplot2` intentionally does not allow you to create double-y axis graphs because they are often very misleading and not considered to be good graphing practice. If the scales are very different you should create two plots one below the other with the same x-axis scale. – jtr13 May 16 '19 at 18:13
  • Hello, thanks for your answers! I uploaded a couple of images as two links above, tho show what is actually happening (image 1), and what I need to obtain (image 2). In image 1, the blue line for the first country looks (misleadingly) as if it's constant, and in image 2, you can actually see how it changed through time. hope that helps to clarify things! – M.B. May 17 '19 at 23:42

0 Answers0