Skip to content

New themes panel.margin.x and panel.margin.y #914

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

Merged
merged 5 commits into from
Mar 25, 2014
Merged
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
3 changes: 3 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ ggplot2 0.9.3.1.99
* `ggpcp()`, `ggfluctuation()`, `ggmissing()`, `ggstructure()`, and
`ggorder()` are now defunct and have been removed.

* 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). (Kirill Müller. Fixes #839)
* `aes()` no more treats variables like `a..x..b as a calculated aesthetic.
(@krlmlr, #834.)

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
8 changes: 6 additions & 2 deletions man/theme.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,11 @@ last example below.
and grid lines. This should be used with \code{fill=NA}
(\code{element_rect}; inherits from \code{rect}) \cr
panel.margin \tab margin around facet panels
(\code{unit}) \cr panel.grid \tab grid lines
(\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
(\code{element_line}; inherits from \code{panel.grid})
Expand Down Expand Up @@ -227,7 +231,7 @@ k + theme(strip.background = element_rect(colour = "purple", fill = "pink",
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