knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(knitr)
library(lavaan)
library(psych)
library(MBESS)
library(xtable)
library(semPlot)
require(car)
library(dplyr)
library(lattice)
library(lavaanPlot)
Load the packages
library(RSocrata)
library(tidyverse)
library(dplyr)
Use the API to connect to Open Data LA
base_url = "https://data.lacity.org/resource/63jg-8b9z.json?"
my_token <- "XXXXXXXXXXXXXXXXXXXXX"
fdf <- read.socrata(base_url, my_token)
glimpse(fdf)

Filter only crimes with ‘child’ in the description perpetrated against children between 6 and 12
childcrimes <- fdf %>%
mutate(vict_age = as.numeric(vict_age)) %>%
filter(vict_age >=6 & vict_age < 13) %>%
filter(str_detect(crm_cd_desc, 'Child|CHILD|child'))
Plot histogram of crimes by age
ggplot(childcrimes, mapping = aes(vict_age)) + geom_bar()

crimebyarea <- childcrimes %>%
dplyr::group_by(crm_cd_desc, area_name) %>%
dplyr::summarise(count= n())
crimebyarea <- na.omit(crimebyarea)
wide_crimebyarea <- crimebyarea %>% spread(key = area_name, value = count)
wide_crimebyarea[is.na(wide_crimebyarea)] <- 0
wide_crimebyarea$total_col = apply(wide_crimebyarea[,-1], 1, sum)
wide_crimebyarea$percent_Mission <- wide_crimebyarea$Mission / wide_crimebyarea$total_col
ggplot(wide_crimebyarea, aes(x= reorder(crm_cd_desc, percent_Mission),
y = percent_Mission, fill = percent_Mission, group = 1)) +
geom_bar(stat = "identity") +
ylab("Percentage of All Crimes") +
xlab("Crime Subtypes") + scale_x_discrete(labels = c("CHILD ABANDONMENT" = "CA", "CHILD ABUSE (PHYSICAL) - AGGRAVATED ASSAULT" = "PCA_AGG",
"CHILD ABUSE (PHYSICAL) - SIMPLE ASSAULT" = "PCA_SA",
"CHILD ANNOYING (17YRS & UNDER)" = "ANNOY", "CHILD NEGLECT (SEE 300 W.I.C.)" = "NEG",
"CHILD PORNOGRAPHY" = "PORN", "CHILD STEALING" = "STEAL",
"LEWD/LASCIVIOUS ACTS WITH CHILD" = "CSA")) +
labs(title = "Crimes Against Children Aged 6-12 in Mission Hills",
subtitle = "2010 - 2018, City of Los Angeles")+
theme(axis.text.x = element_text(size=12))+
theme(axis.text.y = element_text(size = 12))+
theme(axis.title.x = element_text(size=12,color = "#424242"))+
theme(axis.title.y = element_text(size = 12,color = "#424242"))+
theme(plot.title = element_text(lineheight=3, color="black", face = "bold", size=16, hjust =0.5),
plot.subtitle=element_text(size=14, hjust = 0.5))
