mtcars dataset plotter

Óscar Prieto
15/11/20

Introduction

This app was designed with the purpose of making exploratory analysis on the mtcars dataset easy and accesible for every person, even those who are not in touch with coding in R. The dataset we used is a mere example; the true objective would be the application of this general process to any dataset to make it more approachable.

The following car features are used:

  • “mog”: Miles per gallon
  • “cyl”: Number of cylinders
  • “disp”: Displacement
  • “hp”: Horsepower
  • “drat”: Rear axle ratio
  • “wt”: Weight

Features of the app

  • It allows us to plot different features of the dataset in the axis

  • It is possible to apply a smoother

  • It can facet rows and columns

ui.R

library(shiny)
library(ggplot2)

dataset <- mtcars

shinyUI(pageWithSidebar(

    headerPanel("mtcars dataset plotter"),

    sidebarPanel(

        sliderInput('sampleSize', 'Sample Size', min=10, max=nrow(dataset),
                    value=min(10, nrow(dataset)), step=5, round=0),

        selectInput('x', 'X', names(dataset)),
        selectInput('y', 'Y', names(dataset), names(dataset)[[2]]),
        selectInput('color', 'Color', c('None', names(dataset))),

        checkboxInput('smooth', 'Smooth'),

        selectInput('facet_row', 'Facet Row', c(None='.', names(dataset))),
        selectInput('facet_col', 'Facet Column', c(None='.', names(dataset)))
    ),

    mainPanel(
        plotOutput('plot')
    )
))

<!–html_preserve–>

mtcars dataset plotter

<!–/html_preserve–>

server.R

library(shiny)
library(ggplot2)

shinyServer(function(input, output) {

    dataset <- reactive( {
        mtcars[sample(nrow(mtcars), input$sampleSize),]
    })

    output$plot <- reactivePlot(function() {

        p <- ggplot(dataset(), aes_string(x=input$x, y=input$y)) + geom_point()

        if (input$color != 'None')
            p <- p + aes_string(color=input$color)

        facets <- paste(input$facet_row, '~', input$facet_col)
        if (facets != '. ~ .')
            p <- p + facet_grid(facets)

        if (input$smooth)
            p <- p + geom_smooth()

        print(p)

    }, height=600
    )

})

Link to the app