Tsukuba
- 18. うーむ。。。
age <- c("F0", "F1", "F2", "F3")
for(i in 1:4) {
plot(density(quine$Days[quine$Age == age[i]]),
col=i, lwd=3,
xlim=c(-20, 80), ylim=c(0, 0.08),
main="", xlab="", ylab="")
par("new"=TRUE)
}
title("Density of days in each age")
legend(40, 0.06, age, lwd=3, col=1:4)
Sunday, November 22, 2009
- 25. ちょっと変えるだけ
q <- ggplot(quine, aes(Days))
q <- q + geom_histogram(binwidth = 10)
q <- q + facet_grid(Sex ~ Age)
print(q)
Sunday, November 22, 2009
- 35. ggplotの基本 => layer
• Photoshopのレイヤーとかと似たような
もの
• レイヤー(層)を重ねていく
• “Statistics”などもlayerを返す
http://retouch-weblab.com/kennkyuu/layer/layer1.html
Sunday, November 22, 2009
- 36. layerを重ねる(1)
• q <- ggplot(mtcars, aes(qsec, wt))
• (ggplotのオブジェクトが生成されるだけ
で、plotはまだされない。)
Sunday, November 22, 2009
- 38. layerを重ねる(3)
• q <- q + geom_point()
• print(q)
• さっきのレイヤーに
dotのレイヤーを重ね
ている!
Sunday, November 22, 2009
- 40. ヒストグラム
• Geom:bar
• Stat:bin
• Scale:liner
• Coordinate
system:Cartesian
http://had.co.nz/ggplot2/resources/2007-vanderbilt.pdf
Sunday, November 22, 2009
- 48. prob.c.k.given.x <- function(x,y,k,data){
x1 <- data$x1; x2 <- data$x2
o <- rank(mapply(function(X1,X2){sqrt((X1-x)^2 + (X2-
y)^2)},x1,x2))
k.k <- sum(data$class[seq(length(o))[o <= k]] == "r")
return(k.k / k)
}
s <- seq(0, 1, length.out=100)
result <- as.data.frame(expand.grid(s, s))
result <- cbind(result, apply(result, 1, function(col) {
x <- col[1]; y <- col[2]
prob.c.k.given.x(x, y, 1, d)
}))
colnames(result) <- c("x", "y", "probability")
q <- ggplot(result, aes(x = x, y = y))
q <- q + geom_tile(aes(xmin = x, xmax = x + 0.01, ymin = y, ymax
= y + 0.01,
fill = probability), alpha = 0.1)
q <- q + geom_point(data = d, size = 2,
aes(x = x1, y = x2, color = factor(class)))
q <- q + scale_fill_gradient(low="red", high="blue")
print(q)
プログラム
Sunday, November 22, 2009
- 49. point
q <- ggplot(result, aes(x = x, y = y))
# geomsのレイヤー
q <- q + geom_tile(aes(xmin = x, xmax = x + 0.01,
ymin = y, ymax = y + 0.01,
fill = probability), alpha = 0.1)
# geomsのレイヤー
q <- q + geom_point(data = d, size = 2,
aes(x = x1, y = x2, color = factor(class)))
# scalesのレイヤー
q <- q + scale_fill_gradient(low="red", high="blue")
print(q)
Sunday, November 22, 2009
- 60. 背景変えられる?
• 変えられます
• see http://groups.google.com/group/
ggplot2/browse_thread/thread/
beff7d92f05dc741
Sunday, November 22, 2009