Skip to content

Specify different horizontal and vertical margins for facets #840

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

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
* theme: new theme settings panel.margin.x and panel.margin.y (units) allow
specifying horizontal and vertical gap between panels in facetted plots (for
both grid and wrap). (Fixes #839)

ggplot2 0.9.3.1
----------------------------------------------------------------

Expand Down
12 changes: 6 additions & 6 deletions R/facet-grid-.r
Original file line number Diff line number Diff line change
Expand Up @@ -299,9 +299,9 @@ build_strip <- function(panel, label_df, labeller, theme, side = "right") {
strips <- gtable_matrix(name, grobs, heights = heights, widths = widths)

if (horizontal) {
gtable_add_col_space(strips, theme$panel.margin)
gtable_add_col_space(strips, theme$panel.margin.x %||% theme$panel.margin)
} else {
gtable_add_row_space(strips, theme$panel.margin)
gtable_add_row_space(strips, theme$panel.margin.y %||% theme$panel.margin)
}
}

Expand All @@ -314,14 +314,14 @@ facet_axes.grid <- function(facet, panel, coord, theme) {
grobs <- lapply(panel$ranges[cols], coord_render_axis_h,
coord = coord, theme = theme)
axes$b <- gtable_add_col_space(gtable_row("axis-b", grobs),
theme$panel.margin)
theme$panel.margin.x %||% theme$panel.margin)

# Vertical axes
rows <- which(panel$layout$COL == 1)
grobs <- lapply(panel$ranges[rows], coord_render_axis_v,
coord = coord, theme = theme)
axes$l <- gtable_add_row_space(gtable_col("axis-l", grobs),
theme$panel.margin)
theme$panel.margin.y %||% theme$panel.margin)

axes
}
Expand Down Expand Up @@ -382,8 +382,8 @@ facet_panels.grid <- function(facet, panel, coord, theme, geom_grobs) {

panels <- gtable_matrix("panel", panel_matrix,
panel_widths, panel_heights, respect = respect)
panels <- gtable_add_col_space(panels, theme$panel.margin)
panels <- gtable_add_row_space(panels, theme$panel.margin)
panels <- gtable_add_col_space(panels, theme$panel.margin.x %||% theme$panel.margin)
panels <- gtable_add_row_space(panels, theme$panel.margin.y %||% theme$panel.margin)

panels
}
Expand Down
4 changes: 2 additions & 2 deletions R/facet-wrap.r
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,13 @@ facet_render.wrap <- function(facet, panel, coord, theme, geom_grobs) {
# If not listed, assume is unit(1, "null")
widths <- list(
axis_l = width_cm(grobs$axis_l),
vspace = ifelse(layout$COL == ncol, 0, height_cm(theme$panel.margin))
vspace = ifelse(layout$COL == ncol, 0, width_cm(theme$panel.margin.x %||% theme$panel.margin))
)
heights <- list(
panel = unit(aspect_ratio, "null"),
strip_t = height_cm(grobs$strip_t),
axis_b = height_cm(grobs$axis_b),
hspace = ifelse(layout$ROW == nrow, 0, height_cm(theme$panel.margin))
hspace = ifelse(layout$ROW == nrow, 0, height_cm(theme$panel.margin.y %||% theme$panel.margin))
)

col_widths <- compute_grob_widths(info, widths)
Expand Down
2 changes: 2 additions & 0 deletions R/theme-defaults.r
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ theme_grey <- function(base_size = 12, base_family = "") {
panel.grid.major = element_line(colour = "white"),
panel.grid.minor = element_line(colour = "grey95", size = 0.25),
panel.margin = unit(0.25, "lines"),
panel.margin.x = NULL,
panel.margin.y = NULL,

strip.background = element_rect(fill = "grey80", colour = NA),
strip.text.x = element_text(),
Expand Down
2 changes: 2 additions & 0 deletions R/theme-elements.r
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,8 @@ el_def <- function(class = NULL, inherit = NULL, description = NULL) {
panel.background = el_def("element_rect", "rect"),
panel.border = el_def("element_rect", "rect"),
panel.margin = el_def("unit"),
panel.margin.x = el_def("unit", "panel.margin"),
panel.margin.y = el_def("unit", "panel.margin"),
panel.grid.major.x = el_def("element_line", "panel.grid.major"),
panel.grid.major.y = el_def("element_line", "panel.grid.major"),
panel.grid.minor.x = el_def("element_line", "panel.grid.minor"),
Expand Down
6 changes: 5 additions & 1 deletion R/theme.r
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ print.theme <- function(x, ...) str(x)
#' (\code{element_rect}; inherits from \code{rect}) \cr
#' panel.margin \tab margin around facet panels
#' (\code{unit}) \cr
#' panel.margin.x \tab horizontal margin around facet panels
#' (\code{unit}; inherits from \code{panel.margin}) \cr
#' panel.margin.y \tab vertical margin around facet panels
#' (\code{unit}; inherits from \code{panel.margin}) \cr
#' panel.grid \tab grid lines
#' (\code{element_line}; inherits from \code{line}) \cr
#' panel.grid.major \tab major grid lines
Expand Down Expand Up @@ -271,7 +275,7 @@ print.theme <- function(x, ...) str(x)
#' k + theme(strip.text.x = element_text(colour = "red", angle = 45, size = 10,
#' hjust = 0.5, vjust = 0.5))
#' k + theme(panel.margin = unit(5, "lines"))
#' k + theme(panel.margin = unit(0, "lines"))
#' k + theme(panel.margin.y = unit(0, "lines"))
#'
#'
#' # Modify a theme and save it
Expand Down
3 changes: 3 additions & 0 deletions man/diamonds.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
\alias{diamonds}
\title{Prices of 50,000 round cut diamonds}
\format{A data frame with 53940 rows and 10 variables}
\usage{
data(diamonds)
}
\description{
A dataset containing the prices and other attributes of
almost 54,000 diamonds. The variables are as follows:
Expand Down
3 changes: 3 additions & 0 deletions man/economics.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
\alias{economics}
\title{US economic time series.}
\format{A data frame with 478 rows and 6 variables}
\usage{
data(economics)
}
\description{
This dataset was produced from US economic time series
data available from
Expand Down
2 changes: 1 addition & 1 deletion man/geom_abline.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
data for this layer.}

\item{position}{The position adjustment to use for
overlappling points on this layer}
overlapping points on this layer}

\item{...}{other arguments passed on to
\code{\link{layer}}. This can include aesthetics whose
Expand Down
2 changes: 1 addition & 1 deletion man/geom_area.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
data for this layer.}

\item{position}{The position adjustment to use for
overlappling points on this layer}
overlapping points on this layer}

\item{na.rm}{If \code{FALSE} (the default), removes
missing values with a warning. If \code{TRUE} silently
Expand Down
2 changes: 1 addition & 1 deletion man/geom_bar.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
data for this layer.}

\item{position}{The position adjustment to use for
overlappling points on this layer}
overlapping points on this layer}

\item{...}{other arguments passed on to
\code{\link{layer}}. This can include aesthetics whose
Expand Down
2 changes: 1 addition & 1 deletion man/geom_bin2d.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
data for this layer.}

\item{position}{The position adjustment to use for
overlappling points on this layer}
overlapping points on this layer}

\item{...}{other arguments passed on to
\code{\link{layer}}. This can include aesthetics whose
Expand Down
2 changes: 1 addition & 1 deletion man/geom_blank.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
data for this layer.}

\item{position}{The position adjustment to use for
overlappling points on this layer}
overlapping points on this layer}

\item{...}{other arguments passed on to
\code{\link{layer}}. This can include aesthetics whose
Expand Down
2 changes: 1 addition & 1 deletion man/geom_boxplot.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
data for this layer.}

\item{position}{The position adjustment to use for
overlappling points on this layer}
overlapping points on this layer}

\item{...}{other arguments passed on to
\code{\link{layer}}. This can include aesthetics whose
Expand Down
2 changes: 1 addition & 1 deletion man/geom_contour.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
data for this layer.}

\item{position}{The position adjustment to use for
overlappling points on this layer}
overlapping points on this layer}

\item{na.rm}{If \code{FALSE} (the default), removes
missing values with a warning. If \code{TRUE} silently
Expand Down
2 changes: 1 addition & 1 deletion man/geom_crossbar.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
data for this layer.}

\item{position}{The position adjustment to use for
overlappling points on this layer}
overlapping points on this layer}

\item{...}{other arguments passed on to
\code{\link{layer}}. This can include aesthetics whose
Expand Down
2 changes: 1 addition & 1 deletion man/geom_density.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
data for this layer.}

\item{position}{The position adjustment to use for
overlappling points on this layer}
overlapping points on this layer}

\item{na.rm}{If \code{FALSE} (the default), removes
missing values with a warning. If \code{TRUE} silently
Expand Down
2 changes: 1 addition & 1 deletion man/geom_density2d.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
data for this layer.}

\item{position}{The position adjustment to use for
overlappling points on this layer}
overlapping points on this layer}

\item{na.rm}{If \code{FALSE} (the default), removes
missing values with a warning. If \code{TRUE} silently
Expand Down
2 changes: 1 addition & 1 deletion man/geom_dotplot.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
data for this layer.}

\item{position}{The position adjustment to use for
overlappling points on this layer}
overlapping points on this layer}

\item{na.rm}{If \code{FALSE} (the default), removes
missing values with a warning. If \code{TRUE} silently
Expand Down
2 changes: 1 addition & 1 deletion man/geom_errorbar.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
data for this layer.}

\item{position}{The position adjustment to use for
overlappling points on this layer}
overlapping points on this layer}

\item{...}{other arguments passed on to
\code{\link{layer}}. This can include aesthetics whose
Expand Down
2 changes: 1 addition & 1 deletion man/geom_errorbarh.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
data for this layer.}

\item{position}{The position adjustment to use for
overlappling points on this layer}
overlapping points on this layer}

\item{...}{other arguments passed on to
\code{\link{layer}}. This can include aesthetics whose
Expand Down
2 changes: 1 addition & 1 deletion man/geom_freqpoly.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
data for this layer.}

\item{position}{The position adjustment to use for
overlappling points on this layer}
overlapping points on this layer}

\item{...}{other arguments passed on to
\code{\link{layer}}. This can include aesthetics whose
Expand Down
2 changes: 1 addition & 1 deletion man/geom_hex.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
data for this layer.}

\item{position}{The position adjustment to use for
overlappling points on this layer}
overlapping points on this layer}

\item{...}{other arguments passed on to
\code{\link{layer}}. This can include aesthetics whose
Expand Down
2 changes: 1 addition & 1 deletion man/geom_histogram.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
data for this layer.}

\item{position}{The position adjustment to use for
overlappling points on this layer}
overlapping points on this layer}

\item{...}{other arguments passed on to
\code{\link{layer}}. This can include aesthetics whose
Expand Down
2 changes: 1 addition & 1 deletion man/geom_hline.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
data for this layer.}

\item{position}{The position adjustment to use for
overlappling points on this layer}
overlapping points on this layer}

\item{...}{other arguments passed on to
\code{\link{layer}}. This can include aesthetics whose
Expand Down
2 changes: 1 addition & 1 deletion man/geom_jitter.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
data for this layer.}

\item{position}{The position adjustment to use for
overlappling points on this layer}
overlapping points on this layer}

\item{na.rm}{If \code{FALSE} (the default), removes
missing values with a warning. If \code{TRUE} silently
Expand Down
2 changes: 1 addition & 1 deletion man/geom_line.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
data for this layer.}

\item{position}{The position adjustment to use for
overlappling points on this layer}
overlapping points on this layer}

\item{...}{other arguments passed on to
\code{\link{layer}}. This can include aesthetics whose
Expand Down
2 changes: 1 addition & 1 deletion man/geom_linerange.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
data for this layer.}

\item{position}{The position adjustment to use for
overlappling points on this layer}
overlapping points on this layer}

\item{...}{other arguments passed on to
\code{\link{layer}}. This can include aesthetics whose
Expand Down
2 changes: 1 addition & 1 deletion man/geom_path.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
data for this layer.}

\item{position}{The position adjustment to use for
overlappling points on this layer}
overlapping points on this layer}

\item{na.rm}{If \code{FALSE} (the default), removes
missing values with a warning. If \code{TRUE} silently
Expand Down
2 changes: 1 addition & 1 deletion man/geom_point.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
data for this layer.}

\item{position}{The position adjustment to use for
overlappling points on this layer}
overlapping points on this layer}

\item{na.rm}{If \code{FALSE} (the default), removes
missing values with a warning. If \code{TRUE} silently
Expand Down
2 changes: 1 addition & 1 deletion man/geom_pointrange.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
data for this layer.}

\item{position}{The position adjustment to use for
overlappling points on this layer}
overlapping points on this layer}

\item{...}{other arguments passed on to
\code{\link{layer}}. This can include aesthetics whose
Expand Down
2 changes: 1 addition & 1 deletion man/geom_polygon.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
data for this layer.}

\item{position}{The position adjustment to use for
overlappling points on this layer}
overlapping points on this layer}

\item{...}{other arguments passed on to
\code{\link{layer}}. This can include aesthetics whose
Expand Down
2 changes: 1 addition & 1 deletion man/geom_quantile.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
data for this layer.}

\item{position}{The position adjustment to use for
overlappling points on this layer}
overlapping points on this layer}

\item{na.rm}{If \code{FALSE} (the default), removes
missing values with a warning. If \code{TRUE} silently
Expand Down
2 changes: 1 addition & 1 deletion man/geom_raster.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
data for this layer.}

\item{position}{The position adjustment to use for
overlappling points on this layer}
overlapping points on this layer}

\item{...}{other arguments passed on to
\code{\link{layer}}. This can include aesthetics whose
Expand Down
2 changes: 1 addition & 1 deletion man/geom_rect.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
data for this layer.}

\item{position}{The position adjustment to use for
overlappling points on this layer}
overlapping points on this layer}

\item{...}{other arguments passed on to
\code{\link{layer}}. This can include aesthetics whose
Expand Down
2 changes: 1 addition & 1 deletion man/geom_ribbon.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
data for this layer.}

\item{position}{The position adjustment to use for
overlappling points on this layer}
overlapping points on this layer}

\item{na.rm}{If \code{FALSE} (the default), removes
missing values with a warning. If \code{TRUE} silently
Expand Down
2 changes: 1 addition & 1 deletion man/geom_rug.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
data for this layer.}

\item{position}{The position adjustment to use for
overlappling points on this layer}
overlapping points on this layer}

\item{...}{other arguments passed on to
\code{\link{layer}}. This can include aesthetics whose
Expand Down
2 changes: 1 addition & 1 deletion man/geom_segment.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
data for this layer.}

\item{position}{The position adjustment to use for
overlappling points on this layer}
overlapping points on this layer}

\item{na.rm}{If \code{FALSE} (the default), removes
missing values with a warning. If \code{TRUE} silently
Expand Down
Loading