#plot of shootings by state
state <- data %>% dplyr::filter(year == "2015") %>% group_by(state, pop) %>% summarise(total = n())
(state.plot <- highchart() %>%
hc_add_series(name="Shootings", data = ((state$total/state$pop)*100), type = "column") %>%
hc_xAxis(categories = state$state, color ="#2ecc71") %>%
hc_title(text="Fatal Shootings by Police", align="left") %>%
hc_subtitle(text="Per Capita, 2015", align="left") %>%
hc_yAxis(title=" ") %>%
hc_exporting(enabled = TRUE) %>%
hc_credits(enabled = TRUE, # add credits
text = "Washington Post",
href = "https://github.com/washingtonpost/data-police-shootings",
align = "left") %>%
hc_add_theme(hc_theme_gridlight()) %>%
hc_legend(enabled = FALSE))
#plot of shootings by race
race2 <- data %>% dplyr::filter(year == "2015") %>% group_by(race2) %>% summarise(total = n())
(race.plot <- highchart() %>%
hc_add_series(name="Shootings", data=race2$total, type = "column", colorByPoint=T) %>%
hc_xAxis(categories = race2$race2) %>%
hc_title(text="Fatal Shootings by Police", align="left") %>%
hc_subtitle(text="Broken out by race, 2015", align="left") %>%
hc_yAxis(title=" ") %>%
hc_exporting(enabled = TRUE) %>%
hc_credits(enabled = TRUE, # add credits
text = "Washington Post",
href = "https://github.com/washingtonpost/data-police-shootings",
align = "left") %>%
hc_add_theme(hc_theme_gridlight())%>%
hc_legend(enabled = FALSE))
(scatter <- highchart() %>%
hc_add_series_scatter(phl$emp, phl$pax) %>%
hc_yAxis(title = "PHL Monthy Pax") %>%
hc_xAxis(title = "PHL Area Monthly Employment")%>%
hc_title(text = "PHL Employment vs. Monthly Pax", align="left") %>%
hc_add_theme(hc_theme_gridlight()))
library(xts)
pax.ts <- ts(phl$pax, frequency = 12, start = c(2007, 1), end = c(2015,8))
hchart(pax.ts) %>%
hc_title(text = "Monthly PHL Passengers", align = "left") %>%
hc_subtitle(text = "January, 2007 to August, 2015", align = "left")%>%
hc_exporting(enabled = TRUE) %>%
hc_credits(enabled = TRUE, # add credits
text = "T-100 Domestic Market (All Carriers)",
href = "http://www.transtats.bts.gov/DL_SelectFields.asp?Table_ID=310&DB_Short_Name=Air%20Carriers") %>%
hc_add_theme(hc_theme_gridlight())
acf <- acf(diff(pax.ts), plot = FALSE)
hchart(acf) %>%
hc_title(text = "ACF Plot of Monthly PHL Passengers", align = "left") %>%
hc_subtitle(text = "January, 2007 to August, 2015", align = "left") %>%
hc_add_theme(hc_theme_gridlight()) %>%
hc_legend(enabled = FALSE) %>%
hc_yAxis(title=" ")
pax.ts <- ts(phl$pax, frequency = 12, start = c(2007, 1), end = c(2015,8))
price.ts <- ts(phl$price, frequency = 12, start = c(2007, 1), end = c(2015,8))
earnings.ts <- ts(phl$earnings, frequency = 12, start = c(2007, 1), end = c(2015,8))
emp.ts <- ts(phl$emp, frequency = 12, start = c(2007, 1), end = c(2015,8))
mv.ts<- cbind(pax.ts/1000000, price.ts, earnings.ts/1000, emp.ts/1000000)
hchart(mv.ts) %>%
hc_title(text = "PHL Passengers, Employment, Earnings, Nat'l Fuel Prices", align = "left") %>%
hc_subtitle(text = "Monthly Data, January, 2007 to August, 2015", align = "left") %>%
hc_add_theme(hc_theme_gridlight()) %>%
hc_legend(enabled = T)
pax.ts <- ts(phl$pax, frequency = 12, start = c(2007, 1), end = c(2015,8))
price.ts <- ts(phl$price, frequency = 12, start = c(2007, 1), end = c(2015,8))
earnings.ts <- ts(phl$earnings, frequency = 12, start = c(2007, 1), end = c(2015,8))
emp.ts <- ts(phl$emp, frequency = 12, start = c(2007, 1), end = c(2015,8))
mv.ts<- cbind(pax.ts/1000000, price.ts, earnings.ts/1000, emp.ts/1000000)
dygraph(mv.ts, main = "Multivariate Time Series Plot") %>% dyRangeSelector() %>%
dyOptions(colors = RColorBrewer::brewer.pal(4, "Set1"))
pax.ts <- ts(phl$pax, frequency = 12, start = c(2007, 1), end = c(2015,8))
price.ts <- ts(phl$price, frequency = 12, start = c(2007, 1), end = c(2015,8))
earnings.ts <- ts(phl$earnings, frequency = 12, start = c(2007, 1), end = c(2015,8))
emp.ts <- ts(phl$emp, frequency = 12, start = c(2007, 1), end = c(2015,8))
mv.ts<- cbind(pax.ts/1000000, price.ts, earnings.ts/1000, emp.ts/1000000)
dygraph(mv.ts[,1:2], main = "Monthly PHL Pax (Millions) and Nat'l Fuel Prices") %>% dyRangeSelector()%>%
dyOptions(stackedGraph = TRUE) %>%
dyRangeSelector(height = 20) %>%
dyEvent("2008-07-01", "Start of Great Recession", labelLoc = "bottom")
pax.ts <- ts(phl$pax/1000, frequency = 12, start = c(2007, 1), end = c(2015,8))
dygraph(pax.ts, main="PHL Pax W/ Custom Moving Avg", ylab = "Monthly Passengers (000s)") %>% dyRangeSelector()%>%
dyRoller(rollPeriod = 1)
emp.ts <- ts(phl$emp, frequency = 12, start = c(2007, 1), end = c(2015,8))
dygraph(emp.ts, main="PHL Area Monthly Employment", ylab="Monthly Employment") %>% dyRangeSelector()%>%
dyShading(from = "2008-07-01", to = "2009-06-01") %>%
dyAnnotation("2008-12-01", text = "Great Recession", attachAtBottom = TRUE, width = 120) %>%
dyLegend(show = "follow")
pax.ts <- ts(phl$pax/1000, frequency = 12, start = c(2007, 1), end = c(2015,8))
pax.hw <- HoltWinters(pax.ts)
pred <- predict(pax.hw, n.ahead = 36, prediction.interval = TRUE)
all <- cbind(pax.ts, pred)
dygraph(all, main="PHL Monthly Pax", ylab = "Monthly Passengers (000s)") %>%
dySeries("pax.ts", label = "Actual") %>%
dySeries(c("pred.lwr", "pred.fit", "pred.upr"), label = "Predicted")
guide for using with R - https://plot.ly/r/
using with ggplot2 - https://plot.ly/ggplot2/
cheatsheet - https://images.plot.ly/plotly-documentation/images/r_cheat_sheet.pdf
race2 <- data %>% dplyr::filter(year == "2015") %>% group_by(race2) %>% summarise(total = n())
(p <- plot_ly(na.omit(subset(data, data$age > 0)), y=age, x = race2, color = race2, type = "box") %>%
layout(title = "Distribution of Victim Age by Race", xaxis = list(title="Race"), yaxis = list(title="Age")) %>%
layout(showlegend = FALSE))
pax.ts <- ts(phl$pax, frequency = 12, start = c(2007, 1), end = c(2015,8))
plot_ly(phl, x = date, y = pax, text = paste("Passengers: ", pax),
mode = "markers", color = month, size=pax) %>%
layout(title = "PHL Monthly Passengers", xaxis = list(title=" "), yaxis = list(title="Passengers"))
p <- plot_ly(phl, x = date, y = pax)
p %>% add_trace(y = fitted(loess(pax ~ as.numeric(date))), x = date) %>%
layout(title = "PHL Monthly Passengers", showlegend = FALSE,
xaxis = list(title=" "), yaxis = list(title="Passengers"))