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 / gyakutan.R
Last active August 29, 2015 14:17
Gaykutan.R
# http://blog.goo.ne.jp/r-de-r/e/c9cc759642ea0c02560bd08c975fb156
b = 0.111111 * 1:9 # b は 0.111111 ~ 0.999999 の 9 種類
bz = b+0.000001
b2 = 2 * b
bz2 = 2*bz
b.sq = b ^ 2
bz.sq = bz ^ 2
n = 10000 # 探索範囲上限
ansa = c()
@kohske
kohske / spfw.R
Created April 2, 2015 12:15
separate plot of facet_wrap in ggplot2
library(gtable)
d=data.frame(g=c(1,1,1,2,2,3),x=c(1,2,3,1,2,3),y=1:6);
p = ggplot(d,aes(x,y))+geom_bar(stat="identity")+facet_wrap(~g)
gt= ggplot_gtable(ggplot_build(g))
graphics.off()
for (p in c(4, 7, 10)) {
pdf(paste0("plot-", p, ".pdf"))
grid.draw(gt[,c(1:3, p, 12)])
@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 / 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
@kohske
kohske / gist:1393952
Created November 25, 2011 16:47
example of RWeather and ggplot2
library(plyr)
library(ggplot2) # needs dev version of ggplot2 for the colorbar
if(0){
locs <- getCountriesFromGoogle(country="JP")
weas <- llply(locs$name, function(l) {
r <- NULL
try(r <- getWeatherFromGoogle(l))
r
})