Dummy text
library(lubridate)
##
## Attaching package: 'lubridate'
## The following object is masked from 'package:base':
##
## date
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:lubridate':
##
## intersect, setdiff, union
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(maptools)
## Loading required package: sp
## Checking rgeos availability: TRUE
library(stringr)
library(tidyr)
library(ggalt)
## Loading required package: ggplot2
library(ggplot2)
stops_sub <- read.csv("stops_sub.csv")
town_list <- c("East Haven",
"Waterbury",
"Windsor",
"Groton City",
"Branford",
"Darien",
"Norwalk",
"Stonington",
"New Haven")
for (i in 1:length(town_list)) {
stops_sub2 <- subset(stops_sub, Department.Name==town_list[i])
stops_sub2$RealTime2 <- ymd_hms(stops_sub2$RealTime2, tz="America/New_York")
#stops_sub2 <- subset(stops, Department.Name=="Bloomfield")
stops_sub3 <- stops_sub2
stops_sub3$RealTime2 <- NULL
stops_sub3$sunrise <- NULL
stops_sub3$solarnoon <- NULL
stops_sub3$sunset <- NULL
light_dark_df1 <- stops_sub3 %>%
group_by(ethnicity, light_dark) %>%
summarise(count=n()) %>%
spread(light_dark, count) %>%
mutate(dark_percent=(round(dark/(dark+light)*100,2)),light_percent=(round(light/(dark+light)*100,2)))
light_dark_df2 <- stops_sub3 %>%
group_by(ethnicity, light_dark) %>%
summarise(count=n()) %>%
spread(ethnicity, count) %>%
mutate(minority_percent=(round(Minority/(Minority+White)*100,2)),white_percent=(round(White/(Minority+White)*100,2)))
dark_percent <- light_dark_df2[1,4]
light_percent <- light_dark_df2[2,4]
department_name <- town_list[i]
p <- ggplot()
p <- p + geom_point(data=stops_sub2, aes(x=RealTime2, y=hours_since, colour=ethnicity))
p <- p + geom_rect(aes(xmin = as.POSIXct(strptime("2016-05-08 17:15", format = "%Y-%m-%d %H:%M")), xmax = as.POSIXct(strptime("2016-05-08 21:15", format = "%Y-%m-%d %H:%M")), ymin = 0, ymax = 4), alpha = 0.1, fill = "grey")
p <- p + geom_hline(yintercept = 0)
p <- p + theme_minimal(base_family="Arial Narrow")
p <- p + theme(plot.title=element_text(face="bold"))
p <- p + labs(x="Clock time", y="Hours since darkness",
title=paste0(department_name, " traffic stops around sunset"),
subtitle=paste0("During daylight hours, ", light_percent, "% of stops involved minority drivers; after dark, this figure changed to ", dark_percent, "%. \nThe large diagonal gap is a result of the shift from Eastern Daylight Time to Eastern Standard Time."),
caption="Traffic Stop Data Report, CCSU Institute for Municipal and Regional Policy")
print(p)
light_dark_df3 <- stops_sub3 %>%
group_by(ethnicity, light_dark) %>%
summarise(count=n())
g <-ggplot(light_dark_df3, aes(x=ethnicity, y=count, fill=light_dark)) +
geom_bar(stat="identity", position=position_dodge(), colour="black") +
coord_flip() +
labs(x="Traffic stops", y="Ethnicity",
title=paste0(department_name, " traffic stops: Daylight versus night"),
subtitle=paste0("During daylight hours, ", light_percent, "% of stops involved minority drivers; after dark, this figure changed to ", dark_percent, "%."),
caption="Traffic Stop Data Report, CCSU Institute for Municipal and Regional Policy")
print(g)
}