Seeking advice on strategy for plotting muliple panels in columns

51 views
Skip to first unread message

Barry M. Lesht

unread,
Nov 15, 2015, 9:18:10 PM11/15/15
to ggplot2
I'm relatively new to R and to ggplot and am looking for some advice.  I want to create a specific plot, but am unsure about the best way to go about doing it.  I am not looking for specific coding, just for suggestions on how to approach this problem.  Is this something I can even do using ggplot (possibly with other packages)? 

The basic data I want to plot are size distributions of several different species sampled during different years in a number of distinct ecological provinces.  For any province/year I use a stacked bar chart to show the size distributions.  This part of the plotting is straightforward.  What I want to do is stack the yearly bar plots for each province in a column and have a number of the columns side by side so we can visualize both the temporal evolution of the distributions in a province and the spatial differences between provinces for each year.  I want the plots aligned horizontally in each column (i.e. each bar plot has the same x-axis limits) and I want the vertical scale (in this case (kg of biomass)/(unit of axis length)) of every bar plot to be the same, though the maximum y-value might be different from plot to plot (so the individual panels may have different physical lengths depending on the maximum value of the y variable).  Thus, the years may not align horizontally across the columns.

I don't think this is something I can do with faceting, but I'm not really sure.  Similarly, I haven't found anything in the ggplot documentation that suggests either a way to explicitly position a subplot on a "page" or a way to control the physical length of an axis.  If there are functions that would allow this, I'd appreciate hearing about them.

Thanks.

romunov

unread,
Nov 16, 2015, 12:44:38 AM11/16/15
to Barry M. Lesht, ggplot2

Do you find anything useful in the geom_bar part of the documentaton? If nothing escaped me, sounds to me like you want a stacked bar plot.

http://docs.ggplot2.org/current/geom_bar.html

Cheers,
Roman

--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Barry M. Lesht

unread,
Nov 16, 2015, 9:27:14 AM11/16/15
to ggplot2, ble...@gmail.com
Sorry I was not more clear.  Each individual panel is a stacked bar plot.  I can generate each individual panel easily in ggplot.  What I am looking for are ways to (1) control the length of the y-axis in each panel so I can maintain the y-axis (graphed unit)/(physical length) in all panels, and (2) arrange the panels on a single page.  I suppose if I can get the answer to (1) I can create the panels individually save them as files and arrange them in another application.  Thanks for the reply.

Brian

unread,
Nov 16, 2015, 10:25:47 AM11/16/15
to Barry M. Lesht, ggplot2
Hi Barry,

It appears that facet_grid or facet_wrap is what you are looking for.
Could you please provide us a reproducible starting point, so that we
can help you further?

Cheers,
Brian
> <http://docs.ggplot2.org/current/geom_bar.html>
>
> Cheers,
> Roman
>
>
> --
> --
> You received this message because you are subscribed to the ggplot2
> mailing list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google
> Groups "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to ggplot2+u...@googlegroups.com
> <mailto:ggplot2+u...@googlegroups.com>.

Crump, Ron

unread,
Nov 16, 2015, 10:27:06 AM11/16/15
to Barry M. Lesht, ggplot2
Hi Barry,

>Sorry I was not more clear. Each individual panel is a stacked bar plot.
> I can generate each individual panel easily in ggplot. What I am
>looking for are ways to (1) control the length of the y-axis in each
>panel so I can maintain the y-axis
> (graphed unit)/(physical length) in all panels, and (2) arrange the
>panels on a single page. I suppose if I can get the answer to (1) I can
>create the panels individually save them as files and arrange them in
>another application. Thanks for the reply.

What is the code you have used so far? Can we get the data too, or some
manageable facsimile of it? Have you tried adding facet_grid( province ~
year ), or facet_grid( year ~ province ) to your code? Do either give you
what you want? If not, how does it differ from what you want?

Ron.

Barry M. Lesht

unread,
Nov 16, 2015, 11:46:18 AM11/16/15
to ggplot2, ble...@gmail.com, R.E....@warwick.ac.uk
Thank you Brian and Ron.  I did look at faceting, but I didn't think the control provided by the "scales" and "space" options would help do what I want.  Andy suggested that I also look at using grid which I will do as well.

As for a reproducible example, I understand that is the ideal for queries and will try to put together a facsimile.  Ron - so far I have used ggplot to produce the stacked bar charts for each individual year/province combination.  Although I'm generally happy with how these plots look, I have not solved the problem of fixing the y-axis scale.  Most of my experience in graphics is in using IDL which allows explicit control over such things and I was hoping that something similar was "hidden" in ggplot.  To try and explain that part again, when I do the individual bar plots, they all have the same physical size (think X pixels wide by Y pixels high), but the maximum y value differs, depending on the data for that particular year/province.  In one case, the Y pixels may represent 20 kg, in another case the Y pixels might represent 2 kg, so the kg/pixel ratio is different.  What I want is to use a constant kg/pixel ratio across all the bar plots.

Barry

Aksel Olsen

unread,
Nov 17, 2015, 1:42:38 AM11/17/15
to ggplot2
For the facet_wrap or facet_grid mappings, you can include a scales='free' parameter, allowing facets to show relevant parts of the y axis only.
Aksel

Barry M. Lesht

unread,
Feb 22, 2016, 6:28:14 PM2/22/16
to ggplot2
Thanks to all who offered suggestions.  I finally found a solution to this problem, but I ended up doing it using base graphics instead of ggplot2.  The whole process is rather involved, but the basic idea is to use the base split.screen() function to define the sizes of the panels and then plot the graphics in their respective windows.  I had to make two passes through the data, first to get the information needed to properly size the windows, and then again to plot the stacked bar plots.  Unfortunately, split.screen() is incompatible with ggplot2 so I could not take advantage of the convenient ggplot2 features.
Reply all
Reply to author
Forward
0 new messages