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)
@ChgoChad
Copy link

ChgoChad commented Jan 9, 2012

In line 11 above there is a small error that prevents this code from running successfully in R.

Line 11 should read: freq <- table(PRIMARY.DESCRIPTION)
(The word description is misspelled in the original)

Make this one small change and this script will run perfectly and produce the intended results in R.

@Chicago
Copy link
Author

Chicago commented Jan 17, 2012

Got it. The code had originally compensated for an error in the data but I forgot to update this once it was fixed. Thanks!

@geneorama
Copy link

Update using R 4.0 with pipes, and $limit in the query (because now the crime data is huge).

The original example is better in many ways, but I wanted to record the syntax for $limit, which requires another library and a change in the URL structure (limit doesn't work with rows.csv)

## define needed libraries, check if they're installed, install if not, then load
libs <- c("googleVis", "jsonlite")
setdiff(libs, rownames(installed.packages())) |> sapply(install.packages)
sapply(libs, library, character.only = T)

## 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()

@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