You could try to solve this by secundary axes, but I feel obligated to communicate warnings given here and elsewhere. That said, here is how you could do it.
First, we'll make a function that can rescale data from one range to another range
# x is variable to be rescaled
# y is a range to be rescaled towards
scaling <- function(x, y) {
y <- range(y)
x <- (x - min(x)) / (max(x) - min(x))
x * diff(y) + min(y)
}
Then we'll use this function in the aes()
of hp
and in the secondary axis. Colours are added for clarity.
ggplot(mtcars)+
geom_point(mapping = aes(wt, mpg, colour = "wt"))+
geom_line(mapping = aes(x = scaling(hp, wt), y = mpg, colour = "hp")) +
scale_x_continuous(sec.axis = sec_axis(~ scaling(., mtcars$hp), name = "hp"))
