Skip to content

Commit 2e722cc

Browse files
committed
New theme option to put panel/grid on top of data
Closes tidyverse#551
1 parent 4bb9270 commit 2e722cc

File tree

8 files changed

+34
-5
lines changed

8 files changed

+34
-5
lines changed

NEWS

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
NEW FEATURES
2+
3+
* new theme setting `panel.ontop` (logical) allows placing background elements
4+
(e.g., gridlines) on top of data. Usually used with blank or transparent
5+
`panel.background`. (@noamross. #551)
6+
17
ggplot2 1.0.0
28
----------------------------------------------------------------
39

R/facet-grid-.r

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,12 @@ facet_panels.grid <- function(facet, panel, coord, theme, geom_grobs) {
352352
bg <- coord_render_bg(coord, panel$range[[i]], theme)
353353

354354
geom_grobs <- lapply(geom_grobs, "[[", i)
355-
panel_grobs <- c(list(bg), geom_grobs, list(fg))
355+
356+
if(theme$panel.ontop) {
357+
panel_grobs <- c(geom_grobs, list(bg), list(fg))
358+
} else {
359+
panel_grobs <- c(list(bg), geom_grobs, list(fg))
360+
}
356361

357362
gTree(children = do.call("gList", panel_grobs))
358363
})

R/facet-null.r

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,12 @@ facet_render.null <- function(facet, panel, coord, theme, geom_grobs) {
4646

4747
# Flatten layers - we know there's only one panel
4848
geom_grobs <- lapply(geom_grobs, "[[", 1)
49-
panel_grobs <- c(list(bg), geom_grobs, list(fg))
49+
50+
if(theme$panel.ontop) {
51+
panel_grobs <- c(geom_grobs, list(bg), list(fg))
52+
} else {
53+
panel_grobs <- c(list(bg), geom_grobs, list(fg))
54+
}
5055

5156
panel_grob <- gTree(children = do.call("gList", panel_grobs))
5257
axis_h <- coord_render_axis_h(coord, range, theme)

R/facet-wrap.r

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,12 @@ facet_panels.wrap <- function(facet, panel, coord, theme, geom_grobs) {
197197
bg <- coord_render_bg(coord, panel$range[[i]], theme)
198198

199199
geom_grobs <- lapply(geom_grobs, "[[", i)
200-
panel_grobs <- c(list(bg), geom_grobs, list(fg))
200+
201+
if(theme$panel.ontop) {
202+
panel_grobs <- c(geom_grobs, list(bg), list(fg))
203+
} else {
204+
panel_grobs <- c(list(bg), geom_grobs, list(fg))
205+
}
201206

202207
ggname(paste("panel", i, sep = "-"),
203208
gTree(children = do.call("gList", panel_grobs)))

R/theme-defaults.r

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ theme_grey <- function(base_size = 12, base_family = "") {
9595
panel.margin = unit(0.25, "lines"),
9696
panel.margin.x = NULL,
9797
panel.margin.y = NULL,
98+
panel.ontop = FALSE,
9899

99100
strip.background = element_rect(fill = "grey80", colour = NA),
100101
strip.text.x = element_text(),

R/theme-elements.r

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ el_def <- function(class = NULL, inherit = NULL, description = NULL) {
312312
panel.grid.major.y = el_def("element_line", "panel.grid.major"),
313313
panel.grid.minor.x = el_def("element_line", "panel.grid.minor"),
314314
panel.grid.minor.y = el_def("element_line", "panel.grid.minor"),
315+
panel.ontop = el_def("logical"),
315316

316317
strip.background = el_def("element_rect", "rect"),
317318
strip.text.x = el_def("element_text", "strip.text"),

R/theme.r

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,9 @@ print.theme <- function(x, ...) str(x)
153153
#' (\code{element_line}; inherits from \code{panel.grid.minor}) \cr
154154
#' panel.grid.minor.y \tab horizontal minor grid lines
155155
#' (\code{element_line}; inherits from \code{panel.grid.minor}) \cr
156+
#' panel.ontop \tab option to place the panel (background, gridlines)
157+
#' over the data layers. Usually used with a transparent
158+
#' or blank \code{panel.background}. (\code{logical}) \cr
156159
#'
157160
#' plot.background \tab background of the entire plot
158161
#' (\code{element_rect}; inherits from \code{rect}) \cr
@@ -423,7 +426,7 @@ add_theme <- function(t1, t2, t2name) {
423426
# If x is NULL or element_blank, then just assign it y
424427
x <- y
425428
} else if (is.null(y) || is.character(y) || is.numeric(y) ||
426-
inherits(y, "element_blank")) {
429+
is.logical(y) || inherits(y, "element_blank")) {
427430
# If y is NULL, or a string or numeric vector, or is element_blank, just replace x
428431
x <- y
429432
} else {

man/theme.Rd

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
% Generated by roxygen2 (4.0.0): do not edit by hand
1+
% Generated by roxygen2 (4.0.1): do not edit by hand
22
\name{theme}
33
\alias{theme}
44
\title{Set theme elements}
@@ -131,6 +131,9 @@ The individual theme elements are:
131131
(\code{element_line}; inherits from \code{panel.grid.minor}) \cr
132132
panel.grid.minor.y \tab horizontal minor grid lines
133133
(\code{element_line}; inherits from \code{panel.grid.minor}) \cr
134+
panel.ontop \tab option to place the panel (background, gridlines)
135+
over the data layers. Usually used with a transparent
136+
or blank \code{panel.background}. (\code{logical}) \cr
134137

135138
plot.background \tab background of the entire plot
136139
(\code{element_rect}; inherits from \code{rect}) \cr

0 commit comments

Comments
 (0)