Skip to content

Instantly share code, notes, and snippets.

View kohske's full-sized avatar

kohske takahashi kohske

  • Ritsumeikan University
  • Osaka, Japan
  • X @kohske
View GitHub Profile
@kohske
kohske / gist:941662
Created April 26, 2011 01:54
new Guides example code
p <- ggplot(melt(outer(1:4, 1:4)), aes(x=X1, y=X2)) + geom_tile(aes(fill=value))
# default legend
png("guide_%02d.png", width=350, height=350)
p
# colorbar legend (vertical)
p + scale_fill_continuous(guide="colorbar")
# colorbar legend (horizontal)
p + scale_fill_continuous(guide="colorbar") + opts(legend.position="bottom", legend.direction="horizontal")
# change the size of legend
@kohske
kohske / gist:949367
Created April 30, 2011 03:18
new Guides example code - version 2
p <- function()ggplot(melt(outer(1:4, 1:4)), aes(x = X1, y = X2)) + geom_tile(aes(fill = value))
p2 <- function()ggplot(melt(outer(1:4, 1:4)), aes(x = X1, y = X2)) + geom_tile(aes(fill = value)) + geom_point(aes(size = value))
## legend
p()
## direction of guide
p() + opts(legend.direction = "horizontal")
@kohske
kohske / gist:985482
Created May 22, 2011 13:49
ggplot return object with stat and geom name.
g <- ggplot(mtcars, aes(cyl, mpg)) + geom_point() + stat_summary(fun.y=mean, geom="line")
d <- print(g)
## look up data from "stat"
get_data_from_stat <- function(gobj, stat) Find(function(x)attr(x, "stat")==stat, gobj$data)
## look up data from "geom"
get_data_from_geom <- function(gobj, geom) Find(function(x)attr(x, "geom")==geom, gobj$data)
get_data_from_stat(d, "summary")
@kohske
kohske / gist:987847
Created May 23, 2011 23:25
example for feature/coord-manual-lim in ggplot2
lims <- data.frame(vs=c(0,0,1,1), am=c(0,1,0,1),
xmin=c(6,2,2,2), xmax=c(10,10,6,4), ymin=c(10,15,17,21),ymax=c(20,26,25,34))
#ggplot(mtcars, aes(cyl, mpg)) + geom_point() +
# facet_grid(~vs+am, scale="free", lims=lims) # does not work with facet_grid
ggplot(mtcars, aes(cyl, mpg)) + geom_point() +
facet_wrap(~vs+am, scale="free", coord.limits=lims) + coord_cartesian(wise=T) # please try also wise=F
@kohske
kohske / gist:1016124
Created June 9, 2011 05:38
sample script
print(1)
x <- rnorm(10)
@kohske
kohske / gist:1047581
Created June 26, 2011 12:45
new Guides example code - version 3
## --------------------------------
## from guides.R
## --------------------------------
# ggplot object
dat <- data.frame(x=1:5, y=1:5, p=1:5, q=factor(1:5), r=factor(1:5))
p <- function()ggplot(dat, aes(x, y, colour=p, size=q, shape=r)) + geom_point()
# without guide specificatoin
@kohske
kohske / example codes
Created August 11, 2011 14:48
ggplot2: example codes for matrix-like legend guide
p <- qplot(1:20, 1:20, col=letters[1:20])
p + guides(col=guide_legend(nrow=8))
p + guides(col=guide_legend(ncol=8))
p + guides(col=guide_legend(nrow=8, byrow=T))
p + guides(col=guide_legend(ncol=8, byrow=T))
# output plots: http://yfrog.com/klhjpwp
# how to embed images in gist...?
@kohske
kohske / gist:1150934
Created August 17, 2011 06:34
How to install development version of ggplot2
# install devtools
install.packages('devtools')
# install dependency of scales package
install.packages(c("RColorBrewer", "stringr", "dichromat", "munsell", "plyr", "colorspace"))
# load devtools
library(devtools)
# move to development mode
@kohske
kohske / stat_aggr.R
Created November 5, 2011 07:03
stat_aggr2d and stat_aggrhex in ggplot2
# test with R 2.14.0 and ggplot2 0.8.9
library(ggplot2)
# define stat_aggr2d
StatAggr2d <- proto(Stat, {
objname <- "aggr2d"
default_aes <- function(.) aes(fill = ..value..)
required_aes <- c("x", "y", "z")
default_geom <- function(.) GeomRect
@kohske
kohske / MCIllusion.R
Created November 8, 2011 14:13
Merry Christmas Illusion
graphics.off()
W <- 5.5
H <- 5.5
parts <- function(x0, y0, BLfin, Size) {
Nfin <- 48
BLfin <- c(BLfin, 1-BLfin)
FSize <- 1