library(plotly)
library(crosstalk)
library(dplyr)
data1 <- read.csv("report.csv")
Simple
data1%>%
plot_ly(x = ~robberies_percapita, y = ~homicides_percapita, hoverinfo = "text",
text = ~paste("Agency jurisdiction:", agency_jurisdiction))%>%
add_markers(frame = ~report_year, ids = ~agency_code)%>%
layout(title = "Relationship between crimes in police jurisdiction areas: robbery and homicide",
xaxis = list(title = "Robberies per capita"),
yaxis = list(title = "Homicide per capita"))%>%
hide_legend()
Updated slider
a1 <- data1%>%
plot_ly(x = ~robberies_percapita, y = ~homicides_percapita, hoverinfo = "text",
text = ~paste("Agency jurisdiction:", agency_jurisdiction))%>%
add_markers(frame = ~report_year, ids = ~agency_code)%>%
layout(title = "Relationship between crimes in police jurisdiction areas: robbery and homicide",
xaxis = list(title = "Robberies per capita"),
yaxis = list(title = "Homicide per capita"))%>%
hide_legend()
a1%>%
animation_slider(currentvalue = list(prefix = NULL, font = list(color = "red")))
Hidden slider
data1%>%
plot_ly(x = ~robberies_percapita, y = ~homicides_percapita, hoverinfo = "text",
text = ~paste("Agency jurisdiction:", agency_jurisdiction))%>%
add_text(x = 1250, y = 50, text = ~report_year, frame = ~report_year,
textfont = list(color = toRGB("gray80"), size = 200)) %>%
add_markers(frame = ~report_year, ids = ~agency_code)%>%
layout(title = "Relationship between crimes in police jurisdiction areas: robbery and homicide",
xaxis = list(title = "Robberies per capita"),
yaxis = list(title = "Homicide per capita"))%>%
animation_slider(hide = TRUE)%>%
hide_legend()
Baseline
data1975 <- data1 %>%
filter(report_year == 1975)
data1%>%
plot_ly(x = ~robberies_percapita, y = ~homicides_percapita, hoverinfo = "text",
text = ~paste("Agency jurisdiction:", agency_jurisdiction))%>%
add_text(x = 1250, y = 50, text = ~report_year, frame = ~report_year,
textfont = list(color = toRGB("gray90"), size = 200)) %>%
add_markers(data = data1975, marker = list(color = toRGB("gray20"), opacity = 0.5)) %>%
add_markers(frame = ~report_year, ids = ~agency_code, data = data1)%>%
layout(title = "Relationship between crimes in police jurisdiction areas: robbery and homicide",
xaxis = list(title = "Robberies per capita"),
yaxis = list(title = "Homicide per capita"))%>%
animation_slider(hide = TRUE)%>%
hide_legend()
Linking plots
shared_crime <- SharedData$new(data1975)
p1 <- shared_crime%>%
plot_ly(x = ~robberies_percapita, y = ~homicides_percapita, hoverinfo = "text",
text = ~paste("Agency jurisdiction:", agency_jurisdiction))%>%
add_markers()%>%
layout(title = "Relationship between crimes in police jurisdiction areas: robbery and homicide, 1975",
xaxis = list(title = "Robberies per capita"),
yaxis = list(title = "Homicide per capita"))%>%
hide_legend()
p2 <- shared_crime%>%
plot_ly(x = ~robberies_percapita, y = ~rapes_percapita, hoverinfo = "text",
text = ~paste("Agency jurisdiction:", agency_jurisdiction))%>%
add_markers()%>%
layout(title = "Relationship between crimes in police jurisdiction areas: robbery and rape, 1975",
xaxis = list(title = "Robberies per capita"),
yaxis = list(title = "Rapes per capita"))%>%
hide_legend()
subplot(p1, p2, titleY = TRUE, shareX = TRUE)%>%
hide_legend()%>%
highlight(on = "plotly_selected")
Filtering
p3 <- shared_crime%>%
plot_ly(x = ~robberies_percapita, y = ~homicides_percapita, hoverinfo = "text",
text = ~paste("Agency jurisdiction:", agency_jurisdiction))%>%
add_markers()%>%
layout(title = "Relationship between crimes in police jurisdiction areas: robbery and homicide, 1975",
xaxis = list(title = "Robberies per capita"),
yaxis = list(title = "Homicide per capita"))%>%
hide_legend()
p4 <- shared_crime%>%
plot_ly(x = ~robberies_percapita, y = ~rapes_percapita, hoverinfo = "text",
text = ~paste("Agency jurisdiction:", agency_jurisdiction))%>%
add_markers()%>%
layout(title = "Relationship between crimes in police jurisdiction areas: robbery and rape, 1975",
xaxis = list(title = "Robberies per capita"),
yaxis = list(title = "Rapes per capita"))%>%
hide_legend()
bscols(list(p3, p4, filter_slider(id = "agency_code", label = "Population", sharedData = shared_crime, column = ~population)))