grid.edit()

31 views
Skip to first unread message

baptiste auguie

unread,
May 31, 2009, 9:03:44 AM5/31/09
to ggplot2
[copy sent to r-help]

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

baptiste auguie

unread,
Jun 2, 2009, 5:35:14 AM6/2/09
to ggplot2
OK, Paul Murrell's illuminated the path for me. See
http://wiki.r-project.org/rwiki/doku.php?id=tips:graphics-ggplot2:edit-grob
for a minimal tutorial. This may provide a temporary workaround to
annotate a ggplot with maths.

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
______________________________

hadley wickham

unread,
Jun 4, 2009, 1:16:36 PM6/4/09
to baptiste auguie, ggplot2
On Tue, Jun 2, 2009 at 4:35 AM, baptiste auguie <bapt...@googlemail.com> wrote:
>
> OK, Paul Murrell's illuminated the path for me. See
> http://wiki.r-project.org/rwiki/doku.php?id=tips:graphics-ggplot2:edit-grob
> for a minimal tutorial. This may provide a temporary workaround to
> annotate a ggplot with maths.
>
> 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!?

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

--
http://had.co.nz/

Reply all
Reply to author
Forward
0 new messages