Skip to content

Instantly share code, notes, and snippets.

# bubble plot - takes in a dataframe with five different properties, for X, Y, Size, ColorA, B and C, plus additional parameters to control appearances of the plots produced
bubbles_in_2colors<-function( dataset, to_jitter=F, prop_names=NULL, titles=list(main='X vs Y', color='colors', size='size'), min_size = 1, max_size = 5, max_sat = 200, fav_col='green', alpha_default=0.7, ...) {
# NB: this code can handle a third primary color, but human eyes can't so for the moment I've forbidden that option.
if(exists('dataset')) {
if (is.null(prop_names)) {print(prop_names<-names(dataset))};
if (length(prop_names) ==4) {propX<-dataset[,1]; propY<-dataset[,2]; propS<-dataset[,3]; propCA<-dataset[,4]}
else if (length(prop_names) ==5) {propX<-dataset[,1]; propY<-dataset[,2]; propS<-dataset[,3]; propCA<-dataset[,4]; propCB<-dataset[,5]}
else if (length(prop_names) ==6) {propX<-dataset[,1]; propY<-dataset[,2]; propS<-dataset[,3]; propCA<-dataset[,4]; propCB<-dataset[,5]; propCC<-dataset[,6]; print("Three colors a
library(ggplot2);
library(grid);
data(iris)
x <- jitter(iris[,c('Sepal.Length')])
y <- jitter(iris[,c('Sepal.Width')])
z <- factor(iris[,c('Species')])
# The color blind palette without black:
# My take on multi-variable pie charts. Inspired by:
# http://learnr.wordpress.com/2010/08/16/consultants-chart-in-ggplot2/
require(ggplot2)
require(reshape)
# let's create a dummy set
nvars<-3; varnames <- letters[1:nvars] # the number and names of the variables
ncpd<-16; cpdnames <- toupper(letters[1:ncpd]) # the number and name of item (in my case compounds)# a matrix filled with pseudorandom gibberish
MyMatrx<-matrix(ncol=nvars,nrow=ncpd,data=sample(5, repl=T, size=ncpd*nvars))
rownames(MyMatrx)<-cpdnames; colnames(MyMatrx)<-varnames
# alternatively, one could read such a data structure from a file...
@gufodotto
gufodotto / Plot_Bubbles_in_2_Colors.r
Created May 16, 2012 18:12
Example files for bubble plots
# generic routine to plot bubbles in different colors and sizes - run me using
# source(file.path(Sys.getenv("USERPROFILE"),"Documents/My Dropbox/Proj01_p100PassiveTransport/Data/Models/Plot_Bubbles_in_2_Colors.r"))
# if (exists('debug_flag') & debug_flag) {# cleans up everything but the debug_flag
# rm(list=ls(all=TRUE)[-which(ls()=='debug_flag')])
# } else {debug_flag<-F} # default for debug_flag is obviously FALSE
rm('File_In','Calc'); # initial cleanup - so that we read data from scratch
source(file.path(Sys.getenv("USERPROFILE"),"Documents/My Dropbox/Software/useful.r"))
# set up home directory
@gufodotto
gufodotto / gist:2819307
Created May 28, 2012 13:49
Vlaaivis.R (V2)
require(ggplot2)
require(reshape)
# windows()
getRandString<-function(len=12) return(paste(sample(c(LETTERS,letters),len,replace=TRUE),collapse=''))
cpd_name_len<-8
descr_name_len<-3
# let's create a dummy set
nvars<-4; varnames <- as.character(lapply(X=rep(descr_name_len,times=nvars), FUN=getRandString)) # the number and names of the variables
ncpd<-25; cpd_x_row<-5; rows_x_page<-6; cpd_x_page<-cpd_x_row*rows_x_page
library(deSolve) # require this library
Lorenz<-function(t, state, parameters) {
with(as.list(c(state, parameters)),{
# rate of change
dX <- a*X + Y*Z
dY <- b * (Y-Z)
dZ <- -X*Y + c*Y - Z
# return the rate of change
@gufodotto
gufodotto / gist:2863766
Created June 3, 2012 14:35
Plot_Lorenz
library(ggplot2)
pXY<-ggplot(as.data.frame(out)) +geom_path(aes(X, Y, col=time, alpha=Z)) + opts(legend.position = "none")
pZY<-ggplot(as.data.frame(out)) +geom_path(aes(Z, Y, col=time, alpha=X)) + opts(legend.position = "none")
pXZ<-ggplot(as.data.frame(out)) +geom_path(aes(X, Z, col=time, alpha=Y)) + opts(legend.position = c(1.1,0.5))
p3D<-ggplot(as.data.frame(out)) +theme_invisible() +geom_path(aes(X*Y, X*Z, col=time, alpha=Y*Z)) + scale_alpha(range = c(0.4, 0.8)) + opts(legend.position = 'none')
multiplot(pXY,pZY,pXZ,p3D, cols=2)
@gufodotto
gufodotto / gist:2863784
Created June 3, 2012 14:43
Lorentz_Controls
# source("C:/Users/Luca/Documents/My Dropbox/Software/gWidgets.r")
rm(list=ls(all=TRUE))
library(ggplot2)
library(gWidgets)
options("guiToolkit"="RGtk2")
library(deSolve)
# from: http://wiki.stdout.org/rcookbook/Graphs/Multiple%20graphs%20on%20one%20page%20(ggplot2)/
multiplot <- function(..., plotlist=NULL, cols) {
require(grid)
@gufodotto
gufodotto / BubbleExoplanets.r
Created June 22, 2012 14:27
BubbleExoplanets.r
# source("C:/Users/LucaF/Documents/My Dropbox/Software/R/Exoplanets.r")
rm(list=ls(all=TRUE))
if (!exists('palette_l')) {palette_l<-50} # how many steps in the palette
N<-650 # number of exoplanets
R<-1000 # radius of a 2D-circle where you'll want to plot them
MinMass<-5; # minimum mass for the planets
MaxMass<-100; # maximum mass for the planets
P<-6 # power exponent for the mass distribution
@gufodotto
gufodotto / gist:3068827
Created July 8, 2012 00:57
Lorentz_short
library(deSolve) # require this library
solveLorenz <- function(pars, times=seq(0,100,by=0.1)) {
derivs <- function(t, state, pars) { # returns rate of change
with(as.list(c(state, pars)), {
dX <- a*X + Y*Z
dY <- b * (Y-Z)
dZ <- -X*Y + c*Y - Z
return(list(c(dX, dY, dZ)))
}