From 4d99445ebecca79c9a7bbdd2849c433ddf0dfaad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kirill=20M=C3=BCller?= Date: Thu, 11 Jul 2013 16:04:30 +0200 Subject: [PATCH 1/3] fix: use height_cm instead of width_cm --- R/facet-wrap.r | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/facet-wrap.r b/R/facet-wrap.r index 9a59cbbfce..73b67d6ced 100644 --- a/R/facet-wrap.r +++ b/R/facet-wrap.r @@ -164,7 +164,7 @@ 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)) ) heights <- list( panel = unit(aspect_ratio, "null"), From 9df62a6a038b9941a7bd3ada46720558c5aff2b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kirill=20M=C3=BCller?= Date: Thu, 11 Jul 2013 17:08:26 +0200 Subject: [PATCH 2/3] New themes panel.margin.x and panel.margin.y Fixes #839 Conflicts: NEWS R/facet-grid-.r Resolved trivially. --- NEWS | 3 +++ R/facet-grid-.r | 12 ++++++------ R/facet-wrap.r | 4 ++-- R/theme-defaults.r | 2 ++ R/theme-elements.r | 2 ++ R/theme.r | 6 +++++- 6 files changed, 20 insertions(+), 9 deletions(-) diff --git a/NEWS b/NEWS index 3b8aff10c9..c0f36611de 100644 --- a/NEWS +++ b/NEWS @@ -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) ggplot2 0.9.3.1 ---------------------------------------------------------------- diff --git a/R/facet-grid-.r b/R/facet-grid-.r index 43b8f0ae55..350a6dbe14 100644 --- a/R/facet-grid-.r +++ b/R/facet-grid-.r @@ -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) } } @@ -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 } @@ -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 } diff --git a/R/facet-wrap.r b/R/facet-wrap.r index 73b67d6ced..9f1213c2af 100644 --- a/R/facet-wrap.r +++ b/R/facet-wrap.r @@ -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, width_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) diff --git a/R/theme-defaults.r b/R/theme-defaults.r index 85d6e46122..1a3bcc507b 100644 --- a/R/theme-defaults.r +++ b/R/theme-defaults.r @@ -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(), diff --git a/R/theme-elements.r b/R/theme-elements.r index b8690deb30..80f953f303 100644 --- a/R/theme-elements.r +++ b/R/theme-elements.r @@ -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"), diff --git a/R/theme.r b/R/theme.r index b90c99cde4..36bb123a85 100644 --- a/R/theme.r +++ b/R/theme.r @@ -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 @@ -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 From ea53779d6ca50806ac2ed96bb8c27c83e146ac60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kirill=20M=C3=BCller?= Date: Tue, 25 Feb 2014 15:30:05 +0100 Subject: [PATCH 3/3] Roxygenize --- man/guide_colourbar.Rd | 2 +- man/theme.Rd | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/man/guide_colourbar.Rd b/man/guide_colourbar.Rd index 7815995246..69a6624018 100644 --- a/man/guide_colourbar.Rd +++ b/man/guide_colourbar.Rd @@ -2,7 +2,7 @@ \name{guide_colourbar} \alias{guide_colorbar} \alias{guide_colourbar} -\title{Contiuous colour bar guide.} +\title{Continuous colour bar guide.} \usage{ guide_colourbar(title = waiver(), title.position = NULL, title.theme = NULL, title.hjust = NULL, title.vjust = NULL, diff --git a/man/theme.Rd b/man/theme.Rd index 2bdbf862f7..79cf88f466 100644 --- a/man/theme.Rd +++ b/man/theme.Rd @@ -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}) @@ -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