Skip to content

Instantly share code, notes, and snippets.

@baptiste
baptiste / align_ggplot.r
Created April 13, 2012 22:50
fixed_width
library(ggplot2)
library(grid)
p1 <- qplot(1,1000, colour="test", linetype="longer legend") +
ylab("double\nline")
p2 <- qplot(1:10,1:10, colour="test") +
ylab("y") + guides(color="none")
@baptiste
baptiste / exprGrob.r
Created May 11, 2012 21:18
draw table of expressions
library(grid)
rowMax.units <- function(u, nrow){ # rowMax with a fake matrix of units
matrix.indices <- matrix(seq_along(u), nrow=nrow)
do.call(unit.c, lapply(seq(1, nrow), function(ii) {
max(u[matrix.indices[ii, ]])
}))
}
colMax.units <- function(u, ncol){ # colMax with a fake matrix of units
@baptiste
baptiste / expressionsColumn.r
Created May 19, 2012 22:56
table expressions
library(grid)
rowMax.units <- function(u, nrow){ # rowMax with a fake matrix of units
matrix.indices <- matrix(seq_along(u), nrow=nrow)
do.call(unit.c, lapply(seq(1, nrow), function(ii) {
max(u[matrix.indices[ii, ]])
}))
}
colMax.units <- function(u, ncol){ # colMax with a fake matrix of units
@baptiste
baptiste / colours.r
Created May 27, 2012 05:53
R code for blog post on R colours
library(gridExtra)
library(RColorBrewer)
pal1 <- 1:7
pal2 <- brewer.pal(7, "Set1")
pal3 <- brewer.pal(7, "Pastel1")
pal4 <- brewer.pal(7, "PRGn")
colorstrip <- function(x){
@baptiste
baptiste / fixed_width.r
Created June 3, 2012 22:45
fixed width gtable
library(ggplot2)
library(grid)
fixed_width <- function(p, width = unit(3, "cm"),
draw = FALSE){
gt <- ggplot_gtable(ggplot_build(p))
panel_index <- gt$layout$r[gt$layout$name == "panel"]
gt$widths[[panel_index]] <- width
@baptiste
baptiste / colorbar.r
Last active October 7, 2015 17:47
colorbar in grid
require(scales)
require(RColorBrewer)
require(grid)
##' greyscale palette function
##'
##' returns a palette function that maps values to grey
##' @title greyscale_palette
##' @export
##' @param d data giving the range of the palette
m = structure(list(theta = c(0, 0.602006688963212, 1.20401337792641,
1.80602006688964, 2.40802675585284, 3.01003344481606, 3.61204013377926,
4.21404682274247, 4.8160535117057, 5.4180602006689, 6.0200668896321,
6.62207357859532, 7.22408026755854, 7.82608695652174, 8.42809364548496,
9.03010033444817, 9.63210702341137, 10.2341137123746, 10.8361204013378,
11.438127090301, 12.0401337792642, 12.6421404682274, 13.2441471571906,
13.8461538461538, 14.448160535117, 15.0501672240803, 15.6521739130435,
16.2541806020067, 16.8561872909699, 17.4581939799331, 18.0602006688963,
18.6622073578595, 19.2642140468227, 19.866220735786, 20.4682274247492,
21.0702341137124, 21.6722408026756, 22.2742474916388, 22.876254180602,
@baptiste
baptiste / fun.r
Last active December 16, 2015 14:39
Conversion between `my.function`, `my_function`, `myFunction` to find which variant actually exists in the R search path
split_dot <- function(x)
strsplit(x, "\\.")[[1]]
is_dot <- function(x)
length(split_dot(x)) == 2L
split_underscore <- function(x)
strsplit(x, "_")[[1]]
is_underscore <- function(x)
@baptiste
baptiste / tablegrob
Created May 11, 2013 22:54
tablegrob
library(gtable)
library(plyr)
library(grid)
table_theme <- function(bg = c("grey95", "grey98"),
fg = c("black", "black"),
just=c("center","center"),
separator = list(h=FALSE, v=TRUE), box = FALSE,
core = list(bg=bg, fg=fg, separator=separator, box=box, just=just),
@baptiste
baptiste / adda.r
Last active December 17, 2015 10:09
R wrapper for adda runs
adda_spectrum = function(shape = "ellipsoid",
dimensions = c(1,1),
lambda = 0.5,
radius = 0.100,
m = 1.5 + 0.2i ,
medium.index = 1.46,
dpl = ceiling(10 * abs(m)),
test = TRUE) {
command <-paste("echo ./adda -shape ", shape, dimensions[1], dimensions[2],