Skip to content

Instantly share code, notes, and snippets.

View mollietaylor's full-sized avatar

Shawn Mollie Taylor mollietaylor

View GitHub Profile
@mollietaylor
mollietaylor / dateFormats.R
Created August 17, 2013 19:58
Date Formats in R
# importing dates:
dates <- c("05/27/84", "07/07/05")
betterDates <- as.Date(dates,
format = "%m/%d/%y") # here you put the format your dates are currently in
# it will output the ISO standard dates (%Y-%m-%d)
# or:
dates <- c("May 27 1984", "July 7 2005")
betterDates <- as.Date(dates,
@mollietaylor
mollietaylor / log.R
Created June 23, 2013 22:58
Plot Weekly or Monthly Totals in R
library(ggplot2)
library(scales)
# load data:
log <- data.frame(Date = c("2013/05/25","2013/05/28","2013/05/31","2013/06/01","2013/06/02","2013/06/05","2013/06/07"),
Quantity = c(9,1,15,4,5,17,18))
log
str(log)
# convert date variable from factor to date format:
@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 / 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 / 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 / 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 / gini.R
Created January 2, 2013 04:08
Calculating a Gini Coefficients for a Number of Locales at Once in R
# generate random data:
city <- c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J")
income <- sample(1:100000,
100,
replace = TRUE)
cities <- data.frame(city, income)
# graph our data:
library(ggplot2)
ggplot(cities,
@mollietaylor
mollietaylor / stacked.R
Created December 11, 2012 23:55
Stacked Bar Charts in R
# cite: http://stat.ethz.ch/R-manual/R-patched/library/stats/html/reshape.html
# cite: http://www.cs.grinnell.edu/~rebelsky/Courses/MAT115/2008S/R/stacked-bar-graphs.html
# cite: http://www.harding.edu/fmccown/r/#barcharts
library(RColorBrewer)
sequential <- brewer.pal(6, "BuGn")
Loblolly[1:10,]
wide <- reshape(Loblolly,
gps <- read.csv("out.csv",
header = TRUE)
library(ggmap)
mapImageData <- get_googlemap(center = c(lon = median(gps$Longitude), lat = median(gps$Latitude)),
zoom = 11,
# size = c(500, 500),
maptype = c("terrain"))
ggmap(mapImageData,