Automatic Dashboard visualizations with Time series visualizations in R

December 9, 2018
By

(This article was first published on R Programming – DataScience+, and kindly contributed to R-bloggers)

    Categories

    1. Programming

    Tags

    1. R Programming
    2. RMarkdown
    3. Time Series

    In this article, you learn how to make Automatic Dashboard visualizations with Time series visualizations in R. First you need to install the `rmarkdown` package into your R library. Assuming that you installed the `rmarkdown`, next you create a new `rmarkdown` script in R.

    After this you type the following code in order to create a dashboard with rmarkdown and flexdashboard:

    ---
    title: "Dashboard visualizations in R: Time series visualizations"
    author: "Kristian Larsen"
    output: 
      flexdashboard::flex_dashboard:
        orientation: rows
        vertical_layout: scroll
    ---
    
    ```{r setup, include=FALSE}
    library(ggplot2)
    library(plotly)
    theme_set(theme_classic())
    ```
    
    Row
    -----------------------------------------------------------------------
    
    ### Chart A1: Time Series Chart
    
    ```{r}
    ## From Timeseries object (ts)
    library(ggplot2)
    library(ggfortify)
    theme_set(theme_classic())
    
    # Plot 
    autoplot(AirPassengers) + 
      labs(title="AirPassengers") + 
      theme(plot.title = element_text(hjust=0.5))
    ggplotly(p = ggplot2::last_plot())
    ```
    
    ### Chart A2: Time Series Chart
    
    ```{r}
    # Allow Default X Axis Labels
    ggplot(economics, aes(x=date)) + 
      geom_line(aes(y=psavert)) + 
      labs(title="Time Series Chart", 
           subtitle="Returns Percentage from 'Economics' Dataset", 
           caption="Source: Economics", 
           y="Returns %")
    ggplotly(p = ggplot2::last_plot())
    ```
    
    
    ### Chart B: Time Series Plot For a Monthly Time Series
    
    ```{r}
    library(ggplot2)
    library(lubridate)
    theme_set(theme_bw())
    
    economics_m <- economics[1:24, ]
    
    # labels and breaks for X axis text
    lbls <- paste0(month.abb[month(economics_m$date)], " ", lubridate::year(economics_m$date))
    brks <- economics_m$date
    
    # plot
    ggplot(economics_m, aes(x=date)) + 
      geom_line(aes(y=psavert)) + 
      labs(title="Monthly Time Series", 
           subtitle="Returns Percentage from Economics Dataset", 
           caption="Source: Economics", 
           y="Returns %") +  # title and caption
      scale_x_date(labels = lbls, 
                   breaks = brks) +  # change to monthly ticks and labels
      theme(axis.text.x = element_text(angle = 90, vjust=0.5),  # rotate x axis text
            panel.grid.minor = element_blank())  # turn off minor grid
    ggplotly(p = ggplot2::last_plot())
    ```
    
    Row
    -----------------------------------------------------------------------
    
    ### Cart C: Time Series Plot For a Yearly Time Series
    
    ```{r}
    library(ggplot2)
    library(lubridate)
    theme_set(theme_bw())
    
    economics_y <- economics[1:90, ]
    
    # labels and breaks for X axis text
    brks <- economics_y$date[seq(1, length(economics_y$date), 12)]
    lbls <- lubridate::year(brks)
    
    # plot
    ggplot(economics_y, aes(x=date)) + 
      geom_line(aes(y=psavert)) + 
      labs(title="Yearly Time Series", 
           subtitle="Returns Percentage from Economics Dataset", 
           caption="Source: Economics", 
           y="Returns %") +  # title and caption
      scale_x_date(labels = lbls, 
                   breaks = brks) +  # change to monthly ticks and labels
      theme(axis.text.x = element_text(angle = 90, vjust=0.5),  # rotate x axis text
            panel.grid.minor = element_blank())  # turn off minor grid 
    ggplotly(p = ggplot2::last_plot())
    ```
    
    ### Cart D: Time Series Plot From Long Data Format: Multiple Time Series in Same Dataframe Column
    
    ```{r}
    library(ggplot2)
    library(lubridate)
    theme_set(theme_bw())
    
    df <- economics_long[economics_long$variable %in% c("psavert", "uempmed"), ]
    df <- df[lubridate::year(df$date) %in% c(1967:1981), ]
    
    # labels and breaks for X axis text
    brks <- df$date[seq(1, length(df$date), 12)]
    lbls <- lubridate::year(brks)
    
    # plot
    ggplot(df, aes(x=date)) + 
      geom_line(aes(y=value, col=variable)) + 
      labs(title="Time Series of Returns Percentage", 
           subtitle="Drawn from Long Data format", 
           caption="Source: Economics", 
           y="Returns %", 
           color=NULL) +  # title and caption
      scale_x_date(labels = lbls, breaks = brks) +  # change to monthly ticks and labels
      scale_color_manual(labels = c("psavert", "uempmed"), 
                         values = c("psavert"="#00ba38", "uempmed"="#f8766d")) +  # line color
      theme(axis.text.x = element_text(angle = 90, vjust=0.5, size = 8),  # rotate x axis text
            panel.grid.minor = element_blank())  # turn off minor grid
    ggplotly(p = ggplot2::last_plot())
    ```
    
    Row
    -----------------------------------------------------------------------
    
    ### Cart E: Time Series Plot From Wide Data Format: Data in Multiple Columns of Dataframe
    
    ```{r}
    library(ggplot2)
    library(lubridate)
    theme_set(theme_bw())
    
    df <- economics[, c("date", "psavert", "uempmed")]
    df <- df[lubridate::year(df$date) %in% c(1967:1981), ]
    
    # labels and breaks for X axis text
    brks <- df$date[seq(1, length(df$date), 12)]
    lbls <- lubridate::year(brks)
    
    # plot
    ggplot(df, aes(x=date)) + 
      geom_line(aes(y=psavert, col="psavert")) + 
      geom_line(aes(y=uempmed, col="uempmed")) + 
      labs(title="Time Series of Returns Percentage", 
           subtitle="Drawn From Wide Data format", 
           caption="Source: Economics", y="Returns %") +  # title and caption
      scale_x_date(labels = lbls, breaks = brks) +  # change to monthly ticks and labels
      scale_color_manual(name="", 
                         values = c("psavert"="#00ba38", "uempmed"="#f8766d")) +  # line color
      theme(panel.grid.minor = element_blank())  # turn off minor grid
    ggplotly(p = ggplot2::last_plot())
    ```
    
    ### Cart F: Stacked Area Chart
    
    ```{r}
    library(ggplot2)
    library(lubridate)
    theme_set(theme_bw())
    
    df <- economics[, c("date", "psavert", "uempmed")]
    df <- df[lubridate::year(df$date) %in% c(1967:1981), ]
    
    # labels and breaks for X axis text
    brks <- df$date[seq(1, length(df$date), 12)]
    lbls <- lubridate::year(brks)
    
    # plot
    ggplot(df, aes(x=date)) + 
      geom_area(aes(y=psavert+uempmed, fill="psavert")) + 
      geom_area(aes(y=uempmed, fill="uempmed")) + 
      labs(title="Area Chart of Returns Percentage", 
           subtitle="From Wide Data format", 
           caption="Source: Economics", 
           y="Returns %") +  # title and caption
      scale_x_date(labels = lbls, breaks = brks) +  # change to monthly ticks and labels
      scale_fill_manual(name="", 
                        values = c("psavert"="#00ba38", "uempmed"="#f8766d")) +  # line color
      theme(panel.grid.minor = element_blank())  # turn off minor grid
    ggplotly(p = ggplot2::last_plot())
    ```
    
    Row
    -----------------------------------------------------------------------
    
    ### Cart G: Calendar Heatmap
    
    ```{r}
    library(ggplot2)
    library(plyr)
    library(scales)
    library(zoo)
    
    df <- read.csv("https://raw.githubusercontent.com/selva86/datasets/master/yahoo.csv")
    df$date <- as.Date(df$date)  # format date
    df = 2012, ]  # filter reqd years
    
    # Create Month Week
    df$yearmonth <- as.yearmon(df$date)
    df$yearmonthf <- factor(df$yearmonth)
    df <- ddply(df,.(yearmonthf), transform, monthweek=1+week-min(week))  # compute week number of month
    df <- df[, c("year", "yearmonthf", "monthf", "week", "monthweek", "weekdayf", "VIX.Close")]
    # Plot
    ggplot(df, aes(monthweek, weekdayf, fill = VIX.Close)) + 
      geom_tile(colour = "white") + 
      facet_grid(year~monthf) + 
      scale_fill_gradient(low="red", high="green") +
      labs(x="Week of Month",
           y="",
           title = "Time-Series Calendar Heatmap", 
           subtitle="Yahoo Closing Price", 
           fill="Close")
    ggplotly(p = ggplot2::last_plot())
    ```
    
    
    Row
    -----------------------------------------------------------------------
    
    ### Cart I: Seasonal plot: International Airline Passengers
    
    ```{r}
    library(ggplot2)
    library(forecast)
    theme_set(theme_classic())
    
    # Subset data
    nottem_small <- window(nottem, start=c(1920, 1), end=c(1925, 12))  # subset a smaller timewindow
    
    # Plot
    ggseasonplot(AirPassengers) + labs(title="Seasonal plot: International Airline Passengers")
    ggplotly(p = ggplot2::last_plot())
    ```
    
    ### Cart J: Seasonal plot: Air temperatures at Nottingham Castle
    
    
    ```{r}
    ggseasonplot(nottem_small) + labs(title="Seasonal plot: Air temperatures at Nottingham Castle")
    ggplotly(p = ggplot2::last_plot())  
    ```
    

    Screenshot:

    The result of the above coding are published with RPubs here.

    References

    1. Using flexdashboard in R

    Related Post

    1. Automated Dashboard visualizations with distribution in R
    2. Automated Dashboard Visualizations with Ranking in R
    3. Automated Dashboard visualizations with Deviation in R
    4. Automated Dashboard with various correlation visualizations in R
    5. Automated Dashboard with Visualization and Regression for Healthcare Data

    To leave a comment for the author, please follow the link and comment on their blog: R Programming – DataScience+.

    R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



    If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

    Comments are closed.

    Search R-bloggers

    Sponsors

    Never miss an update!
    Subscribe to R-bloggers to receive
    e-mails with the latest R posts.
    (You will not see this message again.)

    Click here to close (This popup will not appear again)