Created
August 23, 2011 14:29
-
-
Save Chicago/1165275 to your computer and use it in GitHub Desktop.
Crimes - Sample R code - Pie Chart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
##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) |
Got it. The code had originally compensated for an error in the data but I forgot to update this once it was fixed. Thanks!
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()
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
In line 11 above there is a small error that prevents this code from running successfully in R.
Make this one small change and this script will run perfectly and produce the intended results in R.