Skip to content

Instantly share code, notes, and snippets.

View mollietaylor's full-sized avatar

Shawn Mollie Taylor mollietaylor

View GitHub Profile
@mollietaylor
mollietaylor / customlegend.R
Last active February 24, 2016 11:45
Using Line Segments to Compare Values in R
library(RColorBrewer)
colors = brewer.pal(8, "Dark2")
library(ggplot2)
data <- as.data.frame(USPersonalExpenditure) # data from package datasets
data$Category <- as.character(rownames(USPersonalExpenditure)) # this makes things simpler later
ggplot(data,
aes(x = Expenditure,
@mollietaylor
mollietaylor / elwyn.R
Last active April 16, 2016 02:01
Basemaps in R Using get_map
gps <- read.csv("elwyn.csv",
header = TRUE)
library(ggmap)
mapImageData <- get_map(location = c(lon = mean(gps$Longitude),
lat = 33.824),
color = "color", # or bw
source = "google",
maptype = "satellite",
# api_key = "your_api_key", # only needed for source = "cloudmade"
@mollietaylor
mollietaylor / file.R
Created January 7, 2013 03:45
Storing a Function in a Separate File in R
# calling the functions
# this is the file where we will call the functions in fun.R and times.R
times <- dget("times.R")
times(-4:4, 2)
source("fun.R")
mult(-4:4, 2)
@mollietaylor
mollietaylor / nationalParksArea.R
Created February 17, 2013 03:19
Shapefiles in R
# load up area shape file:
library(maptools)
area <- readShapePoly("ne_10m_parks_and_protected_lands_area.shp")
# # or file.choose:
# area <- readShapePoly(file.choose())
library(RColorBrewer)
colors <- brewer.pal(9, "BuGn")
@mollietaylor
mollietaylor / histdens2.R
Created September 23, 2012 03:17
Adding Measures of Central Tendency to Histograms in R
png("beaverhistextra.png")
layout(matrix(c(1:2), 2, 1,
byrow = TRUE))
hist(beaver1$temp, # histogram
col = "peachpuff", # column color
border = "black",
prob = TRUE, # show densities instead of frequencies
xlim = c(36,38.5),
ylim = c(0,3),
xlab = "Temperature",
@mollietaylor
mollietaylor / conventions.csv
Created September 8, 2012 03:09
Simple word cloud example in R
wordper25k democrats republicans
millionaires 7 0
arithmetic 3 0
equal pay 6 0
level playing field 2 0
bin laden 5 0
auto 18 1
fair 13 1
middle class 47 7
forward 33 6
@mollietaylor
mollietaylor / factorDummies.R
Created January 2, 2014 02:45
Compare Regression Results to a Specific Factor Level in R
str(ChickWeight$Diet)
table(ChickWeight$Diet)
ols <- lm(weight ~ Time + Diet,
data = ChickWeight)
summary(ols)
ChickWeight$Diet <- relevel(ChickWeight$Diet,
ref = 4)
@mollietaylor
mollietaylor / lineBreaks.R
Last active October 21, 2018 21:52
Line Breaks Between Words in Axis Labels in ggplot in R
library(OIdata)
data(birds)
library(ggplot2)
levels(birds$effect) <- gsub(" ", "\n", levels(birds$effect))
ggplot(birds,
aes(x = effect,
y = speed)) +
geom_boxplot() +
coord_flip()
@mollietaylor
mollietaylor / fit.R
Last active October 21, 2018 21:52
ggplot or Lattice Fit Line in R
ols <- lm(Temp ~ Solar.R,
data = airquality)
summary(ols)
str(ols)
plot(Temp ~ Solar.R,
data = airquality)
abline(ols)
@mollietaylor
mollietaylor / map-new-legend.R
Created September 29, 2013 23:02
Custom Legend in R
library(OIdata)
library(RColorBrewer)
library(classInt)
# load state data from OIdata package:
data(state)
# set constants:
nclr <- 8 # number of bins
min <- 0 # theoretical minimum