Udaya K Tejwani
March 4, 2021
Overview
This application displays a plot of mileage of different types of cars Vs their weight. It has Slider inputs to select:
minimum value on the x-axis
maximum value on the x-axis
minimum value on the y_axis
maximum value on the y-axis
The left control of the upper slider bar is used to choose the minimum value of the x-axis of the plot. The right control of the upper slider bar is used to choose the maximum value of the x-axis on the plot. The left control of the lower slider bar is used to choose the minimum value of the y-axis of the plot. The right control of the lower slider bar is used to choose the maximum value of the y-axis on the plot.
The application also fits a linear model to the data and displays it in the display, along with a mathematical description of the model.
This app is hosted at https://ubts.shinyapps.io/Course_9_Week_4_Project/ and at https://github.com/udayatejwani/Data-Science-Coursera/tree/master/Developing%20Data%20Products/Week%204%20Project
The application uses the following widgets:
sliderInput: The upper slider bar is used to select the minimum and maximum values for the weight of car.
The lower slider bar is used to select the minimum and maximum values for the mpg.
Reactive output display area is for displaying the plot.
Application - ui.R
library(shiny)
shinyUI(fluidPage(
# Application title
navbarPage("MPG Vs Wt Shiny Application",
tabPanel("Instructions",
helpText("You work for Motor Trend, a car manufacturing company looking at a data set of a collection of cars. ",
"The company is interested in exploring the relationship between weight of car and miles per gallon (MPG) ",
"(outcome). They are particularly interested in knowing the optimum weight of car that would provide ",
"maximum mpg for a given number of cylinders of engine. For this purpose they would like to study the ",
"regression model for each possible scenario."),
hr(),
helpText("The application is available by clicking on the 'Analysis' tab. The use of the application is self ",
"explanatory. Please adjust appropriate slider to appropriate values to focus on specific are of the ",
"scatterplot. The display panel will automatically update and will also display the regression model ",
"that is specific to the data that is visible in the display panel."),
hr(),
helpText("Please note that the application will give you an error 'Error: 0 (non-NA) cases' if you move the slider to values where no data ",
"points are available.")
),
tabPanel("Analysis",
titlePanel("The Green Car"),
# Sidebar with a slider input for minimum and maximum values for x-axis and y-axis
sidebarLayout(
sidebarPanel(
sliderInput("sliderX", "Pick Minimum and Maximum Weight Values", 0, 6, value = c(0, 6), step = 0.2),
sliderInput("sliderY", "Pick Minimum and Maximum MPG Values", 0, 25, value = c(0, 25))
),
# Show a plot
mainPanel(
h3("MPG Vs Weight of Car"),
plotOutput("plot1")
)
)
)
)
)
)
Application - server.R
library(shiny)
library(ggplot2)
data(mtcars)
# Define server logic required to draw a regression model representation
shinyServer(function(input, output) {
output$plot1 <- renderPlot({
minX <- input$sliderX[1]
maxX <- input$sliderX[2]
minY <- input$sliderY[1]
maxY <- input$sliderY[2]
# Fit regression line mpg Vs. wt
testdata <- subset(mtcars, subset=(mpg > minY & mpg < maxY & wt > minX & wt < maxX ))
require(stats)
reg <- lm(mpg ~ wt, data = testdata)
coeff = coefficients(reg)
ltext <- paste("mpg = ", round(coeff[2],1), " * wt + ", round(coeff[1],1))
cols <- c("8" = "red", "7" = "pink", "6" = "blue", "5" = "green", "4" = "yellow")
gp <- ggplot(mtcars, aes(wt, mpg, colour = factor(cyl), fill = factor(cyl))) +
geom_point(shape = 21, alpha = 0.5, size = 2) + scale_color_manual(values = cols, breaks = c("4", "5", "6", "7", "8"), aesthetics = c("colour","fill")) +
xlim(minX, maxX) +
ylim(minY, maxY)
gp + geom_abline(intercept = coeff[1], slope = coeff[2], col="green", size = 1.5) +
geom_text(x = minX + 0.8, y = minY, label = ltext, size = 5, color = "black")
})
})