Skip to content

Instantly share code, notes, and snippets.

@Chicago
Created August 23, 2011 14:29
Show Gist options
  • Save Chicago/1165275 to your computer and use it in GitHub Desktop.
Save Chicago/1165275 to your computer and use it in GitHub Desktop.
Crimes - Sample R code - Pie Chart
##Quick and easy way to visualize the crimes data set using open source techniques
##Have more ideas? Tell me about them at @ChicagoCDO
##All you need to do is install R at http://cran.r-project.org/ and run this file
install.packages("googleVis")
library(googleVis)
crimes_stage <- read.csv("http://data.cityofchicago.org/views/x2n5-8w5q/rows.csv")
attach(crimes_stage)
##remove first row with column names
crimes <- crimes_stage[-c(1),]
##create frequency table from crime
freq <- table(PRIMARY.DESCRIPTION)
##change frequency table to data frame
freq.data <- as.data.frame(freq)
pie <- gvisPieChart(freq.data,options=list(height=1200,width=1200))
## Appreciate visual output
plot(pie)
@geneorama
Copy link

I think it's a little overly complicated for purposes of demonstration to include code that automatically installs libraries, this example is cleaner:

## Load required libraries
library("googleVis")
library("jsonlite")

## Plot employee departments
"http://data.cityofchicago.org/resource/xzkq-xp2w.json" |> 
    fromJSON() |> 
    subset(select="department") |> 
    table() |> 
    as.data.frame() |> 
    gvisPieChart(options=list(height=1200,width=1200)) |> 
    plot()

## Plot crimes "primary type", limited to 1000
"http://data.cityofchicago.org/resource/x2n5-8w5q.json?$limit=1000" |> 
    fromJSON() |> 
    subset(select="_primary_decsription") |> 
    table() |> 
    as.data.frame() |> 
    gvisPieChart(options=list(height=1200,width=1200)) |> 
    plot()

Run install.packages("googleVis") and / or install.packages("jsonlite") if the libraries are missing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment