grid.clip and ggplot2

123 views
Skip to first unread message

baptiste auguie

unread,
Sep 3, 2009, 8:24:44 AM9/3/09
to ggplot2
Dear all,

As daft as it may sound, I'm trying to clip a ggplot2 to a particular
viewport without success. For example,

p = qplot(1:10)
g = ggplotGrob(p)

grid.clip(x = 0.3, width = 0.1)
grid.circle(gp=gpar(fill="red"))

grid.newpage()

grid.clip(x = 0.3, width = 0.1)
grid.draw(g)

Is it currently impossible?

I thought it'd be handy to illustrate specific theme settings.

Best,

baptiste

Will

unread,
Sep 3, 2009, 3:21:00 PM9/3/09
to ggplot2
On Sep 3, 8:24 am, baptiste auguie <bapt4...@googlemail.com> wrote:
> Dear all,
>
> I'm trying to clip a ggplot2 to a particular
> viewport without success.
>
> baptiste

Admittedly, I am just getting started with the ggplot2 package, but I
glanced over setup_viewports and assign_viewports that appear to have
clipping attributes. Perhaps these could be of some help? Apologies
for an indefinite answer.

Best -
Will

baptiste auguie

unread,
Sep 3, 2009, 4:02:26 PM9/3/09
to Will, ggplot2
Hi,

I should indeed look at the source code! However I wanted to know
whether it was intentional or not that a high-level ggplot2 grob
should not respect such a clipping region.


Thanks,

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,
Sep 4, 2009, 12:41:36 PM9/4/09
to baptiste auguie, Will, ggplot2
> I should indeed look at the source code! However I wanted to know
> whether it was intentional or not that a high-level ggplot2 grob
> should not respect such a clipping region.

From the documentation of clipGrob:

Pushing or popping a viewport _always_ overrides the clip region
set by a clip grob, regardless of whether that viewport explicitly
enforces a clipping region.

And ggplot2 uses lots of viewports, so I don't think clipGrob is the
way to enforce clipping. Instead, I think you want to create a new
viewport and draw the ggplot2 grob inside of that.

Hadley


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

baptiste auguie

unread,
Sep 4, 2009, 12:51:01 PM9/4/09
to hadley wickham, ggplot2
On Fri, Sep 4, 2009 at 6:41 PM, hadley wickham<h.wi...@gmail.com> wrote:
>> I should indeed look at the source code! However I wanted to know
>> whether it was intentional or not that a high-level ggplot2 grob
>> should not respect such a clipping region.
>
> From the documentation of clipGrob:
>
>     Pushing or popping a viewport _always_ overrides the clip region
>     set by a clip grob, regardless of whether that viewport explicitly
>     enforces a clipping region.
>

Good point! That explain why lattice behaves similarly.

> And ggplot2 uses lots of viewports, so I don't think clipGrob is the
> way to enforce clipping.  Instead, I think you want to create a new
> viewport and draw the ggplot2 grob inside of that.
>

But in that case the ggplot2 grob will scale rather than be clipped,

p = qplot(1:10)
g = ggplotGrob(p)

pushViewport(viewport(width=0.2))
grid.draw(g)

Am I missing something?

Thanks,

baptiste

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

hadley wickham

unread,
Sep 4, 2009, 12:54:10 PM9/4/09
to baptiste auguie, ggplot2
> But in that case the ggplot2 grob will scale rather than be clipped,
>
> p = qplot(1:10)
> g = ggplotGrob(p)
> pushViewport(viewport(width=0.2))
> grid.draw(g)

Oh true. Hmmm, maybe ask Paul?

Hadley

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

Reply all
Reply to author
Forward
0 new messages