Skip to content

Instantly share code, notes, and snippets.

@sdtaylor
sdtaylor / weecology_trees.geojson
Last active April 29, 2019 19:31
Trees of Weecology
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@sdtaylor
sdtaylor / delete_extra_bib_stuff.R
Last active April 26, 2021 21:05
Delete unneeded bib items
library(bib2df)
x = bib2df('manuscript/sample.bib')
x$ANNOTE=NA
x$MENDELEY.GROUPS=NA
x$MENDELEY.TAGS=NA
x$ABSTRACT=NA
x$FILE=NA
x$KEYWORDS=NA
@sdtaylor
sdtaylor / .bashrc
Last active December 30, 2020 00:02
bashrc stuff
alias aptupdate='sudo apt-get update'
alias aptupgrade='sudo apt-get upgrade'
alias aptinstall='sudo apt-get install'
alias aptremove='sudo apt-get remove'
function csvhead {
head "$@" | sed 's/,,/, ,/g;s/,,/, ,/g' | column -t -s,
}
@sdtaylor
sdtaylor / usa_grid_map.R
Created March 12, 2019 17:15
Straightforward USA map with lat/lon grid.
library(tidyverse)
basemap = map_data('state')
lat_range = c(20,60)
lon_range = c(-150,50)
ggplot() +
geom_polygon(data = basemap, aes(x=long, y = lat, group = group), fill=NA, color='black', size=1.5) +
scale_x_continuous(breaks=seq(lon_range[1],lon_range[2],10), minor_breaks = seq(lon_range[1],lon_range[2],1)) +
@sdtaylor
sdtaylor / all_the_colors.R
Last active September 20, 2019 17:44
Generate discrete color values for numerous categories (>10)
library(ggplot2)
# Make a vector of colors of size n based on an RColorBrewer palette
# or list of colors.
get_extended_palette = function(original_palette = 'Paired', n){
brewer_palettes = rownames(RColorBrewer::brewer.pal.info)
if(length(original_palette)>=2){
original_palette_values = original_palette
} else if(original_palette %in% brewer_palettes){
original_palette_values = RColorBrewer::brewer.pal(9, original_palette) # most of the palettes have 9 colors to start with
@sdtaylor
sdtaylor / os_stuff.sh
Created June 1, 2020 12:54
R 4.0 on Ubuntu
# Run these first to get all the relevant packages
# For R 4.0 on ubuntio 18.04
# from https://www.digitalocean.com/community/tutorials/how-to-install-r-on-ubuntu-18-04
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9
sudo add-apt-repository 'deb https://cloud.r-project.org/bin/linux/ubuntu bionic-cran40/'
sudo apt-get update
sudo apt-get install r-base
# The following packages are also needed for various things in the tidyverse
@sdtaylor
sdtaylor / rotate-axis-labels-ggplot2.R
Created June 12, 2020 12:21 — forked from benmarwick/rotate-axis-labels-ggplot2.R
I can never remember how to rotate the x-axis labels with ggplot2: theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5))
# Adapted from https://stackoverflow.com/a/7267364/1036500 by Andrie de Vries
# This is it: theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5))
library(ggplot2)
td <- expand.grid(
hjust=c(0, 0.5, 1),
vjust=c(0, 0.5, 1),
angle=c(0, 45, 90),
@sdtaylor
sdtaylor / getNEONdata.R
Created July 8, 2020 16:07
neon data data download eample
library(tidyverse)
library(neonUtilities)
library(geoNEON)
sites = c('JORN','TALL','BART','BARR', 'HARV','YELL')
for(site_id in sites){
woody_structure=tryCatch(neonUtilities::loadByProduct('DP1.10098.001', site=site_id,
startdate = '2018-01', enddate = '2018-12',
@sdtaylor
sdtaylor / csv_to_sf.R
Last active December 17, 2020 22:59
csv to sf object
library(tidyverse)
library(sf)
# where lon, lat are columns for longitude, latitude, respectively.
# Any other columns are retained in the sf data.frame.
read_csv('site_list.csv') %>%
st_as_sf(coords = c('lon','lat'), crs=4326)
@sdtaylor
sdtaylor / massage_survival_data.R
Last active December 30, 2020 22:42
Massage survival data to different dates
library(tidyverse)
# Create a "date bin" data.frame with columns c('start','end')
# start_date, and last_date are dates
# time_bin_size should be string like '1 year','6 month', etc. see ?seq.Date for more examples
bin_dates = function(first_date, last_date, time_bin_size='1 year'){
if(!(lubridate::is.Date(first_date) & lubridate::is.Date(last_date))) stop('first_date and last_date must be dates')
if(first_date>=last_date) stop('first_date must come before last date')
full_range = seq.Date(first_date,last_date, by=time_bin_size)