Tidyuesday

#Bibliotecas necesarias
library(tidytuesdayR)
library(tidyverse)
library(ggsci)
library(gganimate)
library(ggthemes)
library(ggmap)
library(osmdata)
library(ggpointdensity)
#Lectura de Datos
#tt <- tt_load(2019,49) 
#tt_dat <- tt$tickets 
#save(tt_dat,file = "tickets.Rdata")
load("tickets.Rdata")
df <- tt_dat %>% mutate_if(is.character,as.factor) %>% 
  filter(!is.na(issuing_agency)) %>% 
  mutate(temp = gsub("2017-","",issue_datetime)) %>% 
  separate(temp,c("month","temp2"),sep = "-") %>% 
  select(-c(temp2,issue_datetime)) 
df_polar <- df %>% 
  ungroup() %>% 
  group_by(issuing_agency,month) %>% 
  summarise(total_fine = sum(fine)) %>% 
  mutate(month = as.numeric(month))
g <-ggplot(df_polar,aes(x = reorder(issuing_agency,total_fine),y = total_fine , fill =issuing_agency))+
  geom_bar(stat = "identity") +
  scale_y_log10(breaks =c(10,100,1000,10000,100000),label=c("10","100","1000","10000","100000")) +
  scale_fill_jco()+
  labs(title = "Total Fines Per Agency",
       subtitle = "month: {frame_time}",
       caption = "Vizualization by @DuvanNievesRui1 | Data: 'Philly Parking Tickets' • Open Data Philly",
       x ="Agency",
       y= "Total fine")+
  coord_polar()+
    theme(panel.grid = element_line(color = "grey20"),
      axis.ticks.y = element_line(),
        legend.position = "none",
        panel.background = element_rect(fill="grey10",color = "grey10"),
        plot.background = element_rect(fill="grey10"),
        panel.spacing = unit(2, "lines"),
        plot.title = element_text(size=28, color="grey76",hjust = .5),
        plot.subtitle  = element_text(size=20, color="grey76"),
        plot.caption = element_text(size = 14,color = "grey76", hjust = .98),
        axis.text = element_text(family = "Roboto Mono",
                                 size = 10,
                                 colour = "grey76"), 
        strip.text.x =element_text(family = "Roboto Mono",
                                   size = 14,
                                   colour = "grey76"), 
        axis.title =  element_text(family = "Roboto Mono",
                                   size = 20,
                                   colour = "white"),
        legend.text = element_text(family = "Roboto Mono",
                                   size = 10,
                                   colour = "grey76"),
        legend.title = element_text(family = "Roboto Mono",
                                   size = 14,
                                   colour = "grey76")) +
  transition_components(month) +
  enter_fade()+
  exit_fade()

animate(g, renderer = gifski_renderer(),height = 600, width =600,fps = 2)

map_phi <- getbb("Philadelphia")
  
df_map <- df %>%
  #filter(str_detect(violation_desc,"METER EXPIRED CC")) %>% 
  mutate_at(c("lat","lon"),~round(.x,3)) %>% 
  ungroup() %>% 
  group_by(lat,lon,month) %>% 
  summarise(total_fine = sum(fine)) %>% 
  filter(between(lon,map_phi[1,1],map_phi[1,2]),between(lat,map_phi[2,1],map_phi[2,2])) 

water.sp1 <- opq(map_phi) %>% 
  add_osm_feature("water",c("lake","pond","stream")) %>% 
  osmdata_sf()

water.sp2 <- opq(map_phi) %>% 
  add_osm_feature("waterway","river") %>% 
  osmdata_sf()

road.sp1 <- opq(map_phi) %>% 
  add_osm_feature('highway', c('motorway', 'primary', 'secondary', 'tertiary')) %>% 
  osmdata_sf()

road.sp2 <-  opq(map_phi) %>% 
  add_osm_feature('highway', c('residential', 'living_street', 'unclassified', 'service', 'foodway')) %>%     
  osmdata_sf()
#if(!requireNamespace("devtools")) install.packages("devtools")
#devtools::install_github("dkahle/ggmap", ref = "tidyup", force=TRUE)

#register_google(key = "your key")

#philly.to <- get_map(c(-75.175, 39.950), zoom=13, source = "stamen", maptype= "toner" ,crop = T)
#philly.tr <- get_map(location=c(left = -75.25, bottom = 39.90, right = -75.10, top = 40), zoom=13, source = "stamen", maptype="terrain",crop = T)
#philly.hy <- get_googlemap(c(-75.175, 39.950), zoom=13, source = "google", maptype="hybrid",crop = T)
#map.list <- list(philly.to, philly.tr,philly.hy)
#save(map.list,file = "map_list.Rdata")


load(file = "map_list.Rdata")

for (i in map.list) {
  a <-  ggmap(i) + 
  stat_density2d(aes(x = lon, y = lat, fill = ..level.., alpha=.25),
                 size = 0.001, bins = 30, data = df_map,
                 geom = "polygon", alpha = .03)+
  scale_fill_viridis_c(option = "C",direction = -1) +
  theme_map()+
  labs(title = "Philadelphia Parking Violations",
       subtitle = "Total fines in 2017",
       caption = "Vizualization by @DuvanNievesRui1 | Data: 'Philly Parking Tickets' • Open Data Philly") +
  theme(legend.position = "none",
        plot.margin = unit(c(1,.7,.25,.7),'cm'),
        panel.background = element_rect(fill="grey10",color = "grey10"),
        plot.background = element_rect(fill="grey10"),
        plot.title = element_text(size=40, color="grey76"),
        plot.subtitle  = element_text(size=26, color="grey76"),
        plot.caption = element_text(size = 20,color = "grey76", hjust = .98))
  print(a)
}

ggplot(df_map) +
  geom_sf(data = road.sp2$osm_lines, col = 'grey60', size = .2) +
  geom_sf(data = road.sp1$osm_lines, col = 'grey60', size = .8) +
  geom_sf(data = water.sp1$osm_multipolygons, color = alpha("#0077be", .2), size= 15, fill =alpha("#0077be", .2) ) +
  geom_sf(data = water.sp2$osm_lines, color = alpha("#0077be", .2), size= 20) +
  geom_pointdensity(aes(lon, lat), size = 2, alpha = .3) +
  geom_sf(data = road.sp2$osm_lines, col = alpha('grey60', .2), size = .2) +
  geom_sf(data = road.sp1$osm_lines, col = alpha('grey60', .2), size = .8) +
  scale_color_gradient2(low = "limegreen",high = "red4", mid = "limegreen") +
  coord_sf(xlim = map_phi[1,], ylim = map_phi[2,], expand = F) +
  theme_map() +
  labs(title = "Philadelphia Parking Violations",
       subtitle = "Total fines per pixel in 2017",
       caption = "Vizualization by @DuvanNievesRui1 | Data: 'Philly Parking Tickets' • Open Data Philly")+
  theme(legend.position = "none",
        plot.margin = unit(c(1,.7,.25,.7),'cm'),
        panel.background = element_rect(fill="grey10",color = "grey10"),
        plot.background = element_rect(fill="grey10"),
        plot.title = element_text(size=40, color="grey76"),
        plot.subtitle  = element_text(size=26, color="grey76"),
        plot.caption = element_text(size = 20,color = "grey76", hjust = .98))