Skip to content

Instantly share code, notes, and snippets.

@dsparks
dsparks / eval_parse.R
Created November 26, 2012 02:12
Making code from strings
# A simple scatter plot:
with(mtcars, plot(hp, mpg, col = "red"))
# Let's use paste() to build that same command as a string
dataSet <- "mtcars"
xVar <- "hp"
yVar <- "mpg"
plotCodeString <- paste("with(", dataSet, ", plot(", xVar, ", ", yVar, "))",
sep = "") # We'll leave out the col option
@dsparks
dsparks / rcp_plot.R
Created November 16, 2012 11:54
Scraping and plotting RCP Polling Data
doInstall <- TRUE # Change to FALSE if you don't want packages installed.
toInstall <- c("XML", "ggplot2", "lubridate", "reshape2", "scales")
if(doInstall){install.packages(toInstall, repos = "http://cran.r-project.org")}
lapply(toInstall, library, character.only = TRUE)
# Find your XML file from those listed at
# http://cdn.realclearpolitics.com/epolls/charts/
URL <- "http://cdn.realclearpolitics.com/epolls/charts/1171.xml"
parsedXML <- xmlParse(URL) # First pass
@dsparks
dsparks / rcp_xml.R
Created November 16, 2012 11:05
RealClearPolitics XML Scraping
doInstall <- TRUE # Change to FALSE if you don't want packages installed.
toInstall <- c("XML", "ggplot2", "lubridate", "reshape2", "scales")
if(doInstall){install.packages(toInstall, repos = "http://cran.r-project.org")}
lapply(toInstall, library, character.only = TRUE)
# Find your XML file from those listed at
# http://cdn.realclearpolitics.com/epolls/charts/
URL <- "http://cdn.realclearpolitics.com/epolls/charts/1171.xml"
parsedXML <- xmlParse(URL) # First pass
@dsparks
dsparks / stat_2d.R
Created November 13, 2012 02:06
Encoding two-dimensional density in graphical variables
doInstall <- TRUE # Change to FALSE if you don't want packages installed.
toInstall <- c("ggplot2")
if(doInstall){install.packages(toInstall, repos = "http://cran.r-project.org")}
lapply(toInstall, library, character.only = TRUE)
# Generate some randomly-distributed data
nObs <- 5000
myData <- data.frame(X = rnorm(nObs), Y = rnorm(nObs))
nClusters <- 7 # Cluster it
kMeans <- kmeans(myData, centers = nClusters)
@dsparks
dsparks / electoral_map.R
Created November 7, 2012 13:44
Electoral map
doInstall <- TRUE # Change to FALSE if you don't want packages installed.
toInstall <- c("maps", "ggplot2", "RColorBrewer")
if(doInstall){install.packages(toInstall, repos = "http://cran.r-project.org")}
lapply(toInstall, library, character.only = TRUE)
new_theme_empty <- theme_bw() # Create our own, mostly blank, theme
new_theme_empty$line <- element_blank()
new_theme_empty$rect <- element_blank()
new_theme_empty$strip.text <- element_blank()
new_theme_empty$axis.text <- element_blank()
@dsparks
dsparks / color_picker.R
Created November 5, 2012 23:31
Color picker in R
doInstall <- TRUE # Change to FALSE if you don't want packages installed.
toInstall <- c("ggplot2", "proxy")
if(doInstall){install.packages(toInstall, repos = "http://cran.r-project.org")}
lapply(toInstall, library, character.only = TRUE)
nIncrements <- 36
eachIncrement <- seq(0, 1, len = nIncrements)
colorSweep <- expand.grid(eachIncrement, eachIncrement, eachIncrement)
moduloRemainder <- with(colorSweep, Var1*(nIncrements-1)) %%
floor(sqrt(nIncrements))
@dsparks
dsparks / stacking_wide_data.R
Created November 2, 2012 15:30
Stacking World Bank Data
doInstall <- TRUE # Change to FALSE if you don't want packages installed.
toInstall <- c("reshape2", "ggplot2")
if(doInstall){install.packages(toInstall, repos = "http://cran.r-project.org")}
lapply(toInstall, library, character.only = TRUE)
# Generate some data, which we'll load in a minute:
dataA <- matrix(rnorm(26*50), nrow = 26)
dataA <- t(apply(dataA, 1, cumsum))
colnames(dataA) <- 1960:2009
dataA <- data.frame(cName = LETTERS, cCode = 1:26*100, dataA)
@dsparks
dsparks / bright_object.R
Created October 30, 2012 15:48
Image Manipulation, Part 4
doInstall <- TRUE # Change to FALSE if you don't want packages installed.
toInstall <- c("ReadImages", "reshape", "ggplot2")
if(doInstall){install.packages(toInstall, repos = "http://cran.r-project.org")}
lapply(toInstall, library, character.only = TRUE)
imageLoader <- function(url){ # This function takes a URL, and generates a data.frame with pixel locations and colors
# Download to disk, load
download.file(url, "tempPicture.jpg", mode = "wb") # Stash image locally
readImage <- read.jpeg("tempPicture.jpg")
@dsparks
dsparks / voronoi_raster.R
Created October 30, 2012 15:41
Image Manipulation, Part 3
doInstall <- TRUE # Change to FALSE if you don't want packages installed.
toInstall <- c("ReadImages", "reshape", "ggplot2")
if(doInstall){install.packages(toInstall, repos = "http://cran.r-project.org")}
lapply(toInstall, library, character.only = TRUE)
# Image URL:
allImageURLs <- c("http://media.charlesleifer.com/blog/photos/thumbnails/akira_940x700.jpg",
"http://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/402px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"http://upload.wikimedia.org/wikipedia/commons/thumb/e/e9/Official_portrait_of_Barack_Obama.jpg/441px-Official_portrait_of_Barack_Obama.jpg",
"http://cache.boston.com/universal/site_graphics/blogs/bigpicture/obama_11_05/obama22_16604051.jpg",
@dsparks
dsparks / kmeans_palette.R
Last active June 27, 2019 00:10
Image Manipulation, Part 2
doInstall <- TRUE # Change to FALSE if you don't want packages installed.
toInstall <- c("jpeg", "reshape2", "ggplot2")
if(doInstall){install.packages(toInstall, repos = "http://cran.r-project.org")}
lapply(toInstall, library, character.only = TRUE)
# Image URL:
allImageURLs <- c("http://media.charlesleifer.com/blog/photos/thumbnails/akira_940x700.jpg",
"http://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/402px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"http://upload.wikimedia.org/wikipedia/commons/thumb/e/e9/Official_portrait_of_Barack_Obama.jpg/441px-Official_portrait_of_Barack_Obama.jpg",
"http://cache.boston.com/universal/site_graphics/blogs/bigpicture/obama_11_05/obama22_16604051.jpg",