Skip to content

Instantly share code, notes, and snippets.

@adamhsparks
adamhsparks / happy_biRthday.R
Created April 16, 2014 06:18
Happy birthday in R
lapply((1:4), function(x){paste(c("Happy Birthday ", ifelse(x!=3, "to you", "happy, happy birthday, NAME!")), sep="", collapse="")})
@adamhsparks
adamhsparks / Download and create raster stack CRU CL2.0 Data.R
Last active January 5, 2017 00:28
This R script will download and calculate min/max T and generate raster stacks of precipitation, min/max/mean temperature.
# 05/01/2017
# This script has been superceded by the getCRUCL2.0 R package which offers extended functionality
# see the GitHub repository for this package for more:
# https://github.com/adamhsparks/getCRUCL2.0
##############################################################################
# title : Download and create raster stack CRU CL2.0 Data.R;
# purpose : create R raster stack of CRU CL 2.0 data;
# producer : prepared by A. Sparks;
# last update : in Los Baños, Laguna, PI May 2014;
@adamhsparks
adamhsparks / Compare_EPIRICE_Outputs.R
Last active August 29, 2015 14:03
Compare output from EPIRICE when using 1º and 0.25º data
##############################################################################
# title : Compare_EPIRICE_Outputs.R;
# purpose : Compare output from EPIRICE when using 1º and 0.25º data;
# producer : prepared by A. Sparks;
# last update : in Los Baños, Laguna, Jun. 2014;
# inputs : EPIRICE output from 2001-2008;
# outputs : histograms and graphs of comparison for leaf blast at 1º and 0.25º;
# remarks 1 : ;
# Licence: : GPL2;
##############################################################################
@adamhsparks
adamhsparks / ggplot_table_plot.R
Last active January 6, 2018 10:10
ggplot and table of data both in same figure
# Create some sample data
CV_1 <- 0.2
CV_2 <- 0.3
Mean <- 65
sigma_1 <- sqrt(log(1 + CV_1^2))
mu_1 <- log(Mean) - sigma_1^2 / 2
sigma_2 <- sqrt(log(1 + CV_2^2))
mu_2 <- log(Mean) - sigma_2^2 / 2
q <- c(0.25, 0.5, 0.75, 0.9, 0.95)
Q <- data.frame(Quantile=paste0(100*q,"%ile"),
@adamhsparks
adamhsparks / same_size_map.R
Created June 29, 2015 06:32
Use ggplot2 to plot maps using a common key and all the same size
grid.draw(cbind(ggplotGrob(map1), ggplotGrob(map2), ggplotGrob(map3), size = "last"))
@adamhsparks
adamhsparks / stacked_map_R_ggplot2.md
Created April 20, 2016 11:37 — forked from rafapereirabr/stacked_map_R_ggplot2.md
Creating a stacked map in R using ggplot2

This gist shows in two steps how to tilt and stack maps using ggplot2 in order to create an image like this one: [![enter image description here][1]][1]

Let's load the necessary libraries and data to use a reproducible example:

# load libraries
  library(rgeos)
  library(UScensus2000tract)
  library(ggplot2)
@adamhsparks
adamhsparks / orig.png
Created April 24, 2016 12:06 — forked from hrbrmstr/orig.png
Supreme Annotations - moar splainin here: http://rud.is/b/2016/03/16/supreme-annotations/ - NOTE: this requires the github version of ggplot2
orig.png
# data from http://ec.europa.eu/eurostat/web/gisco/geodata/reference-data/population-distribution-demography/geostat
# Originally seen at http://spatial.ly/2014/08/population-lines/
# So, this blew up on both Reddit and Twitter. Two bugs fixed (southern Spain was a mess,
# and some countries where missing -- measure twice, submit once, damnit), and two silly superflous lines removed after
# @hadleywickham pointed that out. The result of the code below can be seen at http://imgur.com/nhVJqwk
library(tidyverse)
read_csv('../data/geostat-2011/GEOSTAT_grid_POP_1K_2011_V2_0_1.csv') %>%
@adamhsparks
adamhsparks / LB_forecasters.R
Created April 27, 2017 20:25
Late blight forecasters from the cropsim package
# cropsim
# https://r-forge.r-project.org/scm/?group_id=335
#
# Potato late blight forecast models
# Model name: Hyre
# Hyre, R. A. 1954. Progress in forecasting late blight of potato and tomato. Plant Disease Reporter. 38: 245-253.
# R implementation by Robert Hijmans, [email protected]
# October 2009
scatter_violin <- ggplot(data = dat, aes(x = condition, y = value)) +
geom_violin(aes(fill = condition, color = condition)) +
geom_crossbar(stat = "summary", fun.y = mean, fun.ymax = mean, fun.ymin = mean, fatten = 2, width = .5) +
geom_point(color = "black", size = 1, position = position_jitter(w = 0.05)) +
theme_minimal()
# original: https://inattentionalcoffee.wordpress.com/2017/02/14/data-in-the-raw-violin-plots/