I am trying to plot several panels in ggplot2 while using facet_wrap. I want to have two y-axis. Left side y-axis ranged from 5 to 27 and right side y-axis ranged from 25 to 27. I want to show all the data points with regression line. However, when I am plotting, the variation in the right side y-axis is lower comparing to other dataset so appearing as a flat line. I want to keep right side y-axis from 25 to 27 so that variation in the data can be seem clearly. I used this code 1 but not able to sort it out. Any help is highly appreciated.
library(ggplot2)
scaleFactor <- max(d1$weeks) / max(d1$income)
ggplot(mtcars, aes(x=Year)) +
geom_smooth(aes(y=weeks), method="loess", col="blue") +
geom_smooth(aes(y=income * scaleFactor), method="loess", col="red") +
scale_y_continuous(name="weeks", sec.axis=sec_axis(~./scaleFactor, name="income")) +
theme(
axis.title.y.left=element_text(color="blue"),
axis.text.y.left=element_text(color="blue"),
axis.title.y.right=element_text(color="red"),
axis.text.y.right=element_text(color="red")
)