Skip to content

Commit efd3cbd

Browse files
committed
Merge pull request #993 from noamross/feature/panel_on_top
New theme option to put panel/grid on top of data
2 parents a4b7089 + 56d9061 commit efd3cbd

File tree

8 files changed

+53
-4
lines changed

8 files changed

+53
-4
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ ggplot2 1.0.1.9000
101101
one place rather than having to navigate through multiple pages. Use
102102
of `qplot()` in examples has been minimised (#1123, @hrbrmstr)
103103

104+
* new theme setting `panel.ontop` (logical) allows placing background elements
105+
(e.g., gridlines) on top of data. Usually used with blank or transparent
106+
`panel.background`. (@noamross. #551)
107+
104108
ggplot2 1.0.1
105109
----------------------------------------------------------------
106110

R/facet-grid-.r

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

367367
geom_grobs <- lapply(geom_grobs, "[[", i)
368-
panel_grobs <- c(list(bg), geom_grobs, list(fg))
368+
369+
if(theme$panel.ontop) {
370+
panel_grobs <- c(geom_grobs, list(bg), list(fg))
371+
} else {
372+
panel_grobs <- c(list(bg), geom_grobs, list(fg))
373+
}
369374

370375
gTree(children = do.call("gList", panel_grobs))
371376
})

R/facet-null.r

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

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

5055
panel_grob <- gTree(children = do.call("gList", panel_grobs))
5156
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
@@ -202,7 +202,12 @@ facet_panels.wrap <- function(facet, panel, coord, theme, geom_grobs) {
202202
bg <- coord_render_bg(coord, panel$ranges[[i]], theme)
203203

204204
geom_grobs <- lapply(geom_grobs, "[[", i)
205-
panel_grobs <- c(list(bg), geom_grobs, list(fg))
205+
206+
if(theme$panel.ontop) {
207+
panel_grobs <- c(geom_grobs, list(bg), list(fg))
208+
} else {
209+
panel_grobs <- c(list(bg), geom_grobs, list(fg))
210+
}
206211

207212
ggname(paste("panel", i, sep = "-"),
208213
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
@@ -102,6 +102,7 @@ theme_grey <- function(base_size = 12, base_family = "") {
102102
panel.margin = unit(0.25, "lines"),
103103
panel.margin.x = NULL,
104104
panel.margin.y = NULL,
105+
panel.ontop = FALSE,
105106

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

R/theme-elements.r

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ el_def <- function(class = NULL, inherit = NULL, description = NULL) {
272272
panel.grid.major.y = el_def("element_line", "panel.grid.major"),
273273
panel.grid.minor.x = el_def("element_line", "panel.grid.minor"),
274274
panel.grid.minor.y = el_def("element_line", "panel.grid.minor"),
275+
panel.ontop = el_def("logical"),
275276

276277
strip.background = el_def("element_rect", "rect"),
277278
strip.text.x = el_def("element_text", "strip.text"),

R/theme.r

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,9 @@ print.theme <- function(x, ...) str(x)
157157
#' (\code{element_line}; inherits from \code{panel.grid.minor}) \cr
158158
#' panel.grid.minor.y \tab horizontal minor grid lines
159159
#' (\code{element_line}; inherits from \code{panel.grid.minor}) \cr
160+
#' panel.ontop \tab option to place the panel (background, gridlines)
161+
#' over the data layers. Usually used with a transparent
162+
#' or blank \code{panel.background}. (\code{logical}) \cr
160163
#'
161164
#' plot.background \tab background of the entire plot
162165
#' (\code{element_rect}; inherits from \code{rect}) \cr
@@ -293,6 +296,17 @@ print.theme <- function(x, ...) str(x)
293296
#' k + theme(panel.margin = unit(5, "lines"))
294297
#' k + theme(panel.margin.y = unit(0, "lines"))
295298
#'
299+
#' # Put gridlines on top
300+
#' meanprice <- tapply(diamonds$price, diamonds$cut, mean)
301+
#' cut <- factor(levels(diamonds$cut), levels = levels(diamonds$cut))
302+
#' df <- data.frame(meanprice, cut)
303+
#' g <- ggplot(df, aes(cut, meanprice)) + geom_bar(stat = "identity")
304+
#' g + geom_bar(stat = "identity") +
305+
#' theme(panel.background=element_blank(),
306+
#' panel.grid.major.x=element_blank(),
307+
#' panel.grid.minor.x = element_blank(),
308+
#' panel.grid.minor.y=element_blank(),
309+
#' panel.ontop=TRUE)
296310
#'
297311
#' # Modify a theme and save it
298312
#' mytheme <- theme_grey() + theme(plot.title = element_text(colour = "red"))
@@ -410,7 +424,7 @@ add_theme <- function(t1, t2, t2name) {
410424
# If x is NULL or element_blank, then just assign it y
411425
x <- y
412426
} else if (is.null(y) || is.character(y) || is.numeric(y) ||
413-
inherits(y, "element_blank")) {
427+
is.logical(y) || inherits(y, "element_blank")) {
414428
# If y is NULL, or a string or numeric vector, or is element_blank, just replace x
415429
x <- y
416430
} else {

man/theme.Rd

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ The individual theme elements are:
132132
(\code{element_line}; inherits from \code{panel.grid.minor}) \cr
133133
panel.grid.minor.y \tab horizontal minor grid lines
134134
(\code{element_line}; inherits from \code{panel.grid.minor}) \cr
135+
panel.ontop \tab option to place the panel (background, gridlines)
136+
over the data layers. Usually used with a transparent
137+
or blank \code{panel.background}. (\code{logical}) \cr
135138

136139
plot.background \tab background of the entire plot
137140
(\code{element_rect}; inherits from \code{rect}) \cr
@@ -253,6 +256,17 @@ k + theme(strip.text.x = element_text(colour = "red", angle = 45, size = 10,
253256
k + theme(panel.margin = unit(5, "lines"))
254257
k + theme(panel.margin.y = unit(0, "lines"))
255258

259+
# Put gridlines on top
260+
meanprice <- tapply(diamonds$price, diamonds$cut, mean)
261+
cut <- factor(levels(diamonds$cut), levels = levels(diamonds$cut))
262+
df <- data.frame(meanprice, cut)
263+
g <- ggplot(df, aes(cut, meanprice)) + geom_bar(stat = "identity")
264+
g + geom_bar(stat = "identity") +
265+
theme(panel.background=element_blank(),
266+
panel.grid.major.x=element_blank(),
267+
panel.grid.minor.x = element_blank(),
268+
panel.grid.minor.y=element_blank(),
269+
panel.ontop=TRUE)
256270

257271
# Modify a theme and save it
258272
mytheme <- theme_grey() + theme(plot.title = element_text(colour = "red"))

0 commit comments

Comments
 (0)