Created
November 26, 2013 13:48
-
-
Save baptiste/7658502 to your computer and use it in GitHub Desktop.
two-coloured title
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
require(ggplot2) | |
require(grid) | |
element_grob.element_custom <- function(element, label="", colour="black") { | |
disect <- strsplit(label, "\\n")[[1]] | |
N <- length(disect) | |
fun <- function(ii, colour) | |
textGrob(disect[ii], y=unit(N, "lines") - unit(ii-1, "lines"), gp=gpar(col=colour)) | |
grobs <- mapply(fun, ii=seq_along(disect), colour=element$colour, SIMPLIFY=FALSE) | |
gTree(children=do.call(gList, grobs), height=unit(N, "lines"), cl = "custom_title") | |
} | |
# gTrees don't know their size and ggplot would squash it, so give it room | |
grobHeight.custom_title = heightDetails.custom_title = function(x, ...) | |
x$height | |
# silly wrapper to fool ggplot2's slightly paranoid checks... | |
custom_title <- function(colour, ...){ | |
structure( | |
list(colour=colour, ...), # this ... information is not used, btw | |
class = c("element_custom","element_blank", "element") # inheritance test workaround | |
) | |
} | |
qplot(1,1) + ggtitle("first line\nsecond line") + theme(plot.title=custom_title(colour=c("red", "blue"))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment