Ggplot2: No option for multiple independent axis

ggplot2

#1

Hi All,
As far as I can tell there is no option in ggplot2 to add multiple x or y axis that have independent scales as would be used for things CTD plots.

You can do it in base easy enough, but does anyone have any suggestions for how to do it in ggplot2?

x<-seq(1,10,by=1)
y<-seq(1,10,by=1)
yy<-seq(100,10,by=-10)

plot(x,y)
par(new = TRUE)
plot(x, yy, type = "o", axes = FALSE, bty = "n", xlab = "", ylab = "")
axis(side=4, at = pretty(range(yy)))

#2

This looks to me to be the great Dual Axis controversy. By design, ggplot2 does not facilitate this (although you can have one axis a linear combination of the other, eg Fahrenheit and Celsius). There is great danger
in superimposing data from different scales on eachother. I am less dogmatically against them than some are though.

There is a recent summary of the issues at https://blog.datawrapper.de/dualaxis/.


#3

While it is fine for people to have opinions on whether or not things should/shouldn't be done there are instances where there are some world standards on how data is visualized in science where you have multiple axis on a plot (See below). As a result it means that there are large chunks of science which can use the tidyverse for a lot of things they do but then when it comes to the standard reporting they have to go back to lattice or base because of someone else has an opinion of what is 'proper'


#4

"Someone else", as you put it, created a plotting package for free, which is deliberately opinionated and has no responsibility to conform to whatever various fields regard as standard formats.

There are people who have amended the code with a few lines to allow independent axes, so it is possible. It's just not encouraged.

If base/lattice provide the output required, then I'm not sure why you would want to use ggplot2 though. You'd have to make many changes to recreate your example.


#5

The reason I want to use ggplot for it is because after much pushing into learning the tidyverse because so many people are developing tools in it that for something i have little choice but to use it and having a workflow for a project that has 34 other plots in ggplot I would rather like to have the last one match.

If you know of code that allows it to be done I would be grateful; otherwise as you say I will just have to come up with a way on my own.


#6

Here's an work-around example rescaling your second line and using sec.axis.

https://rpubs.com/MarkusLoew/226759

It looks quite nice but will require careful manual attention to make sure your second axis looks right.

There may exist a more elegant solution I am not aware of...?


#7

I think it's a combination of opinion and effort for the developers of ggplot2. Allowing multiple scales for a single axis can be a big hassle. I'm sure you agree; you wouldn't have started this thread if the task were simple using the core graphic packages. So this is asking the developers to put a lot of time and effort into adding a feature.

Now opinion comes in; with any project which takes requests, some requests won't be seen as being worth the time. It's not to say they have no value, just not enough to justify the cost. And the ggplot2 developers don't see much value in overloading an axis scale.

For my own curiosity: does the graph you posted (depth ~ temperature & salinity) communicate something two line charts wouldn't?


#8

I recently used that work-around....which worked really well! Creating useful tools sometimes means providing the ability to use "controversial" methods.

My use (below) was comparing, in one plot, two common ways of summarizing large amounts of 3-dimensional instrument data.