Comments (11)
Worked it out. For future googlers, plot_grid
doesn't add space; it simply uses the spacing already in the plots, which you can remove using theme(plot.margin = unit(c(0, 0, 0, 0), "cm"))
.
library(ggplot2)
library(cowplot)
p1 <- qplot(1:10, 1:10) + theme(plot.margin = unit(c(0, 0, 0, 0), "cm"))
p2 <- qplot(1:10, (1:10)^2) + theme(plot.margin = unit(c(0, 0, 0, 0), "cm"))
plot_grid(p1, p2, p1, p2, p1, p2, p1, p2, p1, p2, p1, p2)
from cowplot.
For those kinds of problems I would now recommend the patchwork library. It's inherently difficult with plot_grid()
, due to its underlying design.
from cowplot.
Claus,
It seems like the approach above doesn't work when the axis labels and titles take up different amounts of space in different panels or when you want to get rid of the axis titles and labels in some panels but not others.
A reproducible example is below. It is based on the idea that sometimes you want a faceted plot, but you want to customize each facet in ways that are not possible with "regular" faceting. So, instead, you split the data frame by the desired faceting column, create a separate plot for each "facet", and then lay the plots out together with plot_grid
as if they had been faceted (see, for example, this SO answer).
It would be nice if there were a way to directly control the space between panels in plot_grid
, even in the case I discuss below. If this is already possible, and my examples are based on a misunderstanding, please let me know.
Thanks,
Joel
Example 1: We create separate plots. Then we remove the y-axis titles, labels, and ticks from all but the plot that will be left-most when we lay them out. We set the left margin of all but the left-most plot to -1 cm. But note in the image that even though we got rid of the left margin on five of the six panels, plot_grid
seems to leave space between all the plots as if it were allowing for the width of y-axis title, labels, and ticks in the left-most plot.
library(tidyverse)
library(cowplot)
# Create list of plots
plist = split(mtcars, mtcars$carb) %>%
map(~ .x %>% group_by(carb) %>%
ggplot(aes(x = mpg, y = wt)) +
geom_point())
# Remove y-axis labels, title, and ticks from all but the left-most plot
plist[2:length(plist)] = plist[2:length(plist)] %>%
map(~ .x + theme(axis.text.y=element_blank(),
axis.title.y=element_blank(),
axis.ticks.y=element_blank(),
plot.margin=margin(l=-1, unit="cm")))
plot_grid(plotlist=plist, ncol=length(plist), align="hv")
Example 2: Same idea, but now we set the left margin of all the plots to be -1 cm. Now we get the thin margins that we want between plots, but at the expense of cutting off the title and most of the labels at the left edge of the plot.
# Create a list of plots
plist = split(mtcars, mtcars$carb) %>%
map(~ .x %>% group_by(carb) %>%
ggplot(aes(x = mpg, y = wt)) +
geom_point() +
theme(plot.margin=margin(l=-0.8,unit="cm")))
# Remove y-axis labels, title, and ticks from all but the left-most plot
plist[2:length(plist)] = plist[2:length(plist)] %>%
map(~ .x + theme(axis.text.y=element_blank(),
axis.title.y=element_blank(),
axis.ticks.y=element_blank()))
plot_grid(plotlist=plist, ncol=length(plist), align="v")
from cowplot.
@kendonB Here is a solution (by Valentin):
https://stackoverflow.com/questions/41834093/decrease-margins-between-plots-when-using-cowplot
plot5 <- plot_grid(plot1, NULL, plot2, rel_widths = c(1, -0.1, 1), align = "hv",
labels = c("A", "B"), nrow = 1)
plot5
from cowplot.
I will need a reproducible code example to understand what the issue is.
from cowplot.
I'm closing this because there isn't sufficient information here to understand what the issue is.
from cowplot.
Apologies for missing explaining this earlier. Suppose I have a grid:
library(ggplot2)
library(cowplot)
p1 <- qplot(1:10, 1:10)
p2 <- qplot(1:10, (1:10)^2)
# simple grid
plot_grid(p1, p2)
I would like to squish them closer together. Is this possible in cowplot? The white space becomes quite large compared to the plots with denser grids.
from cowplot.
Yes, that's how it works. Thanks for posting a clear example solution, this question comes up quite frequently.
from cowplot.
The trick is to extract the x axis label from a plot and add it as a separate column in a plot that otherwise looks like your Example 2. Something along the lines of the last example here: https://cran.r-project.org/web/packages/cowplot/vignettes/shared_legends.html
The code you'd need to write to extract the axis labels would be similar to the get_legend
function, you just need to extract a different grob, probably the one called ylab-l
:
https://github.com/wilkelab/cowplot/blob/master/R/get_legend.R
from cowplot.
It seems to me that when we use align="hv" and the last element of the first line is NULL the approach to making the margin of each plot smaller does not work. I'm trying to approximate density plots to the top and left margin of a line with scater plots (as eipi10 commented) and let the axes of the plots allingned. But when I use the (plot.margin) command I really do not have succees in making the plots closer togheter.
from cowplot.
do you know why as I add rows with plot_grid
the size of my plots shrinks to the point where they become unreadable? (let's say I want to print 6 plots ncol=6, nrow=1
and it works fine. Then I add 24 other plots to get to ncol=6, nrow=5
and it does not work anymore). I posted it here
I have also used the patchwork library but with the same results
from cowplot.
Related Issues (20)
- feature request: geom_image that allows plotting of several images, based on geom_rect? HOT 12
- align by axis values HOT 1
- Conversion failure warnings on unicode character HOT 3
- Label positioning is inconsistent HOT 1
- Rstudio crashed on iOS saving plots into tiff format HOT 6
- [Feature request] Icicle plot with ggplots objects
- Using expressions in plot text generates warnings
- Feature request: Add "units" parameter to all functions where size can be specified.
- Odd problem with setting alpha in an annotate geom
- [Feature request] Align legend with the start and end of the axis
- Cannot convert object of class LayerInstanceLayerggprotogg into a grob?
- changing size of the second plot in plot_grid() HOT 2
- ggarange common.legend positioning overwritten
- No side effect from plot_grid when used inside a function returning an object
- get_legend doesn't match original position
- Blank plot using plot_grid with RNotebook
- Feature request: options for anchors of labels in plot_grid HOT 1
- warning message: The `size` argument of `element_line()` is deprecated as of ggplot2 3.4.0. HOT 5
- Explanation of how align_plot, ggdraw, and draw_plot work together for dual-y-axis plots.
- `draw_image` only works when background themes are set to `ggplot2::element_blank()` HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cowplot.