Skip to content

New theme option to put panel/grid on top of data #993

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 4 commits into from
Jun 18, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
NEW FEATURES

* new theme setting `panel.ontop` (logical) allows placing background elements
(e.g., gridlines) on top of data. Usually used with blank or transparent
`panel.background`. (@noamross. #551)

ggplot2 1.0.0
----------------------------------------------------------------

Expand Down
7 changes: 6 additions & 1 deletion R/facet-grid-.r
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,12 @@ facet_panels.grid <- function(facet, panel, coord, theme, geom_grobs) {
bg <- coord_render_bg(coord, panel$range[[i]], theme)

geom_grobs <- lapply(geom_grobs, "[[", i)
panel_grobs <- c(list(bg), geom_grobs, list(fg))

if(theme$panel.ontop) {
panel_grobs <- c(geom_grobs, list(bg), list(fg))
} else {
panel_grobs <- c(list(bg), geom_grobs, list(fg))
}

gTree(children = do.call("gList", panel_grobs))
})
Expand Down
7 changes: 6 additions & 1 deletion R/facet-null.r
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,12 @@ facet_render.null <- function(facet, panel, coord, theme, geom_grobs) {

# Flatten layers - we know there's only one panel
geom_grobs <- lapply(geom_grobs, "[[", 1)
panel_grobs <- c(list(bg), geom_grobs, list(fg))

if(theme$panel.ontop) {
panel_grobs <- c(geom_grobs, list(bg), list(fg))
} else {
panel_grobs <- c(list(bg), geom_grobs, list(fg))
}

panel_grob <- gTree(children = do.call("gList", panel_grobs))
axis_h <- coord_render_axis_h(coord, range, theme)
Expand Down
7 changes: 6 additions & 1 deletion R/facet-wrap.r
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,12 @@ facet_panels.wrap <- function(facet, panel, coord, theme, geom_grobs) {
bg <- coord_render_bg(coord, panel$range[[i]], theme)

geom_grobs <- lapply(geom_grobs, "[[", i)
panel_grobs <- c(list(bg), geom_grobs, list(fg))

if(theme$panel.ontop) {
panel_grobs <- c(geom_grobs, list(bg), list(fg))
} else {
panel_grobs <- c(list(bg), geom_grobs, list(fg))
}

ggname(paste("panel", i, sep = "-"),
gTree(children = do.call("gList", panel_grobs)))
Expand Down
1 change: 1 addition & 0 deletions R/theme-defaults.r
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ theme_grey <- function(base_size = 12, base_family = "") {
panel.margin = unit(0.25, "lines"),
panel.margin.x = NULL,
panel.margin.y = NULL,
panel.ontop = FALSE,

strip.background = element_rect(fill = "grey80", colour = NA),
strip.text.x = element_text(),
Expand Down
1 change: 1 addition & 0 deletions R/theme-elements.r
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ el_def <- function(class = NULL, inherit = NULL, description = NULL) {
panel.grid.major.y = el_def("element_line", "panel.grid.major"),
panel.grid.minor.x = el_def("element_line", "panel.grid.minor"),
panel.grid.minor.y = el_def("element_line", "panel.grid.minor"),
panel.ontop = el_def("logical"),

strip.background = el_def("element_rect", "rect"),
strip.text.x = el_def("element_text", "strip.text"),
Expand Down
5 changes: 4 additions & 1 deletion R/theme.r
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ print.theme <- function(x, ...) str(x)
#' (\code{element_line}; inherits from \code{panel.grid.minor}) \cr
#' panel.grid.minor.y \tab horizontal minor grid lines
#' (\code{element_line}; inherits from \code{panel.grid.minor}) \cr
#' panel.ontop \tab option to place the panel (background, gridlines)
#' over the data layers. Usually used with a transparent
#' or blank \code{panel.background}. (\code{logical}) \cr
#'
#' plot.background \tab background of the entire plot
#' (\code{element_rect}; inherits from \code{rect}) \cr
Expand Down Expand Up @@ -423,7 +426,7 @@ add_theme <- function(t1, t2, t2name) {
# If x is NULL or element_blank, then just assign it y
x <- y
} else if (is.null(y) || is.character(y) || is.numeric(y) ||
inherits(y, "element_blank")) {
is.logical(y) || inherits(y, "element_blank")) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's this change related to?

# If y is NULL, or a string or numeric vector, or is element_blank, just replace x
x <- y
} else {
Expand Down
5 changes: 4 additions & 1 deletion man/theme.Rd
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
% Generated by roxygen2 (4.0.0): do not edit by hand
% Generated by roxygen2 (4.0.1): do not edit by hand
\name{theme}
\alias{theme}
\title{Set theme elements}
Expand Down Expand Up @@ -131,6 +131,9 @@ The individual theme elements are:
(\code{element_line}; inherits from \code{panel.grid.minor}) \cr
panel.grid.minor.y \tab horizontal minor grid lines
(\code{element_line}; inherits from \code{panel.grid.minor}) \cr
panel.ontop \tab option to place the panel (background, gridlines)
over the data layers. Usually used with a transparent
or blank \code{panel.background}. (\code{logical}) \cr

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