Created
June 26, 2011 12:45
-
-
Save kohske/1047581 to your computer and use it in GitHub Desktop.
new Guides example code - version 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## -------------------------------- | |
## 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 | |
p() | |
# Show colorbar guide for colour. | |
# All these examples below have a same effect. | |
p() + guides(colour = "colorbar", size = "legend", shape = "legend") | |
p() + guides(colour = guide_colorbar(), size = guide_legend(), shape = guide_legend()) | |
p() + scale_colour_continuous(guide="colorbar") + scale_size_discrete(guide="legend") + scale_shape(guide="legend") | |
# all guides are integrated if possible | |
p() + guides(colour = guide_legend("title"), size = guide_legend("title"), shape = guide_legend("title")) | |
# same as | |
g <- guide_legend("title") | |
p() + guides(colour = g, size = g, shape = g) | |
# style of guides | |
# position of guides | |
p() + opts(legend.position = "bottom") | |
# position of guides | |
p() + opts(legend.position = "bottom", legend.box = "horizontal") | |
## -------------------------------- | |
## from guide-legend.R | |
## -------------------------------- | |
# ggplot objects | |
p1 <- 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)) | |
# basic form | |
# short version | |
p1() + scale_fill_continuous(guide = "legend") | |
# long version | |
p1() + scale_fill_continuous(guide = guide_legend()) | |
# separately set the direction of each guide | |
p2() + scale_fill_continuous(guide = guide_legend(direction = "horizontal")) + | |
scale_size(guide = guide_legend(direction = "vertical")) | |
# guide title | |
p1() + scale_fill_continuous(guide = guide_legend(title = "V")) # title text | |
p1() + scale_fill_continuous(name = "V") # same | |
p1() + scale_fill_continuous(guide = guide_legend(title = FALSE)) # no title | |
# control styles | |
# key size | |
p1() + scale_fill_continuous(guide = guide_legend(keywidth = 3, keyheight = 1)) | |
# title position | |
p1() + scale_fill_continuous(guide = guide_legend(title = "LEFT", title.position = "left")) | |
# title text styles | |
p1() + scale_fill_continuous(guide = guide_legend(title = "ROTATE", title.position = "left", title.angle = 90, title.hjust = 0.5)) | |
# title text styles via theme_text | |
p1() + scale_fill_continuous(guide = guide_legend(title.theme = theme_text(size=15, face="italic", col="red", angle=45))) | |
# label position | |
p1() + scale_fill_continuous(guide = guide_legend(label.position = "bottom")) | |
# label styles | |
p1() + scale_fill_continuous(breaks=c(5, 10, 15), labels=paste("long", c(5, 10, 15)), | |
guide = guide_legend(direction="horizontal", title.position="top", title.theme=theme_text(hjust=0.5), | |
label.position="bottom", label.angle = 90, label.hjust=0.5, label.vjust=0.5)) | |
# set aesthetic of legend key | |
# very low alpha value make it difficult to see legend key | |
p3 <- function()ggplot(melt(outer(1:4, 1:4)), aes(x = X1, y = X2)) + geom_tile(aes(fill = value), alpha = 0.1) | |
p3() | |
# set.aes overwrites the alpha | |
p3() + scale_fill_continuous(guide=guide_legend(set.aes = list(alpha=1))) | |
# combine colorbar and legend guide | |
p2() + scale_fill_continuous(guide = "colorbar") + scale_size(guide = "legend") | |
# same, but short version | |
p2() + guides(fill = "colorbar", size = "legend") | |
## -------------------------------- | |
## from guide-legend.R | |
## -------------------------------- | |
## basic form | |
# short version | |
p1() + scale_fill_continuous(guide = "colorbar") | |
# long version | |
p1() + scale_fill_continuous(guide = guide_colorbar()) | |
# separately set the direction of each guide | |
p2() + scale_fill_continuous(guide = guide_colorbar(direction = "horizontal")) + | |
scale_size(guide = guide_legend(direction = "vertical")) ## separately set the direction of each gui | |
## control styles | |
# bar size | |
p1() + scale_fill_continuous(guide = guide_colorbar(barwidth=0.5, barheight=10)) | |
# no label | |
p1() + scale_fill_continuous(guide = guide_colorbar(label = FALSE)) | |
# no tick marks | |
p1() + scale_fill_continuous(guide = guide_colorbar(ticks = FALSE)) | |
# label position | |
p1() + scale_fill_continuous(guide = guide_colorbar(label.position = "left")) | |
# small number of bins | |
p1() + scale_fill_continuous(guide = guide_colorbar(nbin = 3)) | |
# large number of bins | |
p1() + scale_fill_continuous(guide = guide_colorbar(nbin = 100)) | |
# make top- and bottom-most ticks invisible | |
p1() + scale_fill_continuous(limits=c(0,20), breaks=c(0,5,10,15,20), | |
guide = guide_colorbar(nbin=100, draw.ulim = FALSE, draw.llim = FALSE)) | |
# combine colorbar and legend guide | |
p2() + scale_fill_continuous(guide = "colorbar") + scale_size(guide = "legend") | |
# same, but short version | |
p2() + guides(fill = "colorbar", size = "legend") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment