This is how (part) of my dataframe looks like:
dput(head(herbs,50)) structure(list(date = structure(c(15340, 15341, 15342, 15343, 15344, 15345, 15346, 15347, 15348, 15349, 15350, 15351, 15352, 15353, 15354, 15355, 15356, 15357, 15358, 15359, 15360, 15361, 15362, 15363, 15364, 15365, 15366, 15367, 15368, 15369, 15370, 15371, 15372, 15373, 15374, 15375, 15376, 15377, 15378, 15379, 15380, 15381, 15382, 15383, 15384, 15385, 15386, 15387, 15388, 15389), class = "Date"), SUM_PP = c(0.2, 1.01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3.44, 0.6, 0, 0, 0, 0, 0, 0, 0.2, 0, 0, 0.8, 0, 0, 0, 0, 0, 0.61, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), h_fspec_NDVI = c(0.4608, 0.4607, 0.4605, 0.4601, 0.4597, 0.4591, 0.4584, 0.4576, 0.4567, 0.4557, 0.4546, 0.4534, 0.4521, 0.4506, 0.4491, 0.4475, 0.4459, 0.4441, 0.4422, 0.4403, 0.4382, 0.4361, 0.434, 0.4317, 0.4294, 0.4271, 0.4246, 0.4221, 0.4196, 0.417, 0.4143, 0.4116, 0.4089, 0.4061, 0.4033, 0.4005, 0.3976, 0.3947, 0.3918, 0.3889, 0.3859, 0.3829, 0.38, 0.377, 0.374, 0.371, 0.368, 0.365, 0.362, 0.359), h_L_NDVI = c(0.5588, 0.561, 0.5631, 0.5652, 0.5671, 0.5689, 0.5706, 0.5722, 0.5737, 0.5751, 0.5764, 0.5776, 0.5786, 0.5796, 0.5804, 0.5811, 0.5817, 0.5822, 0.5825, 0.5828, 0.5829, 0.5829, 0.5828, 0.5826, 0.5822, 0.5818, 0.5812, 0.5805, 0.5797, 0.5788, 0.5778, 0.5767, 0.5755, 0.5741, 0.5727, 0.5712, 0.5695, 0.5678, 0.566, 0.5641, 0.5621, 0.5601, 0.5579, 0.5557, 0.5534, 0.5511, 0.5487, 0.5462, 0.5437, 0.5411)), row.names = c(NA, 50L), class = "data.frame")
I have a time series with the time date in Y-m-d
format, precipitation SUM_PP, and the NDVI signal h_fspec_NDVI and h_L_NDVI. I want to have a plot with the precipitation in one Y-axis and the two NDVI signals in another Y-axis, along the date.
I've been using this code:
meteo_spec <- ggplot(herbs) +
geom_bar(aes(x=date, y=SUM_PP),stat="identity", fill="cyan",colour="#006000")+
geom_line(aes(x=date, y=100*h_fspec_NDVI),stat="identity",color="red",size=1)+
geom_line(aes(x=date, y=100*h_L_NDVI), stat = "identity", colour="black",size=1)+
labs(title= "NDVI vs Precipitation",
x="TIME",y="PP")+
scale_y_continuous(sec.axis=sec_axis(~.*0.01,name="NDVI")) +
scale_y_continuous(sec.axis=sec_axis(~.*0.01,name="NDVI"))
meteo_spec
and I have successfully plot the precipitation and NDVI signal but I CAN'T MAKE THE PLOT PROPORTIONAL, in other words, I can't make one of the Y-axis independent of the other.
Any help is much appreciated.