Skip to content

Instantly share code, notes, and snippets.

@monkeycycle
Created January 9, 2020 04:05
Show Gist options
  • Save monkeycycle/c23bf848010278fb08f36e8091884df6 to your computer and use it in GitHub Desktop.
Save monkeycycle/c23bf848010278fb08f36e8091884df6 to your computer and use it in GitHub Desktop.
R loop to generate dataframes and corresponding plots -- dirty but useful
# This loop generates a dataframe and plot for each offence
for(offence in cc_code_offences.l){
offence_slug <- slugify::slugify(offence, alphanum_replace = "", space_replace = "_", tolower = TRUE)
offence_slug <- gsub("__", "_", offence_slug, fixed=TRUE)
df_name <- paste(offence_slug, ".df", sep = "")
plot_name <- paste("p_", offence_slug, sep = "")
tmp.df <- filter_cc_offence(df=ywg_monthly_crime_data_2008_2019,
cc_code_offence.str = offence,
columns.l=offence_cols)
assign(df_name, tmp.df)
tmp.plot <- plot_cc_code_offence(tmp.df,
x_var = tmp.df$month_of_report_date,
y_var = tmp.df$distinct_count_of_incident,
y2_var = tmp.df$distinct_count_of_incidents_cleared,
y_colour_var = color_wfp_blue,
y2_colour_var = color_wfp_blue_pale,
smooth_colour_var = color_wfp_red_4,
group_var = tmp.df$district,
subtitle_str = offence,
plot_filename = plot_name
)
ggplot2::ggsave(filename=here::here(dir_plots, paste(plot_name, '.pdf', sep='')),
plot=tmp.plot,
width=8,
height=10,
units='in',
dpi = 300,
device='pdf',
useDingbats=FALSE,
bg="white"
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment