Skip to content

Instantly share code, notes, and snippets.

View jonesor's full-sized avatar

Owen Jones jonesor

View GitHub Profile
@jonesor
jonesor / HWE_deFinetti.R
Last active October 6, 2021 19:08
A de Finetti diagram showing Hardy-Weinberg Equilibrium
# de Finetti diagram
library(dplyr)
library(ggplot2)
library(ggtern)
library(magrittr)
HWEfrequencies <- data.frame(
p = seq(0, 1, 0.01)
) %>%
mutate(q = 1 - p) %>%
@jonesor
jonesor / overlayHistogramDensity.R
Created November 9, 2018 13:18
Overlaying histograms and density plots in R (base and ggplot2)
#I first simulate a dataset to use for this example
set.seed(123)
years <- 2013:2015
n <- 100
df1 <- data.frame(year = rep(years, each = n),
eggdate = c(rnorm(n, 40, 4),
rnorm(n, 45, 5),
rnorm(n, 67, 7)))
#Make sure year is coded as a "factor"
@jonesor
jonesor / contour_plot.R
Created November 9, 2018 12:51
Make a contour plot with a heat map.
#Packages
library(ggplot2)
#Read in some data for the example
#THe data are grade data and acceptance in to grad school.
#GRE (Graduate Record Exam scores), GPA (grade point average)
mydata <- read.csv("https://stats.idre.ucla.edu/stat/data/binary.csv")
head(mydata)
summary(mydata)
mylogit <- glm(admit ~ gre + gpa + gre:gpa, data = mydata, family = "binomial")
@jonesor
jonesor / OffsetInLinearModel.R
Created November 9, 2018 08:52
Example of using an offset in a linear model
#offsets
#Simulate some data for this example
set.seed(12)
sig <- matrix(c(1.1,1,1,1.1),2,2)
df1<-data.frame(MASS::mvrnorm(n = 100, c(30,30), Sigma = sig ))
names(df1) <- c("x","y")
#Plot the data, and a 1:1 line
plot(df1$x,df1$y)
abline(0,1)
@jonesor
jonesor / subset_compadre_based_on_matrices.R
Created November 6, 2018 10:45
How to subset the COMPADRE/COMADRE matrix database based on the matrices themselves. This example counts NA values in the F matrix and uses that to subset.
#How to subset the matrix database based on the matrices themselves.
#This example counts NA values in the F matrix and uses that to subset.
nmat <- nrow(compadre$metadata)
for(i in 1:nmat){
compadre$metadata$NAinFmat[i] <- sum(is.na(compadre$mat[[i]]$matF))
}
table(compadre$metadata$NAinFmat)
x <- Rcompadre::subsetDB(compadre,NAinFmat == 0)
@jonesor
jonesor / barplot with error bars example.R
Created June 4, 2017 18:19
A simple example showing how to add error bars to a bar plot.
#bar heights
x<- c(1,2,3,2,4)
#standard error values
sem <- c(.5,.4,.2,.4,.2)
#bar labels
lab <- c("A","B","C","D","E")
@jonesor
jonesor / PointsInR.R
Created August 20, 2015 14:22
Point types available by default in R.
par(mar=c(1,1,1,1))
plot(x = rep(1:5,5),y = rep(1:5,each=5),pch=1:25,col="black",bg="yellow",axes=F,xlab="",ylab="",
cex=2,xlim=c(0,5.5),ylim=c(0,5.5))
text(x = rep(1:5,5),y = rep(1:5,each=5),text=1:25,pos=2,cex=.8)
@jonesor
jonesor / subsetComadre.R
Created August 6, 2015 14:15
How to subset COMADRE Animal Matrix Database
#How to subset the COMADRE Animal Matrix Database (works with COMPADRE too)
#Create a vector of indices to retain
subsetID <- which(comadre$metadata$Order %in% c("Monotremata","Didelphimorphia","Paucituberculata","Microbiotheria","Dasyurormorphia","Peramelemorphia","Diprotodontia"))
#Subset entire COMADRE object to JUST the above subset
#First make a copy
subset.comadre <- comadre
@jonesor
jonesor / palettebuildr.R
Last active August 29, 2015 14:15
Extract colours from a JPG file to use as a (divergent) colour palette.
palettebuildr <- function(pathToJPEG = "logo.jpg", ncols = 3, dist.method = "euclidian", clust.method = "complete"){
require(jpeg)
#Read in the jpeg file
img <- readJPEG(pathToJPEG)
#Using the whole image is overkill, especially for large files.
#Therefore, create a grid from which extract the colors
xgrid <- ceiling(seq(1, dim(img)[1], length.out = 50))
ygrid <- ceiling(seq(1, dim(img)[2], length.out = 50))
#Stochastic geometric population growth rate
#Simulation settings
pgr = 1.05
var.pgr = 0.1
startPop = 10
nGen = 100
ntrials = 1000
pseudoExtinction = 1