######################################
#  Premise Breakdown
######################################
per_premises <- master %>%
  group_by(Premises) %>%
  summarise(total=n())
ggplot(data=per_premises, mapping= aes(x=reorder(Premises,total), y=total) ) +
  geom_bar(stat="identity") +
  coord_flip() +
  labs(title="Total crime reports in the Heights 2010-2017", y="Total incidents",x="Premise")

######################################
#  Stacked bar of incident type vs. premise
######################################
per_offense_premise <- master %>% 
  group_by(Premises, Offense_Type) %>% 
  summarize(offenses=n())
a = per_offense_premise %>%
  group_by(Premises) %>%
  summarise(ordering=sum(offenses))
per_offense_premise <-  
  left_join(per_offense_premise,a,by="Premises")
per_offense_premise$Premises <-
  reorder(per_offense_premise$Premises, per_offense_premise$ordering)
  
ggplot(data=per_offense_premise, mapping= aes(x=Premises, y=offenses) ) +
  geom_col(aes(fill=Offense_Type))+
  coord_flip() +
  labs(title="Total crime reports in the Heights 2010-2017", y="Total incidents by Type",x="Premise")

######################################
#  Stacked bar of incident type vs. premise
######################################
residential <- master %>%
  filter(Premises=="Residence")
per_month = residential %>% 
  mutate(mon = as.numeric(format(Date, "%m")), yr = as.numeric(format(Date, "%Y"))) %>%
  mutate(YrMon=yr+mon/12) %>%
  filter(YrMon>2010) %>%
  group_by(YrMon, Offense_Type) %>%
  summarize(total=n())
ggplot(data=per_month, mapping=aes(x=YrMon, y=total, color=Offense_Type)) +
  geom_point() +
  geom_smooth(method="lm") +
  labs(title="Total incidents in the Heights ", y="Total incidents per month", x="Averaged Monthly") +
  labs(subtitle="With linear regression")

######################################
#  Residential thefts by month
######################################
residential_month <- residential %>%
  filter(as.numeric(format(Date, "%Y"))<2017) %>%
  mutate(Month = format(Date, "%m"), mon = as.integer(format(Date,"%m"))) %>%
  group_by(mon, Month, Offense_Type) %>%
  summarize(total=n())
ggplot(data=residential_month, mapping=aes(x=Month, y=total, color=Offense_Type)) +
  geom_point() +
  geom_line(aes(x=mon, y=total)) +
  labs(title="Residential incidents in the Heights ", y="Incidents", x="Month")

######################################
#  Residential thefts in Nove & Dec
######################################
residential_NovDec <- residential %>%
  mutate(Month = format(Date, "%m"), Yr = as.integer(format(Date,"%Y"))) %>%
  filter((Month=="11") | (Month=="12")) %>%
  group_by(Yr, Offense_Type) %>%
  summarize(total=n())
ggplot(data=residential_NovDec, mapping=aes(x=Yr, y=total, color=Offense_Type)) +
  geom_point() +
  geom_smooth(method="lm", se=FALSE) +
  labs(title="November & December Residential Thefts", y="Thefts", x="Year")

######################################
#  Hour of day
######################################
master %>%
  group_by(Hour, Offense_Type) %>%
  summarize(total=n()) %>%
  ggplot(., aes(x=Hour, y=total, color=Offense_Type)) +
  geom_point() +
  geom_smooth() +
  labs(title="Incidents in the Heights by Hour of Day", x="Hour", y="Number of Incidents")

######################################
#  Day of week
######################################
master %>%
  mutate(Daynum=wday(Date)) %>%
  group_by(Daynum,Offense_Type) %>%
  summarize(total=n()) %>%
  ggplot(aes(x=wday(Daynum, label=TRUE), y=total, color=Offense_Type,group=Offense_Type)) +
  geom_line() +
  geom_point() +
  labs(title="Incidents in the Heights by Day of Week", x="Weekday", y="Number of Incidents")

######################################
#  Month of Year
######################################
master %>%
  filter(Date<"2017-1-1") %>%
  mutate(MonNum=month(Date)) %>%
  group_by(MonNum,Offense_Type) %>%
  summarize(total=n()) %>%
  ggplot(aes(x=month(MonNum, label=TRUE), y=total, color=Offense_Type,group=Offense_Type)) +
  #geom_smooth(method = "lm", formula = y ~ splines::bs(x, 3), se = FALSE) +
  geom_line() +
  geom_point() +
  labs(title="Incidents in the Heights by Month", x="Month", y="Number of Incidents")+
  labs(subtitle="2010 through 2016")

LS0tCnRpdGxlOiAiRmlyc3QgQW5hbHlzaXMiCm91dHB1dDoKICBodG1sX2RvY3VtZW50OgogICAgZGZfcHJpbnQ6IHBhZ2VkCi0tLQoKYGBge3Igc2V0dXAsIGluY2x1ZGU9RkFMU0V9CmxpYnJhcnkoImdncGxvdDIiKQpsaWJyYXJ5KCJsdWJyaWRhdGUiKQpsaWJyYXJ5KCJkcGx5ciIpCmtuaXRyOjpvcHRzX2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