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 appears that changing line 59 to
g = gtable_add_grob(g, g2$grob[[13]], pp$t, length(g$widths), pp$b - 1)
fixes the right-y-axis title issue
I also added this on line 2 to flip the text:
plot2 <- plot2 + theme(axis.title.y = element_text(angle = 270))