Skip to content

Instantly share code, notes, and snippets.

@kohske
Created April 30, 2011 03:18
Show Gist options
  • Save kohske/949367 to your computer and use it in GitHub Desktop.
Save kohske/949367 to your computer and use it in GitHub Desktop.
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")
p() + scale_fill_continuous(guide = guide_legend(direction = "horizontal"))
p2() + opts(legend.direction = "horizontal") ## set the direction of all guides
p2() + scale_fill_continuous(guide = guide_legend(direction = "horizontal")) +
scale_size(guide = guide_legend(direction = "vertical")) ## separately set the direction of each guide
## guide title
p() + scale_fill_continuous(guide = guide_legend(title = "V")) # title text
p() + scale_fill_continuous(name = "V") # same
p() + scale_fill_continuous(guide = guide_legend(title = FALSE)) # no title
## some styles control
p() + scale_fill_continuous(guide = guide_legend(keywidth = 3, keyheight = 1)) # key size
p() + scale_fill_continuous(guide = guide_legend(title = "LEFT", title.position = "left")) # title pos
p() + scale_fill_continuous(guide = guide_legend(title = "ROTATE", title.position = "left", title.angle = 90, title.hjust = 0.5)) # title text styles
p() + scale_fill_continuous(guide = guide_legend(title.theme = theme_text(size=15, face="italic", col="red", angle=45))) # title text styles via theme_text
p() + scale_fill_continuous(guide = guide_legend(label.position = "bottom")) # lable pos
p() + 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.theme = theme_text(angle = 90, hjust=1))) # label pos etc.
## colorbar
p() + scale_fill_continuous(guide = "colorbar")
p() + scale_fill_continuous(guide = guide_colorbar(direction = "horizontal")) # direction
p2() + opts(legend.direction = "horizontal") ## set the direction of all guides
p2() + scale_fill_continuous(guide = guide_colorbar(direction = "horizontal")) +
scale_size(guide = guide_legend(direction = "vertical")) ## separately set the direction of each gui
## some style control
p() + scale_fill_continuous(guide = guide_colorbar(barwidth=0.5, barheight=10)) # bar size
p() + scale_fill_continuous(guide = guide_colorbar(label = FALSE)) # no label
p() + scale_fill_continuous(guide = guide_colorbar(ticks = FALSE)) # no ticks
p() + scale_fill_continuous(guide = guide_colorbar(label.position = "left")) # change label pos
p() + scale_fill_continuous(guide = guide_colorbar(nbreak = 8)) # change num of breaks
p() + scale_fill_continuous(guide = guide_colorbar(nbin = 3)) # change resolution of bar
p() + scale_fill_continuous(guide = guide_colorbar(nbin = 100)) # change resolution of bar
p() + scale_fill_continuous(limits=c(0,20), breaks=c(0,5,10,15,20),
guide = guide_colorbar(nbin=100, nodraw.ul = TRUE, nodraw.ll = TRUE)) # hide top- and bottom-most ticks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment