Skip to content

Instantly share code, notes, and snippets.

View pedroj's full-sized avatar
🌎
Fazendo ciência e soltando pipa

Pedro Jordano pedroj

🌎
Fazendo ciência e soltando pipa
View GitHub Profile
SankeyR <- function(inputs, losses, unit, labels, format="plot"){
########################
# SankeyR version 1.01 (updated August 10, 2010)
# is a function for creating Sankey Diagrams in R.
# See http://www.sankey-diagrams.com for excellent examples of Sankey Diagrams.ç
# This is based on a Gist by https://gist.github.com/aaronberdanier
#
# OPTIONS:
# 'inputs' is a vector of input values
# 'losses' is a vector of loss values
source_https <- function(url, ...) {
# load package
require(RCurl)
# parse and evaluate each .R script
sapply(c(url, ...), function(u) {
eval(parse(text = getURL(u, followlocation = TRUE, cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl"))),
envir = .GlobalEnv)
})
}
@pedroj
pedroj / multiplot.R
Created January 18, 2015 15:52
Multiple plots per page in ggplot2
# Multiple plot function
#
# ggplot objects can be passed in ..., or to plotlist (as a list of ggplot objects)
# - cols: Number of columns in layout
# - layout: A matrix specifying the layout. If present, 'cols' is ignored.
#
# If the layout is something like matrix(c(1,2,3,3), nrow=2, byrow=TRUE),
# then plot 1 will go in the upper left, 2 will go in the upper right, and
# 3 will go all the way across the bottom.
#
@pedroj
pedroj / mytheme_bw.R
Last active August 29, 2015 14:13
Basic ggplot2 theme
mytheme_bw <- function (base_size = 12, base_family = "")
{
theme_grey(base_size = base_size, base_family = base_family) %+replace%
theme(axis.text = element_text(size = rel(0.8)),
axis.ticks = element_line(colour = "black"),
legend.key = element_rect(colour = "grey80"),
panel.background = element_rect(fill = "white", colour = NA),
panel.border = element_rect(fill = NA, colour = "black"),
panel.grid.major = element_line(colour = "grey90", size = 0.2),
panel.grid.minor = element_line(colour = "grey98", size = 0.4),
#!/bin/bash
##############################################################################
# Name: templateR.sh
# Author: Frank Farach
# Description: Script to clone templateR repo from GitHub account,
# with options to name the local working directory and RStudio
# project. This depends on the existence of a repository called templateR
# in the GitHub account of user named in the global variable USER below.
# See: http://github.com/frankfarach/templateR
@pedroj
pedroj / Asset.R
Created April 6, 2013 00:16
Functions to generate adjacency matrix fills
# ----------------------------------------------------------------------
# [Title]: Functions to generate adjacency matrix fills useful in
# network analysis.
# [Date]: 1 Apr 2013 [Loc]: Sevilla
# Pedro Jordano.
# ......................................................................
# nullmat will generate a 0 matrix of size m x n.
# fullconnmat will generate a fully-connected matrix of size m x n.
# randommat will generate a randomly-filled matrix of size m x n.
# fullynest will generate a fully-nested matrix of size m x n.
@pedroj
pedroj / watts_strogatz_SW.R
Created March 26, 2013 01:43
watts_strogatz_SW
################################################################################
# Illustration of the Small-world property in the Watts-Strogatz model.
#-------------------------------------------------------------------------------
library(igraph)
set.seed(1)
avg.stat <- function(nei, p) {
result <- replicate(1000, {
wsg <- watts.strogatz.game(1, 100, nei, p)
c(average.path.length(wsg),
@pedroj
pedroj / canon_form_matrix.R
Created March 26, 2013 01:37
canon_form_matrix
######################################################################
# Function bdiag to build the canonical form of a matrix
######################################################################
# Pedro Jordano. 18 Dic 2007.
######################################################################
bdiag <- function(x){
if(!is.list(x)) stop("x not a list")
n <- length(x)
if(n==0) return(NULL)
@pedroj
pedroj / params.R
Created March 26, 2013 01:36
params
# Parameters
# Function to estimate network parameters.
require(bipartite)
params <- function(mat)
# Obtain basic parameters from an interaction matrix
# From library(bipartite) functions. We are getting just 8 indexes.
{networklevel(mat, #tapis
index=c(# "species",
"connectance", #1-C
"web asymmetry", #2-WA
@pedroj
pedroj / pairwise.R
Created March 25, 2013 16:37
pairwise vectorization
#########################################################
# Getting the pairwise interactions list
# Format: "Pant.species-Animal.species"
# assocs is a matrix (AxP). With rownames and colnames.
#
# pairwise(t(assocs)) will give the interactions list
# as: "Animal.species-Pant.species"
# Pedro Jordano. Sevilla - 15 Abr 2006 02:13:45.
#--------------------------------------------------------
pairwise<-function(assocs) {