The way I usually do this is by making a pair of maps that go from B to A and A to B. To do this, if you wanted both axes to start at zero, you would first make a dataset with only two observations where the first was the minimum value for each variable and the second was the maximum. If you want both axes to start at zero, you should use zero for the minimum value, otherwise you could use the observed minimum in the data. For example,
tmp <- data.frame(
A = c(0, max(data$A)),
B = c(0, max(data$B))
)
Next, estimate two linear models where we calculate the coefficients to transform from one variable's space into the other. Then we can make functions that will calculate the transformation.
itmod <- lm(A ~ B, data=tmp)
tmod <- lm(B ~ A, data=tmp)
trans <- function(x)coef(tmod)[1] + coef(tmod)[2]*x
invtrans <- function(x)coef(itmod)[1] + coef(itmod)[2]*x
Then, we use those functions in the ggplot:
ggplot(data) +
geom_line(aes(Days, A, color = "A")) +
geom_point(aes(Days, A))+
geom_line(aes(Days, invtrans(B), color = "B"), size = 0.6) +
geom_point(aes(Days, invtrans(B)))+
theme_classic() +
scale_y_continuous(name = "A",
sec.axis = sec_axis(trans=trans, name="B"))+
scale_color_manual(name = "", values = c("A" = "darkgrey", "B" = "black"))

Also note, in your original code, the first call to geom_line()
had fill="grey"
, but you could actually remove that because you're defining colours with the scale_colour_manual()
function. Also discussed in this answer relating to grouped bar plots and this answer relating to grouped box plots.