Automated Dashboard Visualizations with Ranking in R

December 7, 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

    In this article, you learn how to make Automated Dashboard Visualizations with Ranking 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: Ranking"
    author: "Kristian Larsen"
    output: 
      flexdashboard::flex_dashboard:
        orientation: rows
        vertical_layout: scroll
    ---
    
    ```{r setup, include=FALSE}
    library(flexdashboard)
    library(ggplot2)
    library(plotly)
    # Prepare data: group mean city mileage by manufacturer.
    cty_mpg <- aggregate(mpg$cty, by=list(mpg$manufacturer), FUN=mean)  # aggregate
    colnames(cty_mpg) <- c("make", "mileage")  # change column names
    cty_mpg <- cty_mpg[order(cty_mpg$mileage), ]  # sort
    cty_mpg$make <- factor(cty_mpg$make, levels = cty_mpg$make)  # to retain the order in plot.
    ```
    
    Row
    -----------------------------------------------------------------------
    
    ### Chart A: Ordered Bar Chart
    
    ```{r}
    library(ggplot2)
    theme_set(theme_bw())
    
    # Draw plot
    ggplot(cty_mpg, aes(x=make, y=mileage)) + 
      geom_bar(stat="identity", width=.5, fill="tomato3") + 
      labs(title="Ordered Bar Chart", 
           subtitle="Make Vs Avg. Mileage", 
           caption="source: mpg") + 
      theme(axis.text.x = element_text(angle=65, vjust=0.6))
    ggplotly(p = ggplot2::last_plot())
    ```
    
    
    ### Chart B: Lollipop Chart
    
    ```{r}
    library(ggplot2)
    theme_set(theme_bw())
    
    # Plot
    ggplot(cty_mpg, aes(x=make, y=mileage)) + 
      geom_point(size=3) + 
      geom_segment(aes(x=make, 
                       xend=make, 
                       y=0, 
                       yend=mileage)) + 
      labs(title="Lollipop Chart", 
           subtitle="Make Vs Avg. Mileage", 
           caption="source: mpg") + 
      theme(axis.text.x = element_text(angle=65, vjust=0.6))
    ggplotly(p = ggplot2::last_plot())
    ```
    
    Row
    -----------------------------------------------------------------------
    
    ### Cart C: Dot Plot
    
    
    
    ```{r}
    library(ggplot2)
    library(scales)
    theme_set(theme_classic())
    
    # Plot
    ggplot(cty_mpg, aes(x=make, y=mileage)) + 
      geom_point(col="tomato2", size=3) +   # Draw points
      geom_segment(aes(x=make, 
                       xend=make, 
                       y=min(mileage), 
                       yend=max(mileage)), 
                   linetype="dashed", 
                   size=0.1) +   # Draw dashed lines
      labs(title="Dot Plot", 
           subtitle="Make Vs Avg. Mileage", 
           caption="source: mpg") +  
      coord_flip()
    ggplotly(p = ggplot2::last_plot())
    ```
    
    ### Cart D: Slope Chart
    
    ```{r}
    library(ggplot2)
    library(scales)
    theme_set(theme_classic())
    
    # prep data
    df <- read.csv("https://raw.githubusercontent.com/selva86/datasets/master/gdppercap.csv")
    colnames(df) <- c("continent", "1952", "1957")
    left_label <- paste(df$continent, round(df$`1952`),sep=", ")
    right_label <- paste(df$continent, round(df$`1957`),sep=", ")
    df$class <- ifelse((df$`1957` - df$`1952`) < 0, "red", "green")
    
    # Plot
    p <- ggplot(df) + geom_segment(aes(x=1, xend=2, y=`1952`, yend=`1957`, col=class), size=.75, show.legend=F) + 
                      geom_vline(xintercept=1, linetype="dashed", size=.1) + 
                      geom_vline(xintercept=2, linetype="dashed", size=.1) +
                      scale_color_manual(labels = c("Up", "Down"), 
                                         values = c("green"="#00ba38", "red"="#f8766d")) +  # color of lines
                      labs(x="", y="Mean GdpPerCap") +  # Axis labels
                      xlim(.5, 2.5) + ylim(0,(1.1*(max(df$`1952`, df$`1957`))))  # X and Y axis limits
    
    # Add texts
    p <- p + geom_text(label=left_label, y=df$`1952`, x=rep(1, NROW(df)), hjust=1.1, size=3.5)
    p <- p + geom_text(label=right_label, y=df$`1957`, x=rep(2, NROW(df)), hjust=-0.1, size=3.5)
    p <- p + geom_text(label="Time 1", x=1, y=1.1*(max(df$`1952`, df$`1957`)), hjust=1.2, size=5)  # title
    p <- p + geom_text(label="Time 2", x=2, y=1.1*(max(df$`1952`, df$`1957`)), hjust=-0.1, size=5)  # title
    
    # Minify theme
    p + theme(panel.background = element_blank(), 
               panel.grid = element_blank(),
               axis.ticks = element_blank(),
               axis.text.x = element_blank(),
               panel.border = element_blank(),
               plot.margin = unit(c(1,2,1,2), "cm"))
    ggplotly(p = ggplot2::last_plot())
    ```
    
    Row
    -----------------------------------------------------------------------
    
    ### Cart E: Dumbbell Plot
    
    ```{r}
    library(ggplot2)
    library(ggalt)
    theme_set(theme_classic())
    
    health <- read.csv("https://raw.githubusercontent.com/selva86/datasets/master/health.csv")
    health$Area <- factor(health$Area, levels=as.character(health$Area))  # for right ordering of the dumbells
    
    # health$Area <- factor(health$Area)
    gg <- ggplot(health, aes(x=pct_2013, xend=pct_2014, y=Area, group=Area)) + 
            geom_dumbbell(color="#a3c4dc", 
                          size=0.75, 
                          point.colour.l="#0e668b") + 
            scale_x_continuous(label=percent) + 
            labs(x=NULL, 
                 y=NULL, 
                 title="Dumbbell Chart", 
                 subtitle="Pct Change: 2013 vs 2014", 
                 caption="Source: https://github.com/hrbrmstr/ggalt") +
            theme(plot.title = element_text(hjust=0.5, face="bold"),
                  plot.background=element_rect(fill="#f7f7f7"),
                  panel.background=element_rect(fill="#f7f7f7"),
                  panel.grid.minor=element_blank(),
                  panel.grid.major.y=element_blank(),
                  panel.grid.major.x=element_line(),
                  axis.ticks=element_blank(),
                  legend.position="top",
                  panel.border=element_blank())
    plot(gg)
    ```
    

    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 Deviation in R
    2. Automated Dashboard with various correlation visualizations in R
    3. Automated Dashboard with Visualization and Regression for Healthcare Data
    4. Create easy automated dashbords with R and Markdown
    5. Send Desktop Notifications from R in Windows, Linux and Mac

    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)