Skip to content

Instantly share code, notes, and snippets.

#' An R function for creating simple D3 javascript directed network graphs.
#'
#' d3SimpleNetwork creates simple D3 javascript network graphs.
#'
#' @param data a data frame object with three columns. The first two are the names of the linked units. The third records an edge value. (Currently the third column doesn't affect the graph.)
#' @param Source character string naming the network source variable in the data frame. If \code{Source = NULL} then the first column of the data frame is treated as the source.
#' @param Target character string naming the network target variable in the data frame. If \code{Target = NULL} then the second column of the data frame is treated as the target.
#' @param height numeric height for the network graph's frame area.
#' @param width numeric width for the network graph's frame area.
#' @param file a character string of the file name to save the resulting graph. If a file name is given a standalone webpage is created, i.e. with a header and footer. If \code{file = NULL} then
#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',
# 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.",
# 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")
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
##############################################################################
## ##
## FORMATSNIPPETS.R ##
## ggplot format snippets ##
## ##
##############################################################################
#### LABELS ####
# Hide labels
# 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
@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')
# 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) {