width, height of a device

25 views
Skip to first unread message

Kasper Daniel Hansen

unread,
Feb 4, 2009, 12:42:25 AM2/4/09
to ggp...@googlegroups.com
This email is both asking for a bit of help and also acting as a
wishlist for a function.

So far I have only used ggplot2 for producing facetted plots. I am
plotting various collections of datasets in various plots. This means
that my plots sometimes have (say) 2x3 facets, sometimes 4x3 and so.
In my case, the content of each facet is the same, so I have identical
x- and y-axis. But since ggplot2 does the standard R thing of
expanding to fill the device, the aspect ratio of the facets on
different plots are different, something that works havoc on any plot-
to-plot comparison.

In standard R I would usually fix this by changing the device size, say
par(mfrow = c(1,1))
quartz(width = 7, height = 7)
par(mfrow = c(1,2))
quartz(width = 14, height = 7)

But in ggplot2 I have two areas, essentially the legend area and also
the axis area to the left side of the plot. This means that if I have
2 columns of facets, I cannot just do facet_width = device_width / 2

What I would really like is - for a given plot - to tell me how big a
single facet is in width and height in % of the device area. This
should be possible based on my knowledge of grid. At least in the
little grid plotting I have done, I have usually specified sizes in %
of device area, so I am just asking for the right way to interrogate
the ggplot2 structure to get the settings of the facet.

The wishlist function is a function that given a height and a width of
a facet, computes how big the device needs to be in order to get the
given facet height, width. That seems to be very useful for facetted
plotting.

Thanks
Kasper

hadley wickham

unread,
Feb 4, 2009, 9:39:47 AM2/4/09
to Kasper Daniel Hansen, ggp...@googlegroups.com
Hi Kasper,

You can almost get the width of a panel:

library(ggplot2)
qplot(mpg, wt, data = mtcars, facets = vs ~ am)

grid.ls()
# Pick a panel grob
# (These aren't named very useful in this version but I'll work on that)

panel <- grid.get("panel.gTree.654")
downViewport("background")
downViewport("panels")
convertWidth(grobWidth(panel), "cm")

But unfortunately it gives a width of 0 cm. I don't understand why,
so I'll talk to Paul Murrell about it.

What I'm planning for a future release is the complement of your wish
- the ability to set the width of individual components (e.g. panels
or legends). That should make it easier to align multiple plots with
varying numbers of panels and legends. It's not currently possible
because of the way that the viewports are constructed - I first make a
viewport layout for the panels & axes, and then embed that in another
viewport for the title, legend and axis labels.

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

Kasper Daniel Hansen

unread,
Feb 4, 2009, 4:38:59 PM2/4/09
to hadley wickham, ggp...@googlegroups.com

On Feb 4, 2009, at 6:39 , hadley wickham wrote:

> Hi Kasper,
>
> You can almost get the width of a panel:
>
> library(ggplot2)
> qplot(mpg, wt, data = mtcars, facets = vs ~ am)
>
> grid.ls()
> # Pick a panel grob
> # (These aren't named very useful in this version but I'll work on
> that)
>
> panel <- grid.get("panel.gTree.654")
> downViewport("background")
> downViewport("panels")
> convertWidth(grobWidth(panel), "cm")

I get that grobWidth(panel) is equal to 1grobwidth.

My guess is that at this point, the width refers to "the entire grob",
so I guess that you need to get the width from somewhere else.

But my grid is a bit rusty, so I am probably wrong.

Kasper

hadley wickham

unread,
Feb 5, 2009, 10:14:45 AM2/5/09
to Kasper Daniel Hansen, ggp...@googlegroups.com
>> panel <- grid.get("panel.gTree.654")
>> downViewport("background")
>> downViewport("panels")
>> convertWidth(grobWidth(panel), "cm")
>
> I get that grobWidth(panel) is equal to 1grobwidth.
>
> My guess is that at this point, the width refers to "the entire grob",
> so I guess that you need to get the width from somewhere else.

The width of that grob is defined in terms of another grob, but you
should be able to force grid to tell you the physical measurement with
convertWidth. I've emailed Paul to see why it's not working in this
instance.

Hadley

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

Reply all
Reply to author
Forward
0 new messages