Skip to content

Instantly share code, notes, and snippets.

View MCMaurer's full-sized avatar

Michael Culshaw-Maurer MCMaurer

View GitHub Profile
@MCMaurer
MCMaurer / ggplot_range_frame.R
Created February 20, 2018 22:51
Creating a Tufte-style range-frame scatterplot using ggplot
library(ggplot2)
library(ggthemes)
ggplot(mtcars, aes(wt,mpg))+
geom_point()+
theme_tufte()+
theme(axis.ticks = element_blank())+
xlab("Car weight (lb/1000)")+
ylab("Miles per gallon of fuel")+
scale_x_continuous(breaks = round(as.vector(quantile(mtcars$wt)), digits = 1))+
@MCMaurer
MCMaurer / Get_rid_of_non_numeric
Last active March 2, 2018 19:21
Simple gsub to get rid of non-numeric characters and then change to a numeric vector
x <- c("scaffold79", "scaffold30", "scaffold304")
new_x <- gsub("[^0-9]", "", x)
numeric_x <- as.numeric(new_x)
# for a data.frame
x <- NULL
x$scaffolds <- c("scaffold79", "scaffold30", "scaffold304")
x$scaffolds <- gsub("[^0-9]", "", x$scaffolds)
x$scaffolds <- as.numeric(x$scaffolds)
# download file from here http://getdrawings.com/line-drawing-of-animals#line-drawing-of-animals-16.jpg
library(raster)
library(tidyverse)
# make a raster from your downloaded image
test_raster <- raster("../../Downloads/line-drawing-of-animals-16.jpg")
# extract the coordinates with a low value (black lines from the original drawing)
coords <- coordinates(test_raster)[values(test_raster) < 200,]
# download file from here: https://www.dragoart.com/tuts/10861/2/1/86731/how-to-draw-a-conehead-katydid,-conehead-katydid-step-9.htm
# right click on the black and white drawing, and "save as" to a folder, it should save as a JPEG
library(raster)
library(tidyverse)
# make a raster from your downloaded image
test_raster <- raster("../../Downloads/katydid.jpg")
@MCMaurer
MCMaurer / find_packages.txt
Last active November 1, 2018 16:46
used this to look through an RMarkdown website repo to find all libraries used, and then write their names to a file so you can install them all. Just navigate to the repo and run this
grep -horE 'library\(.*?\)' . | sed 's/library//g; s/[()]//g; s/\'//g; s/"//g' | grep -Eo '^\w+' | sort | uniq
p <- 10^(seq(-4,0,0.2))
x <- 1:21
d <- cbind(x,p)
d <- d %>% as.data.frame()
loglogmodel <- lm(data = d, formula = log(p) ~ log(x))
ggplot(d, aes(x=x, y=p)) +
geom_point() +
geom_line(aes(x = exp(loglogmodel$model$`log(x)`), y = exp(loglogmodel$fitted.values)), color = "blue")
@MCMaurer
MCMaurer / find_rows_with_combo_of_values.R
Created December 14, 2018 23:06
Find all the rows in a dataframe where a certain sequence appears at any point
result <- matrix(nrow = nrow(mtcars), ncol = ncol(mtcars)-1)
for (i in 1:32) {
result[i,] <- rollapply(as.character(mtcars[i,]), 2, identical, c("1", "0"))
}
result2 <- vector(length = nrow(mtcars))
for (i in 1:32) {
result2[i] <- TRUE %in% result[i,]
}
# run this BEFORE you reinstall R/RStudio
old_packages <- list.files(.libPaths())
saveRDS(old_packages, "test_old_packages.rds")
# now you close RStudio and reinstall the new R and RStudio
# now you run THIS after you've installed R and RStudio anew
old_pkg_import <- readRDS("test_old_packages.rds")
pkgs_to_install <- unique(old_pkg_import)
#!/bin/bash
find . -size +100M | sed 's|^\./||g' | cat >> .gitignore; awk '!NF || !seen[$0]++' .gitignore
sort -u .gitignore > .gitignore_new && mv .gitignore_new .gitignore
# thanks to https://stackoverflow.com/questions/4035779/gitignore-by-file-size#comment67586426_5200267
library(tidyverse)
#devtools::install_github("AckerDWM/gg3D")
library("gg3D")
flights <- nycflights13::flights
flights %>%
drop_na() %>%
filter(dest %in% c("LAX", "SFO", "SMF")) %>%
ggplot(aes(x = hour, y = dep_delay, z = arr_delay, color = month, shape = origin)) +
axes_3D(theta = 280, phi = 20) +