Say we have a timeseries data.frame
of time and one variable, such as a forecast of hourly flow of a river in cubic feet per second (cfs), and another data.frame
that's serving as a table to relate the flow to another, say for our example, a table relating the river's flow to the height (aka stage) it's known to reach at given flows (typically a non-linear relationship). Knowing that plotting two axes not in a 1-1 transformation is discouraged by ggplot2, what are workarounds to plot flow (in cfs) and stage (in ft) values on separate y-axes for a single geom_line
to show both the flow and stage forecast in 1 plot, for example like how these standard plots by NOAA do?
library(ggplot2)
#example forecast/timeseries data
forecastleadtime_hours <- c(1,2,3,4,5,6,7,8,9)
flow_cfs <- c(65, 68, 501, 2500, 5500, 18000, 15000, 13000, 11000)
df <- data.frame(forecastleadtime_hours, flow_cfs)
# example lookup table
flow_cfs_lookup <- c(60, 500,2000, 3000, 5000, 7000, 11000, 14000, 19000, 22000,
25000, 40000)
stage_feet_lookup <- c(0,1,2.5,3.5,5,6.5,8.5,10,12,13.5, 15, 18)
flowstageratingtable <- data.frame(flow_cfs_lookup, stage_feet_lookup)
ggplot() + geom_line(data = df, aes(x = forecastleadtime_hours, y =
flow_cfs))
I realize this isn't an ideal data visualization, but it seems it's a case where one plot can quickly answer two questions for separate interests - either how much flow is anticipated in x hours or how high the river is expected to rise in x hours - and it's not always easy to offer many plots to an audience like the public, especially in times of emergency like during flooding, fires, quickly rising pollution levels, etc.