Skip to content

Instantly share code, notes, and snippets.

@kohske
Created April 26, 2011 01:54
Show Gist options
  • Save kohske/941662 to your computer and use it in GitHub Desktop.
Save kohske/941662 to your computer and use it in GitHub Desktop.
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
p + scale_fill_continuous(guide="colorbar") + opts(legend.key.width=unit(0.5, "line"), legend.key.height=unit(2, "line"))
# specify the number of breaks of legend
p + scale_fill_continuous(guide=guide_colorbar(nbreak=10))
# manually specify the breaks
p + scale_fill_continuous(guide="colorbar", breaks=c(1,4,9,16))
# manually specify the breaks and labels
p + scale_fill_continuous(guide="colorbar", breaks=c(1,4,9,16), labels=c("one^2", "two^2", "three^2", "four^2"))
# change the resolution of colorbar (default = 20)
p + scale_fill_continuous(guide=guide_colorbar(nbin=3))
p + scale_fill_continuous(guide=guide_colorbar(nbin=100))
# combine with other scales with default scales
p + scale_fill_continuous(guide="colorbar") + geom_point(aes(size=value))
# combine and merge colour and fill
p + geom_point(aes(y=X2+4, colour=value), size=10) +
scale_fill_continuous(guide="colorbar") +
scale_colour_continuous(guide="colorbar")
# combine but not merge colour and fill
ggplot(melt(outer(1:4, 1:4)), aes(x=X1, y=X2)) + geom_tile(aes(fill=value, colour=value), size=3) +
scale_fill_continuous(name="fill", guide="colorbar") +
scale_colour_gradient(name="colour", low="pink", high="green", guide="colorbar")
# use custom gradient (here jet colormap)
jet.colors <- colorRampPalette(c("#00007F", "blue", "#007FFF", "cyan", "#7FFF7F", "yellow", "#FF7F00", "red", "#7F0000"))
ggplot(melt(volcano), aes(x=X1, y=X2, fill=value)) + geom_tile() +
scale_fill_gradientn(colours=jet.colors(7), guide="colorbar") + opts(legend.colorbar.nbin=100)
# plot math label by expression
p + scale_fill_continuous(
expression(paste(alpha, beta, gamma)),
guide="colorbar",
breaks=c(1,16), labels=c(expression(1^2), expression(4^2)))
# if the limits are pretty, then it looks like no expantion outside the limits
p + scale_fill_continuous(guide=guide_colorbar(nbin=100), limits=c(0,20))
# or set the breaks by using data range
p + scale_fill_continuous(guide=guide_colorbar(nbin=100),
breaks=seq(range(p$data$value)[1], range(p$data$value)[2], length=5))
p + scale_fill_continuous(guide=guide_colorbar(nodraw.ul = TRUE, nodraw.ll = TRUE),
limits=range(p$data$value),
breaks=seq(range(p$data$value)[1], range(p$data$value)[2], length=5))
# using syntax suger
ggplot(melt(outer(1:4, 1:4)), aes(x=X1, y=X2)) + geom_point(aes(fill=value, colour=value), size=10) +
guides(fill=guide_colorbar(name="fill"), colour=guide_legend(name="XX"))
dev.off()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment