From 1883332764ec5a1bf558b0aef70d39156d474c07 Mon Sep 17 00:00:00 2001 From: Katiedaisey Date: Wed, 9 Mar 2016 16:17:29 -0500 Subject: [PATCH 01/11] reverts changes to theme-defaults.r necessary to solve inheritance problem --- R/theme-defaults.r | 8 -------- 1 file changed, 8 deletions(-) diff --git a/R/theme-defaults.r b/R/theme-defaults.r index 41e1ebce6a..a1d46e5433 100644 --- a/R/theme-defaults.r +++ b/R/theme-defaults.r @@ -77,8 +77,6 @@ theme_grey <- function(base_size = 11, base_family = "") { ), axis.line = element_line(), - axis.line.x = element_blank(), - axis.line.y = element_blank(), axis.text = element_text(size = rel(0.8), colour = "grey30"), axis.text.x = element_text(margin = margin(t = 0.8 * half_line / 2), vjust = 1), axis.text.y = element_text(margin = margin(r = 0.8 * half_line / 2), hjust = 1), @@ -221,8 +219,6 @@ theme_minimal <- function(base_size = 12, base_family = "") { strip.background = element_blank(), plot.background = element_blank(), axis.ticks = element_line(), - axis.ticks.x = element_blank(), - axis.ticks.y = element_blank(), axis.ticks.length = unit(1, "lines") ) } @@ -235,11 +231,7 @@ theme_classic <- function(base_size = 12, base_family = ""){ panel.border = element_blank(), axis.line = element_line(colour = "black"), panel.grid.major = element_line(), - panel.grid.major.x = element_blank(), - panel.grid.major.y = element_blank(), panel.grid.minor = element_line(), - panel.grid.minor.x = element_blank(), - panel.grid.minor.y = element_blank(), strip.background = element_rect(colour = "black", size = 0.5), legend.key = element_blank() ) From 70acbcbd566268c3e71dbc3d8e87cbf33690e7de Mon Sep 17 00:00:00 2001 From: Katiedaisey Date: Wed, 9 Mar 2016 16:28:15 -0500 Subject: [PATCH 02/11] updates theme.r for correct inheritance changes add_theme, combine_elements adds base_class --- R/theme.r | 54 +++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 43 insertions(+), 11 deletions(-) diff --git a/R/theme.r b/R/theme.r index 32f79a52a4..c90062fac4 100644 --- a/R/theme.r +++ b/R/theme.r @@ -479,6 +479,11 @@ add_theme <- function(t1, t2, t2name) { # The other form will simply drop NULL values t1[item] <- list(x) } + + # Update inherited items + for (item in names(t2)) { + t1[[item]] <- calc_element(item, t1) + } # If either theme is complete, then the combined theme is complete attr(t1, "complete") <- attr(t1, "complete") || attr(t2, "complete") @@ -600,20 +605,47 @@ calc_element <- function(element, theme, verbose = FALSE) { # @param e2 An element object which e1 inherits from combine_elements <- function(e1, e2) { - # If e2 is NULL, nothing to inherit - if (is.null(e2)) return(e1) + # If e1 is NULL or element_blank, inherit everything + if (is.null(e1) || inherits(e1, "element_blank")) {e1 <- e2} + + # If e1 is not NULL or blank and e2 is element_blank, inherit everything from root + if ((!is.null(e1) || !inherits(e1, "element_blank")) && (is.null(e2) || inherits(e2, "element_blank"))) { + e3 <- base_class(e1) + n <- vapply(e1[names(e3)], is.null, logical(1)) + e1[n] <- e3[n] + } - # If e1 is NULL, or if e2 is element_blank, inherit everything from e2 - if (is.null(e1) || inherits(e2, "element_blank")) return(e2) + # If e1 and e2 are not NULL or blank, inherit from e2 then root + if ((!is.null(e1) && !inherits(e1, "element_blank")) && (!is.null(e2) && !inherits(e2, "element_blank"))) { + # inherit from e2 + n <- vapply(e1[names(e2)], is.null, logical(1)) + e1[n] <- e2[n] - # If e1 has any NULL properties, inherit them from e2 - n <- vapply(e1[names(e2)], is.null, logical(1)) - e1[n] <- e2[n] + # inherit missing from base + e3 <- base_class(e1 = e1) + n <- vapply(e1[names(e3)], is.null, logical(1)) + e1[n] <- e3[n] + } # Calculate relative sizes - if (is.rel(e1$size)) { - e1$size <- e2$size * unclass(e1$size) - } + #if (is.rel(e1$size)) { + # e1$size <- e2$size * unclass(e1$size) + #} - e1 + return(e1) +} + +# Fine default properities of element +# +# @param e1 An element object +base_class <- function(e1) { + line = element_line(colour = "black", size = 0.5, linetype = 1,lineend = "butt") + rect = element_rect(fill = "white", colour = "black",size = 0.5, linetype = 1) + text = element_text(family = "", face = "plain",colour = "black", size = 11, lineheight = 0.9, hjust = 0.5, vjust = 0.5, angle = 0, margin = margin(), debug = FALSE) + e3 <- class(e1)[1] + if (e3 == "element_line") {e3 <- line} + else if (e3 == "element_rect") {e3 <- rect} + else if (e3 == "element_text") {e3 <- text} + + return(e3) } From 4c11e2d3545e93432a0ec83992cafe23ebdc6cf6 Mon Sep 17 00:00:00 2001 From: Katiedaisey Date: Wed, 9 Mar 2016 17:13:21 -0500 Subject: [PATCH 03/11] fixes #1565 element inheritance --- NEWS.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/NEWS.md b/NEWS.md index 4b5c99b5a5..205c74753f 100644 --- a/NEWS.md +++ b/NEWS.md @@ -121,6 +121,8 @@ * All elements should now inherit correctly for all themes except theme_void(). (@Katiedaisey, #1555) +* Inheritance of elements pull from default if parent is element_blank(). (@Katiedaisey, #1565) + # ggplot2 2.0.0 ## Major changes From 1f4c6411762e56ae738d0d74275cb438f1a40020 Mon Sep 17 00:00:00 2001 From: Katiedaisey Date: Wed, 9 Mar 2016 19:13:32 -0500 Subject: [PATCH 04/11] updates theme.r for #1565 --- R/theme.r | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/R/theme.r b/R/theme.r index c90062fac4..cb6af03bc1 100644 --- a/R/theme.r +++ b/R/theme.r @@ -628,9 +628,9 @@ combine_elements <- function(e1, e2) { } # Calculate relative sizes - #if (is.rel(e1$size)) { - # e1$size <- e2$size * unclass(e1$size) - #} + if (is.rel(e1$size)) { + e1$size <- e2$size * unclass(e1$size) + } return(e1) } From 93d84ba0fd1a9d5b695b07139d87587990ddbb37 Mon Sep 17 00:00:00 2001 From: Katiedaisey Date: Wed, 9 Mar 2016 19:16:00 -0500 Subject: [PATCH 05/11] updates theme.r for #1565 --- R/theme-defaults.r | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/theme-defaults.r b/R/theme-defaults.r index a1d46e5433..e8a1d57b12 100644 --- a/R/theme-defaults.r +++ b/R/theme-defaults.r @@ -230,8 +230,8 @@ theme_classic <- function(base_size = 12, base_family = ""){ theme( panel.border = element_blank(), axis.line = element_line(colour = "black"), - panel.grid.major = element_line(), - panel.grid.minor = element_line(), + panel.grid.major = element_blank(), + panel.grid.minor = element_blank(), strip.background = element_rect(colour = "black", size = 0.5), legend.key = element_blank() ) From 0f971ea96ae7676513f67616da84ed4dbcbf2144 Mon Sep 17 00:00:00 2001 From: Katiedaisey Date: Wed, 9 Mar 2016 19:24:34 -0500 Subject: [PATCH 06/11] updates theme_void() for #1565 --- R/theme-defaults.r | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/R/theme-defaults.r b/R/theme-defaults.r index e8a1d57b12..fb3b5ed912 100644 --- a/R/theme-defaults.r +++ b/R/theme-defaults.r @@ -276,6 +276,10 @@ theme_void <- function(base_size = 12, base_family = "") { margin = margin(), debug = FALSE ), plot.margin = unit(c(0, 0, 0, 0), "lines"), + panel.background = element_blank(), + panel.grid.major = element_blank(), + axis.line = element_blank(), + axis.ticks = element_blank(), axis.text.x = element_blank(), axis.text.y = element_blank(), axis.title.x = element_blank(), @@ -283,6 +287,7 @@ theme_void <- function(base_size = 12, base_family = "") { legend.text = element_text(size = rel(0.8)), legend.title = element_blank(), strip.text = element_text(size = rel(0.8)), + strip.background = element_blank(), complete = TRUE ) From 419943cea8e6e954d2f22d58309965f0b0c4c682 Mon Sep 17 00:00:00 2001 From: Katiedaisey Date: Wed, 9 Mar 2016 21:20:35 -0500 Subject: [PATCH 07/11] updates rel sizing in combine_elements --- R/theme.r | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/R/theme.r b/R/theme.r index 7e6a1e52a0..90594b17fe 100644 --- a/R/theme.r +++ b/R/theme.r @@ -599,8 +599,10 @@ combine_elements <- function(e1, e2) { } # Calculate relative sizes - if (is.rel(e1$size)) { + if (is.rel(e1$size) && !is.null(e2$size)) { e1$size <- e2$size * unclass(e1$size) + } else if (is.rel(e1$size) $$ !is.null(e3$size)) { + e1$size <- e3$size * unclass(e1$size) } return(e1) From 3bc81f3b15f39fe441b007441f318a28a9ce0299 Mon Sep 17 00:00:00 2001 From: Katiedaisey Date: Wed, 9 Mar 2016 21:25:57 -0500 Subject: [PATCH 08/11] fixes typo --- R/theme.r | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/theme.r b/R/theme.r index 90594b17fe..a58c0cf326 100644 --- a/R/theme.r +++ b/R/theme.r @@ -601,7 +601,7 @@ combine_elements <- function(e1, e2) { # Calculate relative sizes if (is.rel(e1$size) && !is.null(e2$size)) { e1$size <- e2$size * unclass(e1$size) - } else if (is.rel(e1$size) $$ !is.null(e3$size)) { + } else if (is.rel(e1$size) && !is.null(e3$size)) { e1$size <- e3$size * unclass(e1$size) } From 16f4bfd8f964628bcc30cafd1813909a5cbdc6d1 Mon Sep 17 00:00:00 2001 From: Katiedaisey Date: Wed, 9 Mar 2016 22:02:43 -0500 Subject: [PATCH 09/11] comment on legend.justification --- R/theme-elements.r | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/theme-elements.r b/R/theme-elements.r index 13f1a2859c..c085708c5b 100644 --- a/R/theme-elements.r +++ b/R/theme-elements.r @@ -255,7 +255,7 @@ el_def <- function(class = NULL, inherit = NULL, description = NULL) { legend.title.align = el_def("character"), legend.position = el_def("character"), # Need to also accept numbers legend.direction = el_def("character"), - legend.justification = el_def("character"), + legend.justification = el_def("character"), # Need to also accept numbers legend.box = el_def("character"), legend.box.just = el_def("character"), From 68745011edd22ff9738e7e79d3dbd33c56c14cfe Mon Sep 17 00:00:00 2001 From: Katiedaisey Date: Wed, 9 Mar 2016 22:03:06 -0500 Subject: [PATCH 10/11] updates error catching for legend.position and legend.justification --- R/theme.r | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/R/theme.r b/R/theme.r index a58c0cf326..994b5a3aaf 100644 --- a/R/theme.r +++ b/R/theme.r @@ -450,7 +450,7 @@ add_theme <- function(t1, t2, t2name) { # The other form will simply drop NULL values t1[item] <- list(x) } - + # Update inherited items for (item in names(t2)) { t1[[item]] <- calc_element(item, t1) @@ -542,6 +542,12 @@ calc_element <- function(element, theme, verbose = FALSE) { # it is of the class specified in .element_tree if (!is.null(theme[[element]]) && !inherits(theme[[element]], .element_tree[[element]]$class)) { + if (element != "legend.position" && element != "legend.justification") { + stop(element, " should have class ", .element_tree[[element]]$class) + } + } + + if ((element == "legend.position" || element == "legend.justification") && (class(theme[[element]])[1] != "character" && class(theme[[element]])[1] != "numeric")) { stop(element, " should have class ", .element_tree[[element]]$class) } @@ -578,7 +584,7 @@ combine_elements <- function(e1, e2) { # If e1 is NULL or element_blank, inherit everything if (is.null(e1) || inherits(e1, "element_blank")) {e1 <- e2} - + # If e1 is not NULL or blank and e2 is element_blank, inherit everything from root if ((!is.null(e1) || !inherits(e1, "element_blank")) && (is.null(e2) || inherits(e2, "element_blank"))) { e3 <- base_class(e1) From b1601134096439a876d00f53ffef5e5f16502023 Mon Sep 17 00:00:00 2001 From: "Katie.Daisey" Date: Thu, 10 Mar 2016 12:04:52 -0500 Subject: [PATCH 11/11] updates theme.r to pass all checks except 1 --- R/theme.r | 42 ++++++++++++------------------------------ 1 file changed, 12 insertions(+), 30 deletions(-) diff --git a/R/theme.r b/R/theme.r index 994b5a3aaf..add0bfdef9 100644 --- a/R/theme.r +++ b/R/theme.r @@ -452,9 +452,9 @@ add_theme <- function(t1, t2, t2name) { } # Update inherited items - for (item in names(t2)) { - t1[[item]] <- calc_element(item, t1) - } + #for (item in names(t2)) { + # t1[[item]] <- calc_element(item, t1) + #} # If either theme is complete, then the combined theme is complete attr(t1, "complete") <- attr(t1, "complete") || attr(t2, "complete") @@ -572,7 +572,13 @@ calc_element <- function(element, theme, verbose = FALSE) { parents <- lapply(pnames, calc_element, theme, verbose) # Combine the properties of this element with all parents - Reduce(combine_elements, parents, theme[[element]]) + element <- Reduce(combine_elements, parents, theme[[element]]) + + # Check if base is blank + #if (inherits(parents[[length(parents)]], "element_blank")) { + # element <- element_blank() + #} + return(element) } @@ -585,46 +591,22 @@ combine_elements <- function(e1, e2) { # If e1 is NULL or element_blank, inherit everything if (is.null(e1) || inherits(e1, "element_blank")) {e1 <- e2} - # If e1 is not NULL or blank and e2 is element_blank, inherit everything from root + # If e1 is not NULL or blank and e2 is element_blank, inherit everything from farther up tree if ((!is.null(e1) || !inherits(e1, "element_blank")) && (is.null(e2) || inherits(e2, "element_blank"))) { - e3 <- base_class(e1) - n <- vapply(e1[names(e3)], is.null, logical(1)) - e1[n] <- e3[n] } - # If e1 and e2 are not NULL or blank, inherit from e2 then root + # If e1 and e2 are not NULL or blank, inherit from e2 then farther up tree if ((!is.null(e1) && !inherits(e1, "element_blank")) && (!is.null(e2) && !inherits(e2, "element_blank"))) { # inherit from e2 n <- vapply(e1[names(e2)], is.null, logical(1)) e1[n] <- e2[n] - # inherit missing from base - e3 <- base_class(e1 = e1) - n <- vapply(e1[names(e3)], is.null, logical(1)) - e1[n] <- e3[n] } # Calculate relative sizes if (is.rel(e1$size) && !is.null(e2$size)) { e1$size <- e2$size * unclass(e1$size) - } else if (is.rel(e1$size) && !is.null(e3$size)) { - e1$size <- e3$size * unclass(e1$size) } return(e1) } - -# Fine default properities of element -# -# @param e1 An element object -base_class <- function(e1) { - line = element_line(colour = "black", size = 0.5, linetype = 1,lineend = "butt") - rect = element_rect(fill = "white", colour = "black",size = 0.5, linetype = 1) - text = element_text(family = "", face = "plain",colour = "black", size = 11, lineheight = 0.9, hjust = 0.5, vjust = 0.5, angle = 0, margin = margin(), debug = FALSE) - e3 <- class(e1)[1] - if (e3 == "element_line") {e3 <- line} - else if (e3 == "element_rect") {e3 <- rect} - else if (e3 == "element_text") {e3 <- text} - - return(e3) -}