Skip to content

Instantly share code, notes, and snippets.

## GOAL:
## re-create a figure similar to Fig. 2 in Wilson et al. (2018),
## Nature 554: 183-188. Available from:
## https://www.nature.com/articles/nature25479#s1
##
## combines a boxplot (or violin) with the raw data, by splitting each
## category location in two (box on left, raw data on right)
## call required packages
# somewhat hackish solution to:
# https://twitter.com/EamonCaddigan/status/646759751242620928
# based mostly on copy/pasting from ggplot2 geom_violin source:
# https://github.com/hadley/ggplot2/blob/master/R/geom-violin.r
library(ggplot2)
library(dplyr)
"%||%" <- function(a, b) {
@benmarwick
benmarwick / GCMSAgilentDfileImport
Last active March 22, 2024 16:57
Function to import Agilent GCMS Chemstation D files in R
##' Function readDFile
##'
##' Function readDFile
##' @param pathname the pathname of the directory containing the data to import
##' @return outData Output is a matrix of ion counts with rows as scantime and
##' columns as mass, and the respective values as labels
##' @export
readDFile<-function(pathname){
filename<-file.path(pathname,'DATA.MS')
# function to do a dodged half-boxplot and jittered points next to each other
#
# data_in should be a data frame
# factor_col should be a bare column name (not a string)
# although it will work if that column is factor or a character type
# numeric_col is the y axis continuous variable
# offset is the width of the boxplots and jittered point cloud
#
# the basic approach is to draw a boxplot without the tails
# (e.g. only the interquartile range) and then use segments to add the
##############################################################################
## ##
## FORMATSNIPPETS.R ##
## ggplot format snippets ##
## ##
##############################################################################
#### LABELS ####
# Hide labels
This gist is no longer updated, see this one the for the most current version:
https://gist.github.com/benmarwick/6260541
# This script is a workflow for analysing 2D artefact outlines from 3D
# scan objects captured by NextEngine and ScanStudio. Part of the process
# occurs in ScanStudio and GIMP and the quantative analysis of the outlines
# is done in R. In GIMP we process the images into B&W silhouettes ready for R.
# In R we do elliptical fourier analysis to summarise the image outlines
# then PCA and MANOVA to discriminate between shape variations
# and test for differences
# Better alternatives to 3-D
# Here are some examples of what we don't want to do:
# http://stackoverflow.com/questions/12765497/create-a-ribbon-plot
doInstall <- TRUE # Change to FALSE if you don't want packages installed.
toInstall <- c("ggplot2")
if(doInstall){install.packages(toInstall, repos = "http://cran.r-project.org")}
lapply(toInstall, library, character.only = TRUE)
# Air passenger data. ts converted to long matrix:
library(ggplot2)
library(gridExtra)
mtcars$cyl <- ordered(mtcars$cyl)
p <- ggplot(mtcars, aes(mpg, hp, colour = cyl)) + geom_point()
p1 <- p + theme(legend.position = "none")
p2 <- ggplot(mtcars, aes(x=mpg, group=cyl, colour=cyl))
p2 <- p2 + stat_density(fill = NA, position="dodge")
# load required libraries
library(tm)
library(ggplot2)
library(lsa)
# 1. Prepare mock data
text <- c("transporting food by cars will cause global warming. so we should go local.",
"we should try to convince our parents to stop using cars because it will cause global warming.",
"some food, such as mongo, requires a warm weather to grow. so they have to be transported to canada.",
"a typical electronic circuit can be built with a battery, a bulb, and a switch.",
#get data from google sheet
# connect to google sheet
require(RCurl)
options(RCurlOptions = list(capath = system.file("CurlSSL", "cacert.pem", package = "RCurl"), ssl.verifypeer = FALSE))
#in google spreadsheet, go to file-> publish to web -> get link to publish to web -> get csv file
goog <- "https://docs.google.com/spreadsheet/pub?key=0As7CmPqGXTzldFRsVi1VZ2EyNXJ1ZEV5SG5GSExwRHc&single=true&gid=5&output=csv"
data <- read.csv(textConnection(getURL(goog)), stringsAsFactors = FALSE)
# extract just data for plotting: pH, SOM, CaCO3, MS-LF, MS-FD
plotting_data <- na.omit(data[,c('Sample.number',