> dput(figure4)
structure(list(Days = c(1.3, 3, 4, 6, 7.7, 1, 4, 7.7, 1, 4, 6,
7.7), type = c("Indirect", "Indirect", "Indirect", "Indirect",
"Indirect", "Direct_control", "Direct_control", "Direct_control",
"Temp_control", "Temp_control", "Temp_control", "Temp_control"
), fraction = c(100, 75.9, 63.8, 39.2, 32.4, 100, 96.35, 98.1,
100, 107.05, 102.45, 96.26), ratio2 = c(2.87, 2.86, 2.94, 3.55,
3.8, 3.99, 3.638, 4.189, 1.088, 1.112, 1.07, 1.12), se2 = c(15,
11.38, 9.57, 5.88, 4.86, 15, 14.88, 14.7, 15, 16.0571, 15.368,
14.439)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-12L))
>
I would like to have legend with the lines. They belong to the second y axis, but I am struggling to achieve that. Any suggestion?
ylim.prim <- c(0, 120) # in this example, percentage
ylim.sec <- c(0, 5) # in this example, ratio
b <- diff(ylim.prim)/diff(ylim.sec)
a <- ylim.prim[1] - b*ylim.sec[1]
ggplot(figure4, aes(x=Days, y=fraction, shape = type, colour = type)) +
geom_point(size=4) +
geom_errorbar(aes(ymax = fraction + se2, ymin = fraction - se2, group= type), width = 0.25)+
geom_line(aes(x=Days, y=a + ratio2*b,colour = type, linetype = Ratio), size= 1)+
scale_y_continuous("Fraction remainig(%)", sec.axis = sec_axis(~ (. - a)/b, name = "Z/E ratio"))+ theme(text = element_text(size = 15))+
theme(axis.text.x = element_text(angle = 0, vjust = 1, hjust= 1,size =12 ))+
theme(axis.text.y = element_text(size =12 ))+
theme(text = element_text(size = 15)) +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"))`