Skip to content

Commit

Permalink
Correct brush plot. Add MDS
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Fannin authored and Brian Fannin committed Apr 5, 2016
1 parent dc7d0f3 commit 6376848
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 35 deletions.
43 changes: 29 additions & 14 deletions MDS.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
library(dplyr)
dfData <- readr::read_csv("CleanData.csv")
library(ggfortify)

GetMDS <- function(df){

matMDS <- t(as.matrix(df))

matCovar <- matMDS %*% t(matMDS)
matDist <- dist(matCovar)

mds <- cmdscale(matDist, eig = TRUE)

mds
}

PlotMDS <- function(mdsObj){

plot(mdsObj, type = 'n')
text(mdsObj, rownames(matMDS))

}

#=========
# load the data if needed
if (is.null(dfData)){
dfData <- readr::read_csv("CleanData.csv")
}

dfMDS <- dfData %>%
mutate(Fatality = ifelse(BikeInjury == "K: Killed", 1, -1)
Expand All @@ -11,17 +36,7 @@ dfMDS <- dfData %>%
, DriverAlcohol = ifelse(DriverAlcohol == "Yes", 1, -1)) %>%
select(Fatality, NoInjury, BikeAlcohol, FacingTraffic, Intersection, Residential, DriverAlcohol)

PlotMDS <- function(df){
matMDS <- t(as.matrix(df))

matCovar <- matMDS %*% t(matMDS)
matDist <- dist(matCovar)

mdsAccident <- cmdscale(matDist)
plot(mdsAccident, type = 'n')
text(mdsAccident, rownames(matMDS))

}
strMDS <- names(dfMDS)

dfMDS %>% PlotMDS()
dfMDS %>% select(-Intersection, -Residential) %>% PlotMDS
# dfMDS %>% PlotMDS()
# dfMDS %>% select(-Intersection, -Residential) %>% PlotMDS
13 changes: 12 additions & 1 deletion server.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,22 @@ shinyServer(function(input, output) {
if (!is.null(brush)) {
ranges$x <- c(brush$xmin, brush$xmax)
ranges$y <- c(brush$ymin, brush$ymax)
print(ranges$x)
ranges$x <- as.Date(ranges$x, origin = "1970-01-01")
ranges$y <- as.POSIXct(ranges$y, origin = "1970-01-01")
} else {
ranges$x <- NULL
ranges$y <- NULL
}
})

output$pltMDS <- renderPlot({
selectedMDS <- intersect(names(dfMDS), input$chkMDS)
# selectedMDS <- intersect(names(dfMDS), strMDS)
print(selectedMDS)
dfPlotMDS <- dfMDS[, selectedMDS]
myMds <- GetMDS(dfPlotMDS)
autoplot(myMds, label = TRUE)

})

})
55 changes: 35 additions & 20 deletions ui.R
Original file line number Diff line number Diff line change
@@ -1,34 +1,49 @@

#==============
# libs
library(shiny)
library(dplyr)
library(ggplot2)

#=================
# UI widgets
source("MakeWidgets.R")
source("MDS.R")

shinyUI(
pageWithSidebar(

headerPanel("Bike Safety Durham"),
shinyUI(fluidPage(

sidebarPanel(

checkboxGroupInput("chkDriverAlcohol", label = h4("Driver Alcohol"),
titlePanel("Bike Safety Durham"),

fluidRow(
column(width = 2

, checkboxGroupInput("chkDriverAlcohol", label = h4("Driver Alcohol"),
choices = as.list(strDriverAlcohol),
selected = strDriverAlcohol)

, checkboxGroupInput("chkBikeInjury", label = h4("Bike Injury"),
choices = as.list(strBikeInjury),
selected = strBikeInjury)
),
, checkboxGroupInput("chkBikeInjury", label = h4("Bike Injury"),
choices = as.list(strBikeInjury),
selected = strBikeInjury)
), # Row 1, Column 1

mainPanel(
plotOutput("pltDateTime"
, dblclick = "pltDateTime_dblclick"
, brush = brushOpts(id = "pltDateTime_brush"
, resetOnNew = TRUE)
)
column(width = 10
, plotOutput("pltDateTime"
, dblclick = "pltDateTime_dblclick"
, brush = brushOpts(id = "pltDateTime_brush", resetOnNew = TRUE)
)
) # Row 1, Column 2
), # End Row 1

fluidRow(
column(width = 2
, h2("Multi-dimensional Scaling")
, checkboxGroupInput("chkMDS", label = h4("MDS dimensions")
, choices = as.list(strMDS)
, selected = strMDS)
)
, column(width = 10
, plotOutput("pltMDS"))
)

) # End Page
) # End fluidPage

) # End ShinyUI

0 comments on commit 6376848

Please sign in to comment.