Skip to content

Instantly share code, notes, and snippets.

View mollietaylor's full-sized avatar

Shawn Mollie Taylor mollietaylor

View GitHub Profile
@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 / 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 / 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 / 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 / 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 / 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 / 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 / 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 / callan.R
Last active January 19, 2016 07:09
Elevation Profiles in R
gps <- read.csv("callan.csv",
header = TRUE)
# calculate moving average: (this section is optional)
library(TTR)
movingN <- 5 # define the n for the moving average calculations
gps$Altitude <- gps$Altitude * 3.281 # convert m to ft
gps$SMA <- SMA(gps$Altitude,
n = movingN)
gps <- gps[movingN:length(gps$SMA), ] # remove first n-1 points
@mollietaylor
mollietaylor / cities-coords.csv
Last active January 4, 2016 03:59
Merge by City and State in R
City State Latitude Longitude
San Francisco CA 37.7782251 -122.4424955
New York NY 40.7142691 -74.0059729
Los Angeles CA 34.0522342 -118.2436849
Chicago IL 41.850033 -87.6500523
Dallas TX 32.7830556 -96.8066667
Columbus GA 32.4609764 -84.9877094
Columbus OH 39.9611755 -82.9987942