From a6663c413b4d698baaceaa7816a060c98bf0c1fd Mon Sep 17 00:00:00 2001 From: jrowen Date: Wed, 30 Nov 2016 01:29:27 +0000 Subject: [PATCH 01/12] Improvements for dendrograms --- R/ggplotly.R | 1 + R/layers2traces.R | 46 ++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/R/ggplotly.R b/R/ggplotly.R index 78e20fb17f..753eae9937 100644 --- a/R/ggplotly.R +++ b/R/ggplotly.R @@ -702,6 +702,7 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all", ) # if theme(legend.position = "none") is used, don't show a legend _or_ guide + npscales$scales <- Filter(function(x) x$guide != "none", npscales$scales) if (npscales$n() == 0 || identical(theme$legend.position, "none")) { gglayout$showlegend <- FALSE } else { diff --git a/R/layers2traces.R b/R/layers2traces.R index f4413117f5..faa20c905c 100644 --- a/R/layers2traces.R +++ b/R/layers2traces.R @@ -260,6 +260,29 @@ to_basic.GeomRect <- function(data, prestats_data, layout, params, p, ...) { cbind(x = xmax, y = ymax, others), cbind(x = xmax, y = ymin, others)) }) + if (layout$xanchor == "x") { + dat <- with(data, { + rbind(cbind(x = ifelse(xmin == -Inf, layout$x_min, xmin), + y = ifelse(ymin == -Inf, layout$y_min, ymin), others), + cbind(x = ifelse(xmin == -Inf, layout$x_min, xmin), + y = ifelse(ymax == Inf, layout$y_max, ymax), others), + cbind(x = ifelse(xmax == -Inf, layout$x_max, xmax), + y = ifelse(ymax == Inf, layout$y_max, ymax), others), + cbind(x = ifelse(xmax == -Inf, layout$x_max, xmax), + y = ifelse(ymin == -Inf, layout$y_min, ymin), others)) + }) + } else { + dat <- with(data, { + rbind(cbind(x = ifelse(xmin == -Inf, layout$y_min, xmin), + y = ifelse(ymin == -Inf, layout$x_min, ymin), others), + cbind(x = ifelse(xmin == -Inf, layout$y_min, xmin), + y = ifelse(ymax == Inf, layout$x_max, ymax), others), + cbind(x = ifelse(xmax == -Inf, layout$y_max, xmax), + y = ifelse(ymax == Inf, layout$x_max, ymax), others), + cbind(x = ifelse(xmax == -Inf, layout$y_max, xmax), + y = ifelse(ymin == -Inf, layout$x_min, ymin), others)) + }) + } prefix_class(dat, c("GeomPolygon", "GeomRect")) } @@ -602,11 +625,22 @@ geom2trace.GeomBoxplot <- function(data, params, p) { #' @export -geom2trace.GeomText <- function(data, params, p) { +geom2trace.GeomText <- function(data, params, p) + text <- as.character(data[["label"]]) compact(list( x = data[["x"]], y = data[["y"]], - text = data[["label"]], + text = ifelse( + grepl("bold", data[["fontface"]]), + paste0("", + ifelse( + grepl("italic", data[["fontface"]]), + paste0("", text, ""), + text + ), + ""), + text + ), key = data[["key"]], frame = data[["frame"]], ids = data[["ids"]], @@ -618,6 +652,14 @@ geom2trace.GeomText <- function(data, params, p) { aes2plotly(data, params, "alpha") ) ), + textposition = paste0( + ifelse(data[["vjust"]] < 0.5, "top ", + ifelse(data[["vjust"]] > 0.5, "bottom ", "") + ), + ifelse(data[["hjust"]] < 0.5, "right ", + ifelse(data[["vjust"]] > 0.5, "left ", "center") + ) + ), type = "scatter", mode = "text", hoveron = hover_on(data) From 208eed42878baa2d2817665aca83bafa8b234114 Mon Sep 17 00:00:00 2001 From: jrowen Date: Wed, 30 Nov 2016 01:29:27 +0000 Subject: [PATCH 02/12] Improvements for dendrograms --- R/ggplotly.R | 1 + R/layers2traces.R | 44 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/R/ggplotly.R b/R/ggplotly.R index 78e20fb17f..753eae9937 100644 --- a/R/ggplotly.R +++ b/R/ggplotly.R @@ -702,6 +702,7 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all", ) # if theme(legend.position = "none") is used, don't show a legend _or_ guide + npscales$scales <- Filter(function(x) x$guide != "none", npscales$scales) if (npscales$n() == 0 || identical(theme$legend.position, "none")) { gglayout$showlegend <- FALSE } else { diff --git a/R/layers2traces.R b/R/layers2traces.R index f4413117f5..a6d23a1aa3 100644 --- a/R/layers2traces.R +++ b/R/layers2traces.R @@ -260,6 +260,29 @@ to_basic.GeomRect <- function(data, prestats_data, layout, params, p, ...) { cbind(x = xmax, y = ymax, others), cbind(x = xmax, y = ymin, others)) }) + if (layout$xanchor == "x") { + dat <- with(data, { + rbind(cbind(x = ifelse(xmin == -Inf, layout$x_min, xmin), + y = ifelse(ymin == -Inf, layout$y_min, ymin), others), + cbind(x = ifelse(xmin == -Inf, layout$x_min, xmin), + y = ifelse(ymax == Inf, layout$y_max, ymax), others), + cbind(x = ifelse(xmax == -Inf, layout$x_max, xmax), + y = ifelse(ymax == Inf, layout$y_max, ymax), others), + cbind(x = ifelse(xmax == -Inf, layout$x_max, xmax), + y = ifelse(ymin == -Inf, layout$y_min, ymin), others)) + }) + } else { + dat <- with(data, { + rbind(cbind(x = ifelse(xmin == -Inf, layout$y_min, xmin), + y = ifelse(ymin == -Inf, layout$x_min, ymin), others), + cbind(x = ifelse(xmin == -Inf, layout$y_min, xmin), + y = ifelse(ymax == Inf, layout$x_max, ymax), others), + cbind(x = ifelse(xmax == -Inf, layout$y_max, xmax), + y = ifelse(ymax == Inf, layout$x_max, ymax), others), + cbind(x = ifelse(xmax == -Inf, layout$y_max, xmax), + y = ifelse(ymin == -Inf, layout$x_min, ymin), others)) + }) + } prefix_class(dat, c("GeomPolygon", "GeomRect")) } @@ -603,10 +626,21 @@ geom2trace.GeomBoxplot <- function(data, params, p) { #' @export geom2trace.GeomText <- function(data, params, p) { + text <- as.character(data[["label"]]) compact(list( x = data[["x"]], y = data[["y"]], - text = data[["label"]], + text = ifelse( + grepl("bold", data[["fontface"]]), + paste0("", + ifelse( + grepl("italic", data[["fontface"]]), + paste0("", text, ""), + text + ), + ""), + text + ), key = data[["key"]], frame = data[["frame"]], ids = data[["ids"]], @@ -618,6 +652,14 @@ geom2trace.GeomText <- function(data, params, p) { aes2plotly(data, params, "alpha") ) ), + textposition = paste0( + ifelse(data[["vjust"]] < 0.5, "top ", + ifelse(data[["vjust"]] > 0.5, "bottom ", "") + ), + ifelse(data[["hjust"]] < 0.5, "right ", + ifelse(data[["vjust"]] > 0.5, "left ", "center") + ) + ), type = "scatter", mode = "text", hoveron = hover_on(data) From 4dcdebb11db798ba0d1eb794c1c65ac5fae73785 Mon Sep 17 00:00:00 2001 From: jrowen Date: Wed, 30 Nov 2016 01:56:49 +0000 Subject: [PATCH 03/12] Fix rect to handle coord flip --- R/layers2traces.R | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/R/layers2traces.R b/R/layers2traces.R index a6d23a1aa3..d3bfca579b 100644 --- a/R/layers2traces.R +++ b/R/layers2traces.R @@ -260,18 +260,7 @@ to_basic.GeomRect <- function(data, prestats_data, layout, params, p, ...) { cbind(x = xmax, y = ymax, others), cbind(x = xmax, y = ymin, others)) }) - if (layout$xanchor == "x") { - dat <- with(data, { - rbind(cbind(x = ifelse(xmin == -Inf, layout$x_min, xmin), - y = ifelse(ymin == -Inf, layout$y_min, ymin), others), - cbind(x = ifelse(xmin == -Inf, layout$x_min, xmin), - y = ifelse(ymax == Inf, layout$y_max, ymax), others), - cbind(x = ifelse(xmax == -Inf, layout$x_max, xmax), - y = ifelse(ymax == Inf, layout$y_max, ymax), others), - cbind(x = ifelse(xmax == -Inf, layout$x_max, xmax), - y = ifelse(ymin == -Inf, layout$y_min, ymin), others)) - }) - } else { + if (inherits(p$coordinates, "CoordFlip")) { dat <- with(data, { rbind(cbind(x = ifelse(xmin == -Inf, layout$y_min, xmin), y = ifelse(ymin == -Inf, layout$x_min, ymin), others), @@ -282,6 +271,17 @@ to_basic.GeomRect <- function(data, prestats_data, layout, params, p, ...) { cbind(x = ifelse(xmax == -Inf, layout$y_max, xmax), y = ifelse(ymin == -Inf, layout$x_min, ymin), others)) }) + } else { + dat <- with(data, { + rbind(cbind(x = ifelse(xmin == -Inf, layout$x_min, xmin), + y = ifelse(ymin == -Inf, layout$y_min, ymin), others), + cbind(x = ifelse(xmin == -Inf, layout$x_min, xmin), + y = ifelse(ymax == Inf, layout$y_max, ymax), others), + cbind(x = ifelse(xmax == -Inf, layout$x_max, xmax), + y = ifelse(ymax == Inf, layout$y_max, ymax), others), + cbind(x = ifelse(xmax == -Inf, layout$x_max, xmax), + y = ifelse(ymin == -Inf, layout$y_min, ymin), others)) + }) } prefix_class(dat, c("GeomPolygon", "GeomRect")) } From 10936b4e440010c4c4c12f77e618d0429c50c077 Mon Sep 17 00:00:00 2001 From: jrowen Date: Wed, 30 Nov 2016 12:40:55 +0000 Subject: [PATCH 04/12] Fix fontface --- R/layers2traces.R | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/R/layers2traces.R b/R/layers2traces.R index d3bfca579b..d7fcf562da 100644 --- a/R/layers2traces.R +++ b/R/layers2traces.R @@ -627,20 +627,20 @@ geom2trace.GeomBoxplot <- function(data, params, p) { #' @export geom2trace.GeomText <- function(data, params, p) { text <- as.character(data[["label"]]) + text <- ifelse( + grepl("bold", text), + paste0("", text, ""), + text + ) + text <- ifelse( + grepl("italic", text), + paste0("", text, ""), + text + ) compact(list( x = data[["x"]], y = data[["y"]], - text = ifelse( - grepl("bold", data[["fontface"]]), - paste0("", - ifelse( - grepl("italic", data[["fontface"]]), - paste0("", text, ""), - text - ), - ""), - text - ), + text = text, key = data[["key"]], frame = data[["frame"]], ids = data[["ids"]], From f7cce291944b49fd980953588670208c371e9d0a Mon Sep 17 00:00:00 2001 From: jrowen Date: Wed, 30 Nov 2016 13:28:01 +0000 Subject: [PATCH 05/12] Fix fontface --- R/layers2traces.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/layers2traces.R b/R/layers2traces.R index d7fcf562da..2497cab940 100644 --- a/R/layers2traces.R +++ b/R/layers2traces.R @@ -628,12 +628,12 @@ geom2trace.GeomBoxplot <- function(data, params, p) { geom2trace.GeomText <- function(data, params, p) { text <- as.character(data[["label"]]) text <- ifelse( - grepl("bold", text), + grepl("bold", data[["fontface"]]), paste0("", text, ""), text ) text <- ifelse( - grepl("italic", text), + grepl("italic", data[["fontface"]]), paste0("", text, ""), text ) From 7862f00a29b57663948ef6b7c42e07a6f631a7c6 Mon Sep 17 00:00:00 2001 From: jrowen Date: Wed, 30 Nov 2016 13:38:11 +0000 Subject: [PATCH 06/12] Fixed rect --- R/layers2traces.R | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/R/layers2traces.R b/R/layers2traces.R index 2497cab940..5465e39348 100644 --- a/R/layers2traces.R +++ b/R/layers2traces.R @@ -266,9 +266,9 @@ to_basic.GeomRect <- function(data, prestats_data, layout, params, p, ...) { y = ifelse(ymin == -Inf, layout$x_min, ymin), others), cbind(x = ifelse(xmin == -Inf, layout$y_min, xmin), y = ifelse(ymax == Inf, layout$x_max, ymax), others), - cbind(x = ifelse(xmax == -Inf, layout$y_max, xmax), + cbind(x = ifelse(xmax == Inf, layout$y_max, xmax), y = ifelse(ymax == Inf, layout$x_max, ymax), others), - cbind(x = ifelse(xmax == -Inf, layout$y_max, xmax), + cbind(x = ifelse(xmax == Inf, layout$y_max, xmax), y = ifelse(ymin == -Inf, layout$x_min, ymin), others)) }) } else { @@ -277,9 +277,9 @@ to_basic.GeomRect <- function(data, prestats_data, layout, params, p, ...) { y = ifelse(ymin == -Inf, layout$y_min, ymin), others), cbind(x = ifelse(xmin == -Inf, layout$x_min, xmin), y = ifelse(ymax == Inf, layout$y_max, ymax), others), - cbind(x = ifelse(xmax == -Inf, layout$x_max, xmax), + cbind(x = ifelse(xmax == Inf, layout$x_max, xmax), y = ifelse(ymax == Inf, layout$y_max, ymax), others), - cbind(x = ifelse(xmax == -Inf, layout$x_max, xmax), + cbind(x = ifelse(xmax == Inf, layout$x_max, xmax), y = ifelse(ymin == -Inf, layout$y_min, ymin), others)) }) } From 3e63f3ccf41607e7184e8bdb4813b19e0df6aa3a Mon Sep 17 00:00:00 2001 From: jrowen Date: Wed, 30 Nov 2016 13:59:48 +0000 Subject: [PATCH 07/12] Cleanup hjust --- R/layers2traces.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/layers2traces.R b/R/layers2traces.R index 5465e39348..8f108a15d7 100644 --- a/R/layers2traces.R +++ b/R/layers2traces.R @@ -656,8 +656,8 @@ geom2trace.GeomText <- function(data, params, p) { ifelse(data[["vjust"]] < 0.5, "top ", ifelse(data[["vjust"]] > 0.5, "bottom ", "") ), - ifelse(data[["hjust"]] < 0.5, "right ", - ifelse(data[["vjust"]] > 0.5, "left ", "center") + ifelse(data[["hjust"]] < 0.5, "right", + ifelse(data[["vjust"]] > 0.5, "left", "center") ) ), type = "scatter", From 16d097b2552ce357ea05ab222b6b504f94729fe2 Mon Sep 17 00:00:00 2001 From: jrowen Date: Wed, 30 Nov 2016 16:33:31 +0000 Subject: [PATCH 08/12] Clean-up GeomRect --- R/layers2traces.R | 44 ++++++++++++++++++-------------------------- 1 file changed, 18 insertions(+), 26 deletions(-) diff --git a/R/layers2traces.R b/R/layers2traces.R index 8f108a15d7..e637f7d52a 100644 --- a/R/layers2traces.R +++ b/R/layers2traces.R @@ -254,35 +254,27 @@ to_basic.GeomSegment <- function(data, prestats_data, layout, params, p, ...) { to_basic.GeomRect <- function(data, prestats_data, layout, params, p, ...) { data$group <- seq_len(nrow(data)) others <- data[!names(data) %in% c("xmin", "ymin", "xmax", "ymax", "y", "x")] - dat <- with(data, { - rbind(cbind(x = xmin, y = ymin, others), - cbind(x = xmin, y = ymax, others), - cbind(x = xmax, y = ymax, others), - cbind(x = xmax, y = ymin, others)) - }) if (inherits(p$coordinates, "CoordFlip")) { - dat <- with(data, { - rbind(cbind(x = ifelse(xmin == -Inf, layout$y_min, xmin), - y = ifelse(ymin == -Inf, layout$x_min, ymin), others), - cbind(x = ifelse(xmin == -Inf, layout$y_min, xmin), - y = ifelse(ymax == Inf, layout$x_max, ymax), others), - cbind(x = ifelse(xmax == Inf, layout$y_max, xmax), - y = ifelse(ymax == Inf, layout$x_max, ymax), others), - cbind(x = ifelse(xmax == Inf, layout$y_max, xmax), - y = ifelse(ymin == -Inf, layout$x_min, ymin), others)) - }) + x_min <- layout$y_min + x_max <- layout$y_max + y_min <- layout$x_min + y_max <- layout$x_max } else { - dat <- with(data, { - rbind(cbind(x = ifelse(xmin == -Inf, layout$x_min, xmin), - y = ifelse(ymin == -Inf, layout$y_min, ymin), others), - cbind(x = ifelse(xmin == -Inf, layout$x_min, xmin), - y = ifelse(ymax == Inf, layout$y_max, ymax), others), - cbind(x = ifelse(xmax == Inf, layout$x_max, xmax), - y = ifelse(ymax == Inf, layout$y_max, ymax), others), - cbind(x = ifelse(xmax == Inf, layout$x_max, xmax), - y = ifelse(ymin == -Inf, layout$y_min, ymin), others)) - }) + x_min <- layout$x_min + x_max <- layout$x_max + y_min <- layout$y_min + y_max <- layout$y_max } + dat <- with(data, { + rbind(cbind(x = ifelse(xmin == -Inf, x_min, xmin), + y = ifelse(ymin == -Inf, y_min, ymin), others), + cbind(x = ifelse(xmin == -Inf, x_min, xmin), + y = ifelse(ymax == Inf, y_max, ymax), others), + cbind(x = ifelse(xmax == Inf, x_max, xmax), + y = ifelse(ymax == Inf, y_max, ymax), others), + cbind(x = ifelse(xmax == Inf, x_max, xmax), + y = ifelse(ymin == -Inf, y_min, ymin), others)) + }) prefix_class(dat, c("GeomPolygon", "GeomRect")) } From dac04d2c2871e4496e70f6e9f3cbba26189c05d0 Mon Sep 17 00:00:00 2001 From: jrowen Date: Wed, 30 Nov 2016 16:58:33 +0000 Subject: [PATCH 09/12] Update fontface approach --- R/layers2traces.R | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/R/layers2traces.R b/R/layers2traces.R index e637f7d52a..d2d432529f 100644 --- a/R/layers2traces.R +++ b/R/layers2traces.R @@ -619,16 +619,10 @@ geom2trace.GeomBoxplot <- function(data, params, p) { #' @export geom2trace.GeomText <- function(data, params, p) { text <- as.character(data[["label"]]) - text <- ifelse( - grepl("bold", data[["fontface"]]), - paste0("", text, ""), - text - ) - text <- ifelse( - grepl("italic", data[["fontface"]]), - paste0("", text, ""), - text - ) + i_ind <- grepl("italic", data[["fontface"]]) + text[i_ind] <- paste0("", text[i_ind], "") + b_ind <- grepl("bold", data[["fontface"]]) + text[b_ind] <- paste0("", text[b_ind], "") compact(list( x = data[["x"]], y = data[["y"]], From 180fcfd3e6c5e58604478d47e9044941d11aa660 Mon Sep 17 00:00:00 2001 From: jrowen Date: Wed, 30 Nov 2016 18:00:45 +0000 Subject: [PATCH 10/12] Updated approach for guide = none --- R/ggplotly.R | 1 - R/layers2traces.R | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/R/ggplotly.R b/R/ggplotly.R index 753eae9937..78e20fb17f 100644 --- a/R/ggplotly.R +++ b/R/ggplotly.R @@ -702,7 +702,6 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all", ) # if theme(legend.position = "none") is used, don't show a legend _or_ guide - npscales$scales <- Filter(function(x) x$guide != "none", npscales$scales) if (npscales$n() == 0 || identical(theme$legend.position, "none")) { gglayout$showlegend <- FALSE } else { diff --git a/R/layers2traces.R b/R/layers2traces.R index d2d432529f..d269576a89 100644 --- a/R/layers2traces.R +++ b/R/layers2traces.R @@ -72,7 +72,7 @@ layers2traces <- function(data, prestats_data, layout, p) { # draw legends only for discrete scales discreteScales <- list() for (sc in p$scales$non_position_scales()$scales) { - if (sc$is_discrete()) { + if (sc$is_discrete() && sc$guide != "none") { discreteScales[[sc$aesthetics]] <- sc } } From 183b39f781cc34ee653720c57c535b8189287e90 Mon Sep 17 00:00:00 2001 From: Jonathan Owen Date: Wed, 30 Nov 2016 13:09:19 -0600 Subject: [PATCH 11/12] Another guide = none attempt --- R/layers2traces.R | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/R/layers2traces.R b/R/layers2traces.R index d269576a89..42c852c327 100644 --- a/R/layers2traces.R +++ b/R/layers2traces.R @@ -72,7 +72,7 @@ layers2traces <- function(data, prestats_data, layout, p) { # draw legends only for discrete scales discreteScales <- list() for (sc in p$scales$non_position_scales()$scales) { - if (sc$is_discrete() && sc$guide != "none") { + if (sc$is_discrete()) { discreteScales[[sc$aesthetics]] <- sc } } @@ -100,6 +100,7 @@ layers2traces <- function(data, prestats_data, layout, p) { d <- datz[[i]] # variables that produce multiple traces and deserve their own legend entries split_legend <- paste0(names(discreteScales), "_plotlyDomain") + show_legend <- paste0(names(Filter(function(x) x$guide != "none", discreteScales)), "_plotlyDomain") # add variable that produce multiple traces, but do _not_ deserve entries split_by <- c(split_legend, "PANEL", "frame", split_on(d)) # ensure the factor level orders (which determines traces order) @@ -120,7 +121,7 @@ layers2traces <- function(data, prestats_data, layout, p) { trs <- Map(function(x, y) { x$set <- attr(y, "set"); x}, trs, dl) # if we need a legend, set name/legendgroup/showlegend # note: this allows us to control multiple traces from one legend entry - if (any(split_legend %in% names(d))) { + if (any(show_legend %in% names(d))) { nms <- strsplit(names(trs), separator, fixed = TRUE) nms <- vapply(nms, function(x) { y <- unique(x[seq_along(split_legend)]) From 88110c0e0d6afd2ad2f3057609a118457e1a819d Mon Sep 17 00:00:00 2001 From: Jonathan Owen Date: Wed, 12 Apr 2017 08:03:49 -0500 Subject: [PATCH 12/12] Added geom_text and geom_rect tests --- tests/testthat/test-ggplot-rect.R | 33 +++++++++++++++++++++++++++++++ tests/testthat/test-ggplot-text.R | 25 +++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/tests/testthat/test-ggplot-rect.R b/tests/testthat/test-ggplot-rect.R index 19046cb6ae..21fbbe299d 100644 --- a/tests/testthat/test-ggplot-rect.R +++ b/tests/testthat/test-ggplot-rect.R @@ -139,3 +139,36 @@ test_that('Specifying alpha in hex color code works', { expect_match(info$data[[1]]$fillcolor, "rgba\\(0,0,0,0\\.0[6]+") }) +p1 = ggplot(data.frame(x = 1, y = 1)) + + geom_point(aes(x = x, y = y)) + + geom_rect(xmin = 0.9, xmax = 1.1, ymin = -Inf, ymax = Inf) +p2 = ggplot(data.frame(x = 1, y = 1)) + + geom_point(aes(x = x, y = y)) + + geom_rect(ymin = 0.9, ymax = 1.1, xmin = -Inf, xmax = Inf) + + coord_flip() +info1 <- save_outputs(p1, "rect-vert-inf") +info2 <- save_outputs(p2, "rect-vert-flip-inf") + +test_that("rect vertical inf is translated correctly", { + expect_identical(info1$data[[2]]$x, c(0.9, 0.9, 1.1, 1.1, 0.9)) + expect_identical(info1$data[[2]]$y, c(0.5, 1.5, 1.5, 0.5, 0.5)) + expect_identical(info2$data[[2]]$x, c(0.9, 1.1, 1.1, 0.9, 0.9)) + expect_identical(info2$data[[2]]$y, c(0.5, 0.5, 1.5, 1.5, 0.5)) +}) + +p3 = ggplot(data.frame(x = 1, y = 1)) + + geom_point(aes(x = x, y = y)) + + geom_rect(ymin = 0.9, ymax = 1.1, xmin = -Inf, xmax = Inf) +p4 = ggplot(data.frame(x = 1, y = 1)) + + geom_point(aes(x = x, y = y)) + + geom_rect(xmin = 0.9, xmax = 1.1, ymin = -Inf, ymax = Inf) + + coord_flip() +info3 <- save_outputs(p3, "rect-hor-inf") +info4 <- save_outputs(p4, "rect-hor-flip-inf") + +test_that("rect horizontal inf is translated correctly", { + expect_identical(info4$data[[2]]$y, c(0.9, 0.9, 1.1, 1.1, 0.9)) + expect_identical(info4$data[[2]]$x, c(0.5, 1.5, 1.5, 0.5, 0.5)) + expect_identical(info3$data[[2]]$y, c(0.9, 1.1, 1.1, 0.9, 0.9)) + expect_identical(info3$data[[2]]$x, c(0.5, 0.5, 1.5, 1.5, 0.5)) +}) diff --git a/tests/testthat/test-ggplot-text.R b/tests/testthat/test-ggplot-text.R index 4dfccf9db7..1af0700895 100644 --- a/tests/testthat/test-ggplot-text.R +++ b/tests/testthat/test-ggplot-text.R @@ -42,3 +42,28 @@ test_that("geom_text splits along colour", { # Right colour for each trace expect_true(L$data[[1]]$textfont$color != L$data[[2]]$textfont$color) }) + +gg1 = ggplot(data.frame(x = seq(5, 25, 5), y = 60)) + + geom_point(aes(x = x, y = y)) + + geom_text(x = 5, y = 60, label = "nothing") + + geom_text(x = 10, y = 60, label = "bold", fontface = "bold", hjust = 0, vjust = 0) + + geom_text(x = 15, y = 60, label = "italic", fontface = "italic", hjust = 1, vjust = 1) + + geom_text(x = 20, y = 60, label = "bold italic", fontface = "bold.italic", hjust = 0, vjust = 1) + + geom_text(x = 25, y = 60, label = "plain", fontface = "plain", hjust = 1, vjust = 0) +info1 <- save_outputs(gg1, "text-fontjust") + +test_that("fontface is translated correctly", { + expect_identical(info1$data[[2]]$text, rep("nothing", 5)) + expect_identical(info1$data[[3]]$text, rep("bold", 5)) + expect_identical(info1$data[[4]]$text, rep("italic", 5)) + expect_identical(info1$data[[5]]$text, rep("bold italic", 5)) + expect_identical(info1$data[[6]]$text, rep("plain", 5)) +}) + +test_that("hjust/vjust is translated correctly", { + expect_identical(info1$data[[2]]$textposition, rep("center", 5)) + expect_identical(info1$data[[3]]$textposition, rep("top right", 5)) + expect_identical(info1$data[[4]]$textposition, rep("bottom left", 5)) + expect_identical(info1$data[[5]]$textposition, rep("bottom right", 5)) + expect_identical(info1$data[[6]]$textposition, rep("top center", 5)) +})