Splitting long time series into multiple panels with ggplot2

I have a fairly long period of time that I want to build in ggplot, but it is long enough that even using the full width of the page, it is barely readable.

one time series in one panel

Instead, I want to divide the chart into 2 (or more, in general) panels, one on top of the other.

I could do it manually, but not only is it cumbersome, but it’s also difficult to get an axis equal to the scale. Ideally, I would like to have something like this:

ggplot(data, aes(time, y)) + 
  geom_line() +
  facet_time(time, n = 2)

And then get something like this:

one time series in several panels

(This graph was made using facet_wrap(~(year(as.Date(time)) > 2000), ncol = 1, scales = "free_x"), which ruined the x-axis scale, it works only for 2 panels and does not work with geom_smooth())

, . , geom_smooth() ( , ).

?

!

+4
2

, 1982-1999 1999-2016 ., grid.arrange gridExtra. .

ggplot, loess, geom_line ( , , , lm, gam, , .). , , .

library(dplyr)      # For the chaining (%>%) operator
library(purrr)      # For the map function
library(gridExtra)  # For the grid.arrange function

ggplot. , .

# http://stackoverflow.com/questions/12539348/ggplot-separate-legend-and-plot
g_legend<-function(a.gplot){
  tmp <- ggplot_gtable(ggplot_build(a.gplot))
  leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
  legend <- tmp$grobs[[leg]]
  legend
}

# Fake data
set.seed(255)
dat = data.frame(time=rep(seq(1982,2016,length.out=500),2),
                 value= c(arima.sim(list(ar=c(0.4, 0.05, 0.5)), n=500), 
                          arima.sim(list(ar=c(0.3, -0.3, 0.6)), n=500)),
                 group=rep(c("A","B"), each=500))

, loess: group, group_by dplyr:

dat = dat %>% group_by(group) %>%
        mutate(smooth = predict(loess(value ~ time, span=0.1)))

, . map ( base lapply map):

pl = map(list(c(1982,1999), c(1999,2016)), 
         ~ ggplot(dat %>% filter(time >= .x[1], time <= .x[2]), 
                  aes(colour=group)) +
             geom_line(aes(time, value), alpha=0.5) +
             geom_line(aes(time, smooth), size=1) + 
             scale_x_continuous(breaks=1982:2016, expand=c(0.01,0)) +
             scale_y_continuous(limits=range(dat$value)) +
             theme_bw() +
             labs(x="", y="", colour="") +
             theme(strip.background=element_blank(),
                   strip.text=element_blank(),
                   axis.title=element_blank()))


# Extract legend as a separate graphics object
leg = g_legend(pl[[1]])

, ( ) :

grid.arrange(arrangeGrob(grobs=map(pl, function(p) p + guides(colour=FALSE)), ncol=1),
             leg, ncol=2, widths=c(10,1), left="Value", bottom="Year")

enter image description here

+2

, , . coord_cartesian:

orig_plot <- ggplot(data, aes(time, y)) + 
  geom_line() 

early <-  orig_plot + coord_cartesian(xlim = c(1982, 2000))
late  <-  orig_plot + coord_cartesian(xlim = c(2000, 2016))

, .

, ( ggplot2, , , - pdf):

library(grid)
vp1 <- viewport(width = 1, height = .5, just = c("center", "bottom"))
vp2 <- viewport(width = 1, height = .5, just = c("center", "top"))
print(early, vp = vp1)
print(late, vp = vp2)
+2

Source: https://habr.com/ru/post/1673154/


All Articles