Skip to content

Instantly share code, notes, and snippets.

@baptiste
baptiste / doubleaxis.r
Last active December 20, 2015 13:49
double axis hack in ggplot2
library(gtable)
library(ggplot2)
library(plyr)
set.seed(1)
d <- data.frame(x=rep(1:10, 5),
y=rnorm(50),
g = gl(5,10))
# example plot
@baptiste
baptiste / knit
Last active December 20, 2015 19:39
Minimal example of a Rmd file that can be processed to produce both pdf and html output through pandoc, using two different file formats for the figures.
#!/usr/bin/Rscript
require(knitr)
lf <- commandArgs(trailingOnly = TRUE)
if(!length(lf))
lf = list.files(pattern="\\.rmd")
for (f in lf)
try(knit(f))
md = paste0(sub("\\.[[:alnum:]]*$", "", lf), ".md")
@baptiste
baptiste / gtable_arrange.r
Last active December 23, 2015 14:59
implementing grid.arrange using gtable
gtable_arrange <- function(..., grobs=list(), as.table=TRUE,
top = NULL, bottom = NULL,
left = NULL, right = NULL, draw=TRUE){
require(gtable)
# alias
gtable_add_grobs <- gtable_add_grob
dots <- list(...)
params <- c("nrow", "ncol", "widths", "heights",
"respect", "just", "z") # TODO currently ignored
@baptiste
baptiste / gtable_layout.r
Last active December 26, 2015 01:19
arrange grobs in a gtable from a layout specification
gtable_layout <- function(grobs, widths = NULL, heights = NULL,
m, ...){
if(is.null(widths))
widths <- unit(rep(1,ncol(m)), "null")
if(is.null(heights))
heights <- unit(rep(1,nrow(m)), "null")
cells <- sort(unique(c(m)))
@baptiste
baptiste / colours.r
Created November 26, 2013 13:48
two-coloured title
require(ggplot2)
require(grid)
element_grob.element_custom <- function(element, label="", colour="black") {
disect <- strsplit(label, "\\n")[[1]]
N <- length(disect)
fun <- function(ii, colour)
textGrob(disect[ii], y=unit(N, "lines") - unit(ii-1, "lines"), gp=gpar(col=colour))
ask = function(msg){
h <- paste(capture.output(help(msg)), collapse="")
print(h)
if(nchar(msg) < 20){
m <- paste0(stringr::str_extract_all(h,"\\?")[[1]], collapse="")
Recall(m)
}
}
ask("???")
global_settings { assumed_gamma 1.2 }
#include "colors.inc"
#include "textures.inc"
#include "glass.inc"
#declare R = 3;
#declare Rs = 0.05;
#declare Rd = R - Rs;
#declare dist = 1.0;
//////
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
library(grid)
lollipopGrob <- function(x, y, size, turns=3){
theta <- seq(0, turns*2*pi, length=120)
xx <- unit(x, "npc") + unit(size*theta/max(theta)*cos(theta+pi), "mm")
yy <- unit(y, "npc") + unit(size*theta/max(theta)*sin(theta+pi), "mm")
segmentsGrob(head(xx, -1), head(yy, -1), tail(xx, -1), tail(yy, -1),
gp=gpar(col=hcl(seq(0,360,length=12), 90, 70), lwd=5))
#' symmetrise_scale
#'
#' @param p ggplot2
#' @param axis axis
#'
#' @return plot with scales adjusted
#' @export
#' @examples
#' p1 <- qplot(mpg, wt, data=mtcars, colour=cyl) + facet_wrap(~carb, nrow=1, scales="free")