{{ message }}

Instantly share code, notes, and snippets.

# mylesmharrison/toronto_datasci_plots.R

Last active Aug 29, 2015
Toronto Data Science Group - plots
 # Toronto Data Science Group Talk plots # Myles Harrison # http://www.everydayanalytics.ca/2014/02/toronto-data-science-group-talk.html library(hexbin) library(RColorBrewer) # Create random data x <- rnorm(5000, mean=1000) y <- rnorm(5000, mean=2000) # Scatterplot plot(x, y, pch=16, col='black') # Scatter with transparency plot(x, y, pch=16, col=rgb(0,0,0,0.1)) # Smaller plotting symbols plot(x, y, pch=16, col='black', cex=0.1) # Simulate kernel density estimation with Euclidean distance to means d <- sqrt((x-mean(x))^2 + (y-mean(y))^2) # Select palette and normalize to fit in color range palette(rainbow(32)) d <- d/max(d)*32+1 # Plot plot(x, y, pch=16, col=d) # Hexbinning h <- hexbin(x,y) plot(h) # With color plot(h, colramp=BTY) # DISTRIBUTION # Regular histogram hist(x, breaks=125, col='red', xlab='x', main='Histogram of x') # Boxplot x2 <- rnorm(1000, mean=1000) boxplot(x2) # Add jitter stripchart(x2,vertical=T,method="jitter",jitter=0.1,add=T, pch=16, cex=0.1, col=rgb(0,0,0,0.5)) # Bubble chart demo # Data from Flowing Data: http://flowingdata.com/2010/11/23/how-to-make-bubble-charts/ crime <- read.csv("http://datasets.flowingdata.com/crimeRatesByState2005.tsv", header=TRUE, sep="\t") radius <- sqrt( crime\$population/ pi ) plot(crime\$murder, crime\$burglary, col='red', pch=16, ylab='Burglary Rate', xlab='Murder Rate', xlim=c(0, 10), ylim=c(150, 1250)) symbols(crime\$murder, crime\$burglary, circles=radius, inches=0.35, fg="black", bg="red", xlab="Murder Rate", ylab="Burglary Rate", xlim=c(0, 10), ylim=c(150, 1250)) # Category-like colouring palette(colorRampPalette(c("red", "green", "darkgreen"))(3)) c <- round(runif(50)*2)+1 symbols(crime\$murder, crime\$burglary, circles=radius, inches=0.35, fg="black", bg=c, xlab="Murder Rate", ylab="Burglary Rate", xlim=c(0, 10), ylim=c(150, 1250)) # Quantitive-like colouring palette(colorRampPalette(c("lightblue", "yellow", "red"), interpolate='spline')(11)) c <- crime\$population/max(crime\$population)*10+1 symbols(crime\$murder, crime\$burglary, circles=radius, inches=0.35, fg="black", bg=c, xlab="Murder Rate", ylab="Burglary Rate", xlim=c(0, 10), ylim=c(150, 1250))