-4

I need to plot several time series charts on the same page, each in its own panel in a grid 3x2 or similar. In the latter case I'd have 6 charts on a page in 3 rows and 2 columns. I need to use R, any popular plot library is fine.

Further, each chart will have multiple time series on it. All but one time series will be scaled to the y-axis on the left hand side, while one time series will be in a different scale with its y labels on the right hand side. This is similar to plotyy command in MATLAB.

I will need to customize quite a bit, things like colors and line types. What's the solution in R for this kind of a chart?

1 Answers1

0

With ggplot2 we can use facet_wrap to define the facets (panels) and sec_axis to define the secondary axis.

library(ggplot2)

ggplot(dd, aes(x, y, col = id)) + 
  geom_line() + 
  facet_wrap(~ panel) +
  scale_y_continuous(sec.axis = sec_axis(~ . / 2))

Note

We used dd defined below as input above. id defines the series and panel defines the panel in which each id should be placed.

set.seed(123)
dd <- data.frame(x = 1:11, y = rnorm(44), 
  id =  gl(4, 11, labels = paste0("ser", 1:4)), 
  panel = gl(2, 22, labels = paste0("panel", 1:2)))
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
  • If you have multivariate zoo series `library(zoo); z <- zoo(matrix(dd$y, 11, dimnames = list(NULL, unique(dd$id))), 1:11)` then `fortify.zoo(z, melt = TRUE)` will convert it to a long form data frame and to that you can append a panel column. – G. Grothendieck Aug 31 '18 at 02:27