0

I am trying to combine observed vs predicted values of different models as lines, along with % error as columns, using a secondary axis. I think I am close to achieving this but it would appear the columns are not plotting on the secondary y axis. Further complicating is the range of ~2.5 to -2.5 required to portray the %Error. I had originally envisioned combining separate line and column plots in a facet, with four facet columns, one for each model, and two rows, based on line and column plots, but that seemed far above my skill level. So, I opted to try combining the column and line in a four panel facet:

ggplot(DatMod) + 
    geom_line(aes(x = RunYear, y = Observed), size = 1, color="red", group = 1)+
  geom_line(aes(x = RunYear, y = Predicted), size = 1, color="black", group = 1)+
  geom_col(aes(x = RunYear, y = PERCENT_ERR), size = 1, color = "darkblue", fill = "white")+
  scale_y_continuous(sec.axis = sec_axis(~./20000, name = "% Error"))+
  facet_grid(rows=vars(Model))

The resulting plot: Predicted vs Observed line with % error secondary Y axis

Thanks for any guidance.

tjebo
  • 21,977
  • 7
  • 58
  • 94
salconflu
  • 11
  • 3
  • 1
    You might find an [answer](https://stackoverflow.com/a/66055331/11374827) I gave earlier to a similar question relevant. – teunbrand Feb 18 '21 at 18:27
  • ggplot2 is an opinionated visualization framework, and one of those opinions is that secondary axes should be avoided. Secondary axes are allowed, but only as a plot "decoration"; all the mapping of data to location is still tied to the primary axis. So in your case you'll need to multiply your PERCENT_ERR * 20000 so that it gets the inverse translation as you have applied to the secondary axis itself. – Jon Spring Feb 18 '21 at 20:08

0 Answers0