Modified from:
Updated: 2016-07-27
# Create fake data.frame
data.add.x = data.frame(
y1 = runif(100, 0, 100),
x1 = runif(100, 0, 100)
)
# Add second x-axis that scales with first
data.add.x$x2 = (data.add.x$x1 + 50)^0.75
# Create plots
plot1.x = qplot(y = y1, x = x1, data = data.add.x)
plot2.x = qplot(y = y1, x = x2, data = data.add.x)
# Run function
ggplot_dual_axis(plot1.x, plot2.x, "x")
# Add second y-axis that scales with first
data.add.x$y2 = (data.add.x$y^0.5) / 500
# Create plots
plot1.y = qplot(y = y1, x = x1, data = data.add.x)
plot2.y = qplot(y = y2, x = x1, data = data.add.x)
# Run function
ggplot_dual_axis(plot1.y, plot2.y, "y")
It works pretty well. Thanks for writing this function!