Skip to content

Instantly share code, notes, and snippets.

@dsparks
dsparks / tapered-intensity-curved_edges.R
Last active December 8, 2021 12:23
Beautiful tapered-intensity-curved edge network graph with ggplot2
doInstall <- TRUE # Change to FALSE if you don't want packages installed.
toInstall <- c("sna", "ggplot2", "Hmisc", "reshape2")
if(doInstall){install.packages(toInstall, repos = "http://cran.r-project.org")}
lapply(toInstall, library, character.only = TRUE)
# Empty ggplot2 theme
new_theme_empty <- theme_bw()
new_theme_empty$line <- element_blank()
new_theme_empty$rect <- element_blank()
new_theme_empty$strip.text <- element_blank()
@dsparks
dsparks / geocoded_Tweets.R
Created December 18, 2012 17:15
Gathering Tweets, geocoding users, and plotting them
doInstall <- TRUE
toInstall <- c("twitteR", "dismo", "maps", "ggplot2")
if(doInstall){install.packages(toInstall, repos = "http://cran.us.r-project.org")}
lapply(toInstall, library, character.only = TRUE)
searchTerm <- "#rstats"
searchResults <- searchTwitter(searchTerm, n = 1000) # Gather Tweets
tweetFrame <- twListToDF(searchResults) # Convert to a nice dF
userInfo <- lookupUsers(tweetFrame$screenName) # Batch lookup of user info
@dsparks
dsparks / pretty_network.R
Created December 12, 2012 17:39
Graphing a subset of isDotR's Twitter ego net.
doInstall <- TRUE
toInstall <- c("sna", "igraph")
if(doInstall){install.packages(toInstall, repos = "http://cran.us.r-project.org")}
lapply(toInstall, library, character.only = TRUE)
adjacencyList <- read.csv("https://dl.dropbox.com/s/8wabcrtqxysp03u/Twitter_network.R.csv?dl=1")
head(adjacencyList)
adjacencyMatrix <- table(adjacencyList)
as.matrix(sort(rowSums(adjacencyMatrix))) # Out-degree
@dsparks
dsparks / Herfindahl–Hirschman_Index.R
Created December 11, 2012 18:54
Effective number of parties
doInstall <- TRUE # Change to FALSE if you don't want packages installed.
toInstall <- c("plyr", "ggplot2")
if(doInstall){install.packages(toInstall, repos = "http://cran.r-project.org")}
lapply(toInstall, library, character.only = TRUE)
ANES <- read.csv("http://www.oberlin.edu/faculty/cdesante/assets/downloads/ANES.csv")
head(ANES)
ANES$PID3 <- factor(ANES$pid7) # Convert to three-level Party ID:
levels(ANES$PID3) <- c("Dem", "Dem", "Dem", "Ind", "Rep", "Rep", "Rep")
@dsparks
dsparks / lme4_arm_example.R
Created December 11, 2012 18:05
Denver debate analysis II
rm(list = ls())
doInstall <- TRUE # Change to FALSE if you don't want packages installed.
toInstall <- c("zoo", "tm", "ggplot2", "lme4", "arm", "Snowball")
if(doInstall){install.packages(toInstall, repos = "http://cran.r-project.org")}
lapply(toInstall, library, character.only = TRUE)
# From: http://www.cnn.com/2012/10/03/politics/debate-transcript/index.html
Transcript <- readLines("https://raw.github.com/dsparks/Test_image/master/Denver_Debate_Transcript.txt")
head(Transcript, 20)
@dsparks
dsparks / tm_example.R
Created December 11, 2012 16:43
Denver debate analysis I
rm(list = ls())
doInstall <- TRUE # Change to FALSE if you don't want packages installed.
toInstall <- c("zoo", "tm", "ggplot2", "Snowball")
if(doInstall){install.packages(toInstall, repos = "http://cran.r-project.org")}
lapply(toInstall, library, character.only = TRUE)
# From: http://www.cnn.com/2012/10/03/politics/debate-transcript/index.html
Transcript <- readLines("https://raw.github.com/dsparks/Test_image/master/Denver_Debate_Transcript.txt")
head(Transcript, 20)
@dsparks
dsparks / sna_example.R
Created December 11, 2012 13:34
Flag similarity network
# Drawing a scatter plot of raster images
doInstall <- TRUE # Change to FALSE if you don't want packages installed.
toInstall <- c("png", "sna")
if(doInstall){install.packages(toInstall, repos = "http://cran.r-project.org")}
lapply(toInstall, library, character.only = TRUE)
# Go to https://www.gosquared.com/resources/2400-flags, download the ZIP,
# and put the 64 x 64 files into a directory of your choosing.
# Then setwd() to that directory:
setwd("C:/Dropbox/isDotR_Files/flagIcons")
@dsparks
dsparks / fanny_example.R
Created December 11, 2012 04:34
Clustering breakfast foods
doInstall <- TRUE
toInstall <- c("ggplot2", "cluster", "MASS", "smacof")
if(doInstall){install.packages(toInstall, repos = "http://cran.us.r-project.org")}
lapply(toInstall, library, character.only = TRUE)
# Generate a matrix of dissimilarities from pairwise correlations
data(breakfast) # Preference orderings of breakfast items, from smacof
corrMat <- cor(breakfast, use = "pair")
distMat <- dist(corrMat)
@dsparks
dsparks / advent_calendar
Created December 11, 2012 02:23
Drawing and saving the Advent CalendaR
library(ggplot2)
new_theme_empty <- theme_bw()
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()
#new_theme_empty$plot.title <- element_blank()
new_theme_empty$axis.title <- element_blank()
new_theme_empty$plot.margin <- structure(c(0, 0, -1, -1), unit = "lines", valid.unit = 3L, class = "unit")
@dsparks
dsparks / SMACOF_MDS.R
Created December 10, 2012 18:48
Scaling thermometer ratings
doInstall <- TRUE
toInstall <- c("ggplot2", "smacof")
if(doInstall){install.packages(toInstall, repos = "http://cran.us.r-project.org")}
lapply(toInstall, library, character.only = TRUE)
ANES <- read.csv("https://raw.github.com/dsparks/Test_image/master/2008%20ANES%20Thermometers.csv")
head(ANES)
# Multidimensional scaling of a rectangular thermometer rating matrix
thermometerFrame <- ANES[, -c(1:2)]