The below code describes basics of creating bar plot and line graph
# Required libraries
library(readxl)
library(plotly)
library(dplyr)
library(Hmisc)
library(tidyr)
library(ggplot2)
library(shiny)
getwd()
#Read Crimes data
crimes_data <- read_excel("crimes.xlsx",skip = 1, col_names = c("Year","Violent","Murder","Property", "Rape","Robbery","Assault","Burglary", "Larceny_Theft","Vehicle_Theft") )
str(crimes_data)
#calculting total rates
crimes_data <- mutate(crimes_data, Average_Rate =((rowSums(crimes_data[,c(2,4)]))/2))
#Reshaping columns from wide to long format For Barplot
crimes_data_barplot <- crimes_data %>% gather(key = 'crimes_data', value = 'Rate', -c(Year,Violent,Property,Average_Rate))
#Reordering factors in crimes_data column
crimes_data_barplot$crimes_data <- factor(crimes_data_barplot$crimes_data, levels = c("Murder","Rape","Assault","Robbery","Vehicle_Theft","Larceny_Theft","Burglary"), ordered = TRUE)
# Define UI for application
ui <- fluidPage(
#Title of visualisation
titlePanel("Annual change in number of crimes committed in USA from 1961 to 2018"),
h6("Created by : Divya Ulaganathan"),
mainPanel(position = "top" ,
tabsetPanel(
tabPanel("Crime Rate Change Comparision Barplot",
plotOutput("barPlot",width = "100%", height = "350px"),
br(),
fluidRow(
column(4, sliderInput("Year", label = "Year", min = 1961, sep="",
max = 2018, value = 1961,
animate = animationOptions(interval = 500, loop = TRUE)
)),
column(7,h6("* Press play to view the crime rate changes across time."),
h6("* Press pause to view the crime rate of different crimes for a particular year.")
))),
tabPanel("Detailed Plot For each crime",
plotlyOutput("linePlot_plotly",width = "100%", height = "350px"),
p("* To choose a different crime press the button on top"),
p("* To compare different crimes click on the legends on the right side of the graph"),
p("* For a detailed rate value of a specific year, hover the mouse pointer on the line graph")
)
)
),
br(),
h4("Information:"),
h5("This visualisation aims to represent the change in crime rates."),
h5("The crimes committed in USA are classified into two groups: violent and property crimes"),
h5("* Crimes such as murder, rape, robbery and assault belongs Violent crimes."),
h5("* Crimes such as burglary, larcent theft and vehicle theft belongs Property or Non-Violent crimes."),
h5("In order to calculate the rate of change for a specific crime, the below formula is used"),
h5("Change Rate Calculation:"),
h5("Rate(Current_Year/Previous_Year) = (value(Current_Year) - value(Previous_Year)) / value(Previous_Year)"),
br(),
br(),
h6("Source data : Disastercenter.com. (2019). United States Crime Rates 1960 - 2018."),
h6("Available at: http://www.disastercenter.com/crime/uscrime.htm")
)
#Assigning server function
server <- function(input, output) {
#Line graph Using Plotly
output$linePlot_plotly <- renderPlotly ({
plot_ly(data = crimes_data) %>%
add_lines(x=~Year, y=~Average_Rate, name = "Average Crime Change Rate", visible = "TRUE",line = list(color = 'black')) %>%
add_lines(x=~Year, y=~Violent, name = "Violent", visible = "legendonly",line = list(color = "#8c510a")) %>%
add_lines(x=~Year, y=~Murder, name = "Murder", visible = "legendonly",line = list(color = "#67000d") )%>%
add_lines(x=~Year, y=~Rape, name = "Rape", visible = "legendonly",line = list(color = "#fb6a4a")) %>%
add_lines(x=~Year, y=~Assault, name = "Assault", visible = "legendonly",line = list(color = "#993404")) %>%
add_lines(x=~Year, y=~Robbery, name = "Robbery", visible = "legendonly",line = list(color = "#fe9929")) %>%
add_lines(x=~Year, y=~Property, name = "Property", visible = "legendonly",line = list(color = "#4d004b")) %>%
add_lines(x=~Year, y=~Vehicle_Theft, name = "Vehicle Theft", visible = "legendonly",line = list(color = "#ffffcc")) %>%
add_lines(x=~Year, y=~Larceny_Theft, name = "Larceny Theft", visible = "legendonly",line = list(color = "#bdbdbd")) %>%
add_lines(x=~Year, y=~Burglary, name = "Burglary", visible = "legendonly",line = list(color = "#525252")) %>%
layout(xaxis=list(fixedrange=TRUE)) %>% layout(yaxis=list(fixedrange=TRUE))%>%
layout(title = "Trend of various crimes in USA", showlegend= TRUE,
xaxis=list(zeroline = TRUE,title="Year"),
yaxis=list(zeroline = TRUE,title="% change in crime rate"),
updatemenus= updatemenus)
})
# update indicator variable menu - for Line graph Using Plotly
updatemenus <- list(
list(
active = 0,
type= 'buttons',
direction = "right",
xanchor = 'center',
yanchor = "top",
pad = list('r'= 0, 't'= 5, 'b' = 5),
x = 0.60,
y = 2.00,
buttons = list(
list(
label = "Average Change Rate",
method = "update",
args = list(list(visible = c(TRUE, "legendonly", "legendonly",
"legendonly", "legendonly","legendonly",
"legendonly","legendonly", "legendonly", "legendonly" )))),
list(
label = "Violent",
method = "update",
args = list(list(visible = c("legendonly", TRUE, "legendonly", "legendonly",
"legendonly", "legendonly","legendonly",
"legendonly","legendonly", "legendonly" )))),
list(
label = "Murder",
method = "update",
args = list(list(visible = c("legendonly", "legendonly", TRUE, "legendonly",
"legendonly", "legendonly", "legendonly",
"legendonly","legendonly", "legendonly")))),
list(
label = "Rape",
method = "update",
args = list(list(visible = c("legendonly", "legendonly", "legendonly", TRUE,
"legendonly", "legendonly", "legendonly",
"legendonly", "legendonly", "legendonly")))),
list(
label = "Assault",
method = "update",
args = list(list(visible = c( "legendonly", "legendonly", "legendonly","legendonly",
TRUE,"legendonly","legendonly",
"legendonly","legendonly", "legendonly")))),
list(
label = "Robbery",
method = "update",
args = list(list(visible = c("legendonly", "legendonly", "legendonly","legendonly",
"legendonly",TRUE,"legendonly",
"legendonly","legendonly", "legendonly")))),
list(
label = "Property",
method = "update",
args = list(list(visible = c("legendonly", "legendonly", "legendonly", "legendonly",
"legendonly", "legendonly",TRUE,
"legendonly", "legendonly" ,"legendonly")))),
list(
label = "Vehicle Theft",
method = "update",
args = list(list(visible = c("legendonly", "legendonly", "legendonly","legendonly",
"legendonly","legendonly", "legendonly",
TRUE,"legendonly", "legendonly" )))),
list(
label = "Larceny Theft",
method = "update",
args = list(list(visible = c("legendonly", "legendonly", "legendonly", "legendonly",
"legendonly", "legendonly", "legendonly",
"legendonly", TRUE, "legendonly")))),
list(
label = "Burglary",
method = "update",
args = list(list(visible = c("legendonly", "legendonly", "legendonly", "legendonly",
"legendonly", "legendonly", "legendonly",
"legendonly","legendonly", TRUE ))))
)
)
)
#Barplot function
output$barPlot <- renderPlot({
#Subsetting dataset to be used in function
data <- subset(crimes_data_barplot, crimes_data_barplot$Year == input$Year)
#ggplot function
ggplot(data = data, aes(x = crimes_data, y = Rate)) + geom_col(aes(fill = crimes_data), color = "black",show.legend = TRUE) +
labs(title = " " , x = "Crime Types", y = "% change in crime rate") +
theme_bw() + scale_y_continuous(limits = c(-30,30), expand = c(0,0)) + geom_hline(yintercept=0, linetype="solid", color = "black")+
scale_fill_manual(values = c("#67000d","#fb6a4a", "#993404","#fe9929","#ffffcc", "#bdbdbd","#525252"))
})
}
# Run the application
shinyApp(ui = ui, server = server)
https://divyaulaganathan.shinyapps.io/DataVisualizationProject1/
[1] Source data : Disastercenter.com. (2019). United States Crime Rates 1960 - 2018., Available at: http://www.disastercenter.com/crime/uscrime.htm