ggplot with 2 y axes in a tweet

#rstats dual-y-axes #ggplot
d=mtcars
x='wt'
y='mpg'
z='disp'
a=range(d[[y]])
b=range(d[[z]])
s=diff(a)/diff(b)
d[[z]]=((d[[z]]-b[1])*s)+a[1]
ggplot(d)+
geom_line(aes_string(x,y))+ 
geom_line(aes_string(x,z),col='red') + 
scale_y_continuous(sec=sec_axis(~((.-a[1])/s)+b[1],z))

ggplot with 2 y axes - expanded

library(ggplot2)

#-----------------------------------------------------------------------------
# Pre-specify some variables to make the following code more generic
#-----------------------------------------------------------------------------
d  <- mtcars
x  <- 'wt'
y1 <- 'mpg'
y2 <- 'disp'


#-----------------------------------------------------------------------------
# Rescale the second y axis by 
#   - subtracting its minimum value (to set it to start at 0)
#   - scaling so that it has the same range as the 'y1' variable
#   - offsettting it by the minimum value of y1
#-----------------------------------------------------------------------------
a            <- range(d[[y1]])
b            <- range(d[[y2]])
scale_factor <- diff(a)/diff(b)
d[[y2]]      <- ((d[[y2]] - b[1]) * scale_factor) + a[1]


#-----------------------------------------------------------------------------
# Need to define the second axis transformation to be the inverse of the data
# transformation to everything cancels out appropriately
#-----------------------------------------------------------------------------
trans <- ~ ((. - a[1]) / scale_factor) + b[1]


#-----------------------------------------------------------------------------
# tell the y axis to set up a scaled secondary axis with the given transform
#-----------------------------------------------------------------------------
ggplot(d) +
  geom_line(aes_string(x, y1)) + 
  geom_line(aes_string(x, y2), col='red') + 
  scale_y_continuous(sec.axis = sec_axis(trans=trans, name=y2))