I\'m hoping I can combine the spiffy importing and drawing powers of grImport
with the awesome graphing power of ggplot2
, but I simply don\'t under
Below is a hack to use a custom grob for axis labels.
library(grid)
library(ggplot2)
## convert the labels to some parameter to be used in the custom grob
## here simply an index that will be interpreted as color
mapping <- function(x, ...){
seq_along(x)
}
library(grImport)
hourglass <- new("Picture",
paths= list(new("PictureFill",
x=c(0, 1, 0, 1),
y=c(0, 0, 1, 1))),
summary= new("PictureSummary",
numPaths=1,
xscale=c(0, 1),
yscale=c(0, 1)))
## bare bones edit of theme_text()
my_axis <- function ()
{
structure(function(label, x = 0.5, y = 0.5, default.units = "npc", ...) {
cols <- mapping(label)
symbolsGrob(hourglass, x, 0*x + unit(0.5, "npc"),
use.gc=FALSE,size=unit(5,"mm"), gp=gpar(fill=cols))
}, class = "theme", type = "custom", call = match.call())
}
qplot(1:12, rnorm(12)) +
opts( axis.text.x = my_axis(), axis.ticks.margin = unit(0.5, "cm"))
For a single panel, it is fairly straight-forward to extract information from the x axis grob, and replace it with your own. I'm not sure how this could be extended cleanly to automatic, multi-panel axes.
library(grid)
library(ggplot2)
p <- qplot(1:12, rnorm(12))
grid.newpage()
g <- ggplotGrob(p)
grid.draw(g)
g0 <- getGrob(g, gPath("axis.text.x"), grep=TRUE)
grid.set(gPath("axis.text.x"),
pointsGrob(x = g0$x, y=0*g0$x + unit(0.5,"npc"),
pch=19, gp=gpar(col=seq_along(g0$x)),
name = g0$name), grep = TRUE)
here is an example:
# convert ps to RGML
PostScriptTrace(file.path(system.file(package = "grImport"), "doc", "GNU.ps"), "GNU.xml")
PostScriptTrace(file.path(system.file(package = "grImport"), "doc", "tiger.ps"), "tiger.xml")
# read xml
pics <- list(a = readPicture("GNU.xml"), b = readPicture("tiger.xml"))
# custom function for x axis label.
my_axis <- function () {
structure(
function(label, x = 0.5, y = 0.5, ...) {
absoluteGrob(
do.call("gList", mapply(symbolsGrob, pics[label], x, y, SIMPLIFY = FALSE)),
height = unit(1.5, "cm"))
}
)}
qplot(factor(c("a", "b")), 1:2) + opts( axis.text.x = my_axis())