Dear all,
I'm trying to access and modify grobs in a ggplot2 plot. The basic
idea for raw Grid objects I understand from Paul Murrell's R graphics
book, or this page of examples,
http://www.stat.auckland.ac.nz/~paul/grid/copygrob/copygrobs.R
However I can't figure out how to apply this to a ggplot (basically I
don't know how to write a syntactically correct gPath),
p <- # minimal example
qplot(0,0)+ annotate("text",0,0,label="test")
g <- # store the plot as a grob
ggplotGrob(p)
# structure of the grob
grid.ls(g) # rather large!
# find a particular grob in the gTree
getGrob(g,"texts", grep = T)
# next step, modify, say, the colour of these grobs
grid.edit() # what do I put in here?
Thanks for any piece of advice,
baptiste
On the other hand, I now realise that my initial goal is compromised:
I wanted to use grid.edit() with a regular expression to change the
colour of all relevant grobs by applying the dichromat() function,
thereby checking the appearance for color-blindness. The problem is,
there is no regular expression that will give me all the coloured
grobs, is there!?
As an alternative, perhaps theme() and colour_scale() could gain a new
argument, dichromat = FALSE, or is there a specific step in the
construction of a ggplot where this colour conversion could be easily
made on the whole plot?
Ideally, one would wish,
library(ggplot2)
dsamp <- diamonds[sample(nrow(diamonds), 1000), ]
(p <- qplot(carat, price, data=dsamp, colour=clarity))
p + test_dichromat()
to get a result similar to,
# install.packages("ggextra", repos="http://R-Forge.R-project.org")
library(ggextra)
p + scale_colour_dichromat()
but with the colours being flattened from the original scale as
opposed to being set to arbitrary dichromat-friendly values.
Does this make sense?
Best,
baptiste
--
_____________________________
Baptiste Auguié
School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK
http://newton.ex.ac.uk/research/emag
______________________________
No, you'd have to loop through all the grobs, and then see if they are
coloured or not.
> As an alternative, perhaps theme() and colour_scale() could gain a new
> argument, dichromat = FALSE, or is there a specific step in the
> construction of a ggplot where this colour conversion could be easily
> made on the whole plot?
That's going to be very tricky. I think the above approach, where you
simply retrieve all grobs and then loop through them one by one is
most likely to work.
Hadley