Automated Dashboard visualizations with Deviation in R

December 6, 2018
By

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

    Categories

    1. Programming

    Tags

    1. Data Visualisation
    2. R Markdown
    3. R Programming
    4. Tips & Tricks

    In this article, you learn how to make Automated Dashboard visualizations with Deviation 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: Deviation"
    author: "Kristian Larsen"
    output: 
      flexdashboard::flex_dashboard:
        orientation: rows
        vertical_layout: scroll
    ---
    
    ```{r setup, include=FALSE}
    library(flexdashboard)
    library(ggplot2)
    library(plotly)
    theme_set(theme_bw())  
    
    # Data Prep
    data("mtcars")  # load data
    mtcars$`car name` <- rownames(mtcars)  # create new column for car names
    mtcars$mpg_z <- round((mtcars$mpg - mean(mtcars$mpg))/sd(mtcars$mpg), 2)  # compute normalized mpg
    mtcars$mpg_type <- ifelse(mtcars$mpg_z < 0, "below", "above")  # above / below avg flag
    mtcars <- mtcars[order(mtcars$mpg_z), ]  # sort
    mtcars$`car name` <- factor(mtcars$`car name`, levels = mtcars$`car name`)  # convert to factor to retain sorted order in plot.
    ```
    
    Row
    -----------------------------------------------------------------------
    
    ### Chart A: Diverging Barcharts
    
    ```{r}
    ggplot(mtcars, aes(x=`car name`, y=mpg_z, label=mpg_z)) + 
      geom_bar(stat='identity', aes(fill=mpg_type), width=.5)  +
      scale_fill_manual(name="Mileage", 
                        labels = c("Above Average", "Below Average"), 
                        values = c("above"="#00ba38", "below"="#f8766d")) + 
      labs(subtitle="Normalised mileage from 'mtcars'", 
           title= "Diverging Bars") + 
      coord_flip()
    ggplotly(p = ggplot2::last_plot())
    ```
    
    
    ### Chart B: Diverging Lollipop Chart
    
    ```{r}
    library(ggplot2)
    theme_set(theme_bw())
    
    ggplot(mtcars, aes(x=`car name`, y=mpg_z, label=mpg_z)) + 
      geom_point(stat='identity', fill="black", size=6)  +
      geom_segment(aes(y = 0, 
                       x = `car name`, 
                       yend = mpg_z, 
                       xend = `car name`), 
                   color = "black") +
      geom_text(color="white", size=2) +
      labs(title="Diverging Lollipop Chart", 
           subtitle="Normalized mileage from 'mtcars': Lollipop") + 
      ylim(-2.5, 2.5) +
      coord_flip()
    ggplotly(p = ggplot2::last_plot())
    ```
    
    Row
    -----------------------------------------------------------------------
    
    ### Cart C: Diverging Dot Plot
    
    
    
    ```{r}
    library(ggplot2)
    theme_set(theme_bw())
    
    # Plot
    ggplot(mtcars, aes(x=`car name`, y=mpg_z, label=mpg_z)) + 
      geom_point(stat='identity', aes(col=mpg_type), size=6)  +
      scale_color_manual(name="Mileage", 
                         labels = c("Above Average", "Below Average"), 
                         values = c("above"="#00ba38", "below"="#f8766d")) + 
      geom_text(color="white", size=2) +
      labs(title="Diverging Dot Plot", 
           subtitle="Normalized mileage from 'mtcars': Dotplot") + 
      ylim(-2.5, 2.5) +
      coord_flip()
    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 with various correlation visualizations in R
    2. Automated Dashboard with Visualization and Regression for Healthcare Data
    3. Create easy automated dashbords with R and Markdown
    4. Send Desktop Notifications from R in Windows, Linux and Mac
    5. CHAID vs. ranger vs. xgboost — a comparison

    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)