stops <- read.csv("data/mega.csv", stringsAsFactors=FALSE)
stops <- stops[!is.na(stops$InterventionLocationLatitude),]

stops$timeofday <- as.POSIXct(as.Date(stops$InterventionTime, origin="1899-12-30"))


stops$ethnicity <- ifelse(((stops$SubjectRaceCode ==  "W") & (stops$SubjectEthnicityCode =="N")), "White", "Minority")
stops$RE <- paste0(stops$SubjectRaceCode, stops$SubjectEthnicityCode)
stops$RE <- gsub("AH", "Hispanic", stops$RE)
stops$RE <- gsub("AM", "Middle_eastern", stops$RE)
stops$RE <- gsub("AN", "Asian", stops$RE)
stops$RE <- gsub("BH", "Black", stops$RE)
stops$RE <- gsub("BM", "Black", stops$RE)
stops$RE <- gsub("BN", "Black", stops$RE)
stops$RE <- gsub("IH", "Indian", stops$RE)
stops$RE <- gsub("IM", "Middle_eastern", stops$RE)
stops$RE <- gsub("IN", "Indian", stops$RE)
stops$RE <- gsub("WH", "Hispanic", stops$RE)
stops$RE <- gsub("WM", "Middle_eastern", stops$RE)
stops$RE <- gsub("WN", "White", stops$RE)

attr(stops$timeofday,"tzone") <- "EST"

forcarto <- stops[c("RE", "ethnicity", "InterventionDate", "timeofday", "InterventionLocationLatitude", "InterventionLocationLongitude")]
write.csv(forcarto, "data/forcarto.csv")

y2013vy2014 <- stops %>%
  group_by(DepartmentName, RE) %>%
  summarise(total=n()) %>%
  spread(RE, total) %>%
  mutate(total=sum(White+Black,Hispanic,Middle_eastern,Asian,Indian, na.rm=TRUE), white_p=White/total, black_p=Black/total, asian_p=Asian/total, hispanic_p=Hispanic/total)

y2013vy2014 <- y2013vy2014[c("DepartmentName", "white_p", "black_p", "asian_p", "hispanic_p")]

y2013vy2014 <- gather(y2013vy2014, "DepartmentName", "percent")
colnames(y2013vy2014) <- c("DepartmentName", "type", "percent2013")

this_year <- read.csv("data/mega_df.csv", stringsAsFactors=FALSE)
this_year <- subset(this_year, is.na(ReportingOfficerIdentificationID))

this_year <- this_year[c("DepartmentName", "white_p", "black_p", "asian_p", "hispanic_p")]

this_year <- gather(this_year, "DepartmentName", "percent2014")
colnames(this_year) <- c("DepartmentName", "type", "percent2014")

y2013vy2014 <- left_join(y2013vy2014, this_year)
## Joining by: c("DepartmentName", "type")
y2013vy2014$percent2014 <- y2013vy2014$percent2014/100
y2013vy2014$type <- gsub("_p", "", y2013vy2014$type)

department_list <- unique(y2013vy2014$DepartmentName)

y2013vy2014$type <- gsub("white", "White", y2013vy2014$type)
y2013vy2014$type <- gsub("black", "Black", y2013vy2014$type)
y2013vy2014$type <- gsub("asian", "Asian", y2013vy2014$type)
y2013vy2014$type <- gsub("hispanic", "Hispanic", y2013vy2014$type)

percent_first <- function(x) {
  x <- sprintf("%d%%", round(x*100))
  x[2:length(x)] <- sub("%$", "", x[2:length(x)])
  x
}
for (i in 1:length(department_list)) {
  dumbbell1 <- subset(y2013vy2014, DepartmentName==department_list[i])
  
dumbbell1$x_adjusted1 <- ifelse(((dumbbell1$percent2013-dumbbell1$percent2014<.02) & (dumbbell1$percent2013-dumbbell1$percent2014>0)), dumbbell1$percent2013+.02, dumbbell1$percent2013)
dumbbell1$x_adjusted1 <- ifelse(((dumbbell1$percent2014-dumbbell1$percent2013<.02) & (dumbbell1$percent2014-dumbbell1$percent2013>0)), dumbbell1$percent2013-.02, dumbbell1$x_adjusted1)

dumbbell1$x_adjusted2 <- ifelse(((dumbbell1$percent2013-dumbbell1$percent2014<.033) & (dumbbell1$percent2013-dumbbell1$percent2014>0)), dumbbell1$percent2014-.02, dumbbell1$percent2014)
dumbbell1$x_adjusted2 <- ifelse(((dumbbell1$percent2014-dumbbell1$percent2013<.02) & (dumbbell1$percent2014-dumbbell1$percent2013>0)), dumbbell1$percent2014+.02, dumbbell1$x_adjusted2)

gg <- ggplot()

gg <- gg + geom_segment(data=dumbbell1, aes(y=type, yend=type, x=0, xend=1), color="#b2b2b2", size=0.15)

gg <- gg + geom_dumbbell(data=dumbbell1, aes(y=type, x=percent2013, xend=percent2014),
                         size=1.5, color="#b2b2b2", point.size.l=3, point.size.r=3,
                         point.colour.l="tomato", point.colour.r="steelblue")

#   point.colour.l="tomato", point.colour.r="steelblue"
#gg <- gg + geom_lollipop(point.colour="steelblue", point.size=3, horizontal=TRUE)
gg <- gg + scale_x_continuous(expand=c(0,0), limits=c(0, 1))


# text below points
# gg <- gg + annotate(data=filter(dumbbell1, type=="White"),
#                      aes(x=x_adjusted1, y=type, label="2013"),
#                      color="tomato", size=6, vjust=-1, hjust=-.05, fontface="bold", family="Calibri")
# gg <- gg + geom_text(data=filter(dumbbell1, type=="White"),
#                      aes(x=x_adjusted2, y=type, label="2014"),
#                      color="steelblue", size=6, vjust=-1, hjust=.8, fontface="bold", family="Calibri")
# text above points

gg <- gg + geom_text(data=dumbbell1, aes(x=x_adjusted1, y=type, label=percent_first(percent2013)),
                     color="tomato", size=5, vjust=1.75, family="Calibri")

gg <- gg + geom_text(data=dumbbell1, color="steelblue", size=5, vjust=1.75, family="Calibri",
                     aes(x=x_adjusted2, y=type, label=percent_first(percent2014)),
                     color="tomato", size=5, vjust=1.75, family="Calibri")
# difference column

gg <- gg + scale_y_discrete(expand=c(.7,0))
gg <- gg + labs(x=NULL, y=NULL, title=paste("Traffic stops in", department_list[i], "by race"),
                caption="CCSU Institute for Municipal and Regional Policy Management")
gg <- gg + theme_bw(base_family="Calibri")
gg <- gg + theme(text = element_text(size=20))

gg <- gg + theme(panel.grid.major=element_blank())
gg <- gg + theme(panel.grid.minor=element_blank())
gg <- gg + theme(panel.border=element_blank())
gg <- gg + theme(text = element_text(size=20))
gg <- gg + theme(axis.ticks=element_blank())
gg <- gg + theme(axis.text.x=element_blank())
gg <- gg + theme(plot.title=element_text(face="bold", family="Lato Black", size=22))
gg <- gg + theme(plot.subtitle=element_text(face="italic", size=9, margin=margin(b=12)))
gg <- gg + theme(plot.caption=element_text(size=12, margin=margin(t=12), color="#7a7d7e"))

gg <- gg +   annotate("point", x = .7, y = 2.5, colour = "tomato", size = 2)  
gg <- gg +   annotate("point", x = .7, y = 1.5, colour = "steelblue", size = 2) 
gg <- gg +   annotate("text", x = .75, y = 2.5, label = "2013", size=5, colour="tomato") 
gg <- gg +   annotate("text", x = .75, y = 1.5, label = "2014", size=5, colour="steelblue")
  
print(gg)

hm <- gsub(" ", "", department_list[i])
hm <- str_to_lower(hm)
ggsave(gg, file = paste0("img/locations_", hm, ".png"), width = 8, height = 4, type = "cairo-png")


}
## Warning: The plyr::rename operation has created duplicates for the
## following name(s): (`colour`, `size`, `vjust`, `family`)
## Warning: Removed 1 rows containing missing values (geom_text).

## Warning: Removed 1 rows containing missing values (geom_text).
## Warning: The plyr::rename operation has created duplicates for the
## following name(s): (`colour`, `size`, `vjust`, `family`)

## Warning: Removed 1 rows containing missing values (geom_text).
## Warning: Removed 1 rows containing missing values (geom_text).
## Warning: The plyr::rename operation has created duplicates for the
## following name(s): (`colour`, `size`, `vjust`, `family`)

## Warning: Removed 1 rows containing missing values (geom_dumbbell).
## Warning: Removed 1 rows containing missing values (geom_text).

## Warning: Removed 1 rows containing missing values (geom_text).
## Warning: Removed 1 rows containing missing values (geom_dumbbell).
## Warning: Removed 1 rows containing missing values (geom_text).

## Warning: Removed 1 rows containing missing values (geom_text).
## Warning: The plyr::rename operation has created duplicates for the
## following name(s): (`colour`, `size`, `vjust`, `family`)