Monday, November 12, 2012

illustrate the crime trends across years

One day I was asked for help to illustrate and compare the crime trends for 4 communities in Chicago which are Beverly Hill, Washington Heights, Mount Greenwood and Morgan Park. The crime count data is from the Policy Department of Chicago. It gives all crimes happened in each community from year 2001 to 2010.

Personally, I feel it would not be hard to make a quick plot, but it worth putting some time to make this small task look nicer. Therefore, I decided to use ggplot2 and make the plot in R.

Hope someone who also wants to make similar plots like I did can eventually find my code and procedure useful. If anyone has other interesting ideas to make the plots nicer or more informative, please leave your comment :)



library(ggplot2) #import ggplot2 library
dat <- read.csv("community_crime.csv",header=T)
dat$community = factor(dat$community,
labels=c("Beverly Hill","Washington Heights","Mount Greenwood","Morgan Park")) 

X11(width = 18, height = 12) 

ggplot(dat, aes(x=year, y=any_1000, color=community)) + 
geom_point(shape=19,size=I(8),alpha = 0.3) +

scale_colour_hue(l=50) + # Use a slightly darker palette than normal

geom_smooth(method=lm,   # Add linear regression lines

se=FALSE,    # Don't add shaded confidence region

fullrange=T, size=1.4)+ # Extend regression lines

ylab("all crime count per 1000 population")+

xlab("Year")+

opts(title = expression("All Crime Count per 1000 population"))


No comments:

Post a Comment