I'm trying to plot a two "y" axis, the first one with "soil water content" in % (geom_line() ) and a geom_bar() with precipitation data. The problem is the precipitation chart. I need to "reverse" the plot.
I have this chart now:
and I need the soil water content time series as image above but precipitation as following image
Transforming the data do not solve the problem since i can not transform the bar plot for visualize it form upside down since the data are bars not points. Overmore when i reverse the plot both axis turn reverse
the "hum_melt10" data frame is a data frame with 3 columns: fecha = date (daily), value = water content (%) and variable = if the data is from a probe o from a model
the "pp_melt" data frame is a data frame with 3 columns: fecha = date (daily), value = cm of precipitation for each day and variable = if the water is from precipitation or irrigation
gpp = ggplot() +
geom_line(data = hum_melt10,aes(x = fecha, y = value, color = variable), size = 1.0) +
xlab("Fecha") +
geom_bar(data = pp_melt, aes(x = fecha, y = value / 20, fill = variable), stat="identity",position = 'dodge', na.rm = TRUE) +
scale_y_continuous(name = "Contenido de agua (%)",sec.axis = sec_axis(~.*20, name = "pp y riego (cm)")) +
scale_x_date(breaks = '2 month', labels = fecha, date_labels = '%b %y') +
theme(plot.title = element_text(lineheight=.8, face="bold", size = 20)) +
theme_bw() + theme( panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"), aspect.ratio = 0.3)
Thank you!
ggplot2
the finally resulted in the ability to effectively add just transformed labels; the second axis does nothing with the data itself. (This is clear by the need to dovalue
in one andvalue/20
in the other.) – r2evans