-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Write vignette about grid #1239
Comments
FWIW the latest version of grid.arrange has gotten quite flexible, it might be worth mentioning for users who'd rather not learn about viewports but just want the equivalent of par(mfrow) (and something compatible with ggsave). There's also |
Since you probably know more about this than me, would you be interested in drafting the vignette? |
OK, I'll give it a go, but quite busy at the moment with teaching etc. |
started, output rendered on this wiki (but losing fig captions). |
Looks good so far! One thing to bear in mind is that since the vignette is included in the package, it needs to be kept fairly small and not use too many graphics. If you want to use lots of pictures, we'll probably have to publish a different way. |
On 9 August 2015 at 15:39, Hadley Wickham notifications@github.com wrote:
it's likely to be (become) too long for a brief reference document, I grid.arrange(p1, p2, nrow=1) is much more concise than a discussion of grid viewports, and will probably |
Does grid.arrange working right now? I wrote a code in the beginning of this year (2016) for aligning two plots side by side (with unit.pmax adjustment for heights). it was working fine. But the exactly same code does not working right now. Is there any update that we should've know? Thanks a lot! |
@baptiste any interest in finishing this off? Otherwise I'll close this issue as it's unlikely that I'll ever have time to do it. |
here's the latest version for reference, but it's unlikely I find motivation to come back to it https://github.com/baptiste/gridextra/wiki/arranging-ggplot let me know if you want the Rmd source |
ccing @wilkelab and @thomasp85 in case they have any interest in leading on this. |
I think gtable should probably have some vignette of some sort (maybe with a short section on its tie to grid layouts) I'm over my head in projects right now though so it's not something that will come from me anytime soon... |
here's what I started writing re gtable a few years ago: |
closing this... I think there are better venues for this than in a ggplot2 vignette |
Hello and thank you for sharing this vignette. If I wanted to add a common y-axis title for multiple graphs displayed in a single column after employing the following: gA <- ggplotGrob(Temp250m) grid::grid.newpage() Can I ask how to go about doing that? My apologies if this is not the place to ask questions. Thanks for any guidance |
@EchoDelta7272 for usage questions it is generally easier to ask them on the Posit community or stackoverflow, both because it keeps issues focussed and because there is a lot more people available to consider your question. |
Understood and thank you for the guidance @teunbrand! |
Text removed from book below
If you want to arrange multiple plots on a single page, you'll need to learn a little bit of grid, the underlying graphics system used by ggplot. The key concept you'll need to learn about is a viewport: a rectangular subregion of the display. The default viewport takes up the entire plotting region, and by customising the viewport you can arrange a set of plots in just about any way you can imagine. \index{Layout} \index{Publication!multiple plots on the same page}
To begin, let's create three plots that we can experiment with. When arranging multiple plots on a page, it will usually be easiest to create them, assign them to variables and then plot them. This makes it easier to experiment with plot placement independent of content. The plots created by the code below are shown in Figure \ref{fig:layout}.
r columns(3)
Subplots
One common layout is to have a small subplot drawn on top of the main plot. To achieve this effect, we first plot the main plot, and then draw the subplot in a smaller viewport. Viewports are created with (surprise!) the
viewport()
function, with parametersx
,y
,width
andheight
to control the size and position of the viewport. By default, the measurements are given in 'npc' units, which range from 0 to 1. The location (0, 0) is the bottom left, (1, 1) the top right and (0.5, 0.5) the centre of viewport. If these relative units don't work for your needs, you can also use absolute units, likeunit(2, "cm")
orunit(1, "inch")
. \index{Sub-figures} \index{Subplots}By default, the x and y parameters control the location of the centre of the viewport. When positioning the plot in other locations, you may need to use the
just
parameter to control which corner of the plot you are positioning. The following code gives some examples.To draw the plot in our new viewport, we use the
vp
argument of theprint()
method. This method is normally called automatically whenever you evaluate something on the command line, but because we want to customise the viewport, we need to call it ourselves. The result of this is shown in Figure \ref{fig:subplot-1}.This gives us what we want, but we need to make a few tweaks to the appearance: the text should be smaller, we want to remove the axis labels and shrink the plot margins. The result is shown in Figure \ref{fig:subplot-2}.
\begin{figure}[htbp]
\centering
\subfigure[Figure with subplot.]{
\includegraphics[width=0.5\textwidth]{figures/polishing-subplot-1}
\label{fig:subplot-1}
}%
\subfigure[Subplot tweaked for better display.]{
\includegraphics[width=0.5\textwidth]{figures/polishing-subplot-2}
\label{fig:subplot-2}
}
\caption{Two examples of a figure with subplot. It will usually be necessary to tweak the theme settings of the subplot for optimum display.}
\label{fig:subplot}
\end{figure}
Note we need to use
pdf()
(orpng()
etc.) to save the plots to disk becauseggsave()
only saves a single plot.Rectangular grids
A more complicated scenario is when you want to arrange a number of plots in a rectangular grid. Of course you could create a series of viewports and use what you've learned above, but doing all the calculations by hand is cumbersome. A better approach is to use
grid.layout()
, which sets up a regular grid of viewports with arbitrary heights and widths. You still need to create each viewport, but instead of explicitly specifying the position and size, you can specify the row and column of the layout.The following example shows how this work. We first create the layout, here a 2 by 2 grid, then assign it to a viewport and push that viewport on to the plotting device. Now we are ready to draw each plot into its own position on the grid. We create a small function to save some typing, and then draw each plot in the desired place on the grid. You can supply a vector of rows or columns to span a plot over multiple cells. The results are shown in Figure \ref{fig:layout-2}.
r columns(1)
By default
grid.layout()
makes each cell the same size, but you can use thewidths
andheights
arguments to make them different sizes. See the documentation forgrid.layout()
for more examples.The text was updated successfully, but these errors were encountered: