rbind_ggplot_timeseries: combine multiple time series ggplots

Description Usage Arguments Details Value Examples

Description

This function combines multiple time series gplots. It allows for setting the x limits of all plots and removes x axis labels.

Usage

1
2
rbind_ggplot_timeseries(ggplot_list = list(), limits, hide_x_labels = TRUE,
  shrink_space = TRUE, shrink_factor = 0.2)

Arguments

ggplot_list

A list of ggplot objects in order to be plotted. Ordered from top to bottom

limits

a vector (n = 2) of as.POSIXct objects representing the minimum and maximum dates of the timeseries

hide_x_labels

a logical value indicating if the x axis labels, ticks and title should be suppressed from all but the last plot. Default is TRUE

shrink_space

a logical value indicating if the space between the plots should be reduced. Ignored if hide_x_labels = FALSE. Default is TRUE

shrink_factor

a numerical value indicating the distance between each of the plots.

Details

A function similar to combine multiple timeseries plots in a column

This function is based on the rbind function in the gtable package. The function is designed to work with time series plots and allow for the combination of multiple plots.

Value

returns a grob object using the arrangeGrob function.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# Load some example time series data
 library(lubridate)
 library(dplyr)
 library(reshape2)
 library(ggthemes)
 data(breamar)
 breamar$date <- ymd(paste(breamar$year, breamar$month, "1"))

 # Create three plots without subsetting the data.
 rain_plot <- ggplot() + geom_line(aes(date, rain_mm), breamar) + theme_few(20)
 min_temp_plot <- ggplot() + geom_line(aes(date, min_temp), breamar) + theme_few(20)
 max_temp_plot <- ggplot() + geom_line(aes(date, max_temp), breamar) + theme_few(20)

 # Plot all three together
 combined_plot <- rbind_ggplot_timeseries(ggplot_list = list(rain_plot,
                                            min_temp_plot,
                                            max_temp_plot
                                       ),
                         limits = c(dmy("01011960", "31122014"))
 )
combined_plot
 # Plot all together with zooming in on 1990 - 2000
 combined_plot <- rbind_ggplot_timeseries(ggplot_list = list(rain_plot,
                                            min_temp_plot,
                                            max_temp_plot
                                       ),
                         limits = c(dmy("01011990", "31122000"))
 )
 combined_plot
 # An example with data from different time frames
 # Plot monthly rain from 1960 - 1980
 rain_plot <- breamar %>%
     filter(date<=dmy("01011980")) %>%
     ggplot() + geom_line(aes(date, rain_mm)) + theme_few(20)
 rain_plot
 # Plot monthly minimum temp from 1980 - 2000
 min_temp_plot <- breamar %>%
   filter(date>=dmy("01011980")&date<=dmy("01012000")) %>%
   ggplot() + geom_line(aes(date, min_temp)) + theme_few(20)
 min_temp_plot
 # Plot  monthly maximum temp from 2000 onwards
 max_temp_plot <- breamar %>%
   filter(date>=dmy("01012000")) %>%
   ggplot() + geom_line(aes(date, max_temp)) + theme_few(20)
 max_temp_plot

 # Plot all three timeseries together.
 combined_plot <- rbind_ggplot_timeseries(ggplot_list = list(rain_plot,
                                            min_temp_plot,
                                            max_temp_plot
                                           ),
                         limits = c(dmy("01011960", "31122014"))
 )

# An example with a legend
# Create three plots without subsetting the data.
rain_plot <- ggplot() + geom_line(aes(date, rain_mm), breamar) + theme_few(20)
# Create a plot with groups and a legend.
temp_data <- melt(breamar[, c("date", "min_temp", "max_temp")],
                  id.var = "date"
                  )
temp_plot <- ggplot() + geom_line(aes(date, value, colour=variable), temp_data) +
                        theme_few(20)

combined_plot <- rbind_ggplot_timeseries(ggplot_list = list(rain_plot,
                                                            temp_plot
                                                            ),
                                         limits = c(dmy("01011990", "31122000"))
)

johnDorian/ggsnippets documentation built on May 19, 2019, 3:02 p.m.