From e1089348788227e0f47ccf7c9e8e82ac3ab96525 Mon Sep 17 00:00:00 2001 From: Marianne Corvellec Date: Wed, 22 Oct 2014 01:29:18 -0400 Subject: [PATCH 1/6] Support conversion for geom_vline() --- R/ggplotly.R | 7 +++++++ R/trace_generation.R | 31 +++++++++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/R/ggplotly.R b/R/ggplotly.R index 0293440da8..d9fa054f3c 100644 --- a/R/ggplotly.R +++ b/R/ggplotly.R @@ -98,6 +98,10 @@ gg2list <- function(p){ xrange <- sapply(ggranges, `[[`, "x.range", simplify=FALSE, USE.NAMES=FALSE) ggxmin <- min(sapply(xrange, min)) ggxmax <- max(sapply(xrange, max)) + # Extract y.range + yrange <- sapply(ggranges, `[[`, "y.range", simplify=FALSE, USE.NAMES=FALSE) + ggymin <- min(sapply(yrange, min)) + ggymax <- max(sapply(yrange, max)) # Get global size range because we need some of its info in layer2traces if ("size.name" %in% name.names) { @@ -168,6 +172,9 @@ gg2list <- function(p){ # Add global x-range info misc$prestats.data$globxmin <- ggxmin misc$prestats.data$globxmax <- ggxmax + # Add global y-range info + misc$prestats.data$globymin <- ggymin + misc$prestats.data$globymax <- ggymax # Add global size info if relevant if ("size.name" %in% name.names) { diff --git a/R/trace_generation.R b/R/trace_generation.R index 9ac0471cb0..6ded6789ed 100644 --- a/R/trace_generation.R +++ b/R/trace_generation.R @@ -121,7 +121,7 @@ layer2traces <- function(l, d, misc) { } ## Then split on visual characteristics that will get different ## legend entries. - data.list <- if(basic$geom %in% names(markLegends)){ + data.list <- if (basic$geom %in% names(markLegends)) { mark.names <- markLegends[[basic$geom]] ## However, continuously colored points are an exception: they do ## not need a legend entry, and they can be efficiently rendered @@ -152,11 +152,24 @@ layer2traces <- function(l, d, misc) { } } + # Split hline and vline when multiple + if (g$geom == "hline" || g$geom == "vline") { + if (nrow(g$data) > 1) { + df.list <- split(basic$data, rep(1:nrow(g$data))) #, drop=TRUE) + data.list <- lapply(df.list, function(df) { + params <- basic$params + list(data=df, + params=params) + }) + } + } + ## case of no legend, if either of the two ifs above failed. if(is.null(data.list)){ data.list <- structure(list(list(data=basic$data, params=basic$params)), names=basic$params$name) } + getTrace <- geom2trace[[basic$geom]] if(is.null(getTrace)){ warning("Conversion not implemented for geom_", @@ -167,7 +180,8 @@ layer2traces <- function(l, d, misc) { } traces <- NULL names.in.legend <- NULL - for(data.i in seq_along(data.list)){ + + for (data.i in seq_along(data.list)) { data.params <- data.list[[data.i]] data.params$params$stat.type <- l$stat$objname tr <- do.call(getTrace, data.params) @@ -303,6 +317,11 @@ toBasic <- list( g$params$xend <- max(g$prestats.data$globxmax) g }, + vline=function(g) { + g$params$ystart <- min(g$prestats.data$globymin) + g$params$yend <- max(g$prestats.data$globymax) + g + }, point=function(g) { if ("size" %in% names(g$data)) { g$params$sizemin <- min(g$prestats.data$globsizemin) @@ -527,5 +546,13 @@ geom2trace <- list( type="scatter", mode="lines", line=paramORdefault(params, aes2line, line.defaults)) + }, + vline=function(data, params) { + list(x=c(data$xintercept, data$xintercept), + y=c(params$ystart, params$yend), + name=params$name, + type="scatter", + mode="lines", + line=paramORdefault(params, aes2line, line.defaults)) } ) From 66d166aecdca8e3957ef57ed6ba053953537f582 Mon Sep 17 00:00:00 2001 From: Marianne Corvellec Date: Wed, 22 Oct 2014 13:35:58 -0400 Subject: [PATCH 2/6] Add tests for geom_vline --- tests/testthat/test-ggplot-vline.R | 42 ++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 tests/testthat/test-ggplot-vline.R diff --git a/tests/testthat/test-ggplot-vline.R b/tests/testthat/test-ggplot-vline.R new file mode 100644 index 0000000000..e02305433c --- /dev/null +++ b/tests/testthat/test-ggplot-vline.R @@ -0,0 +1,42 @@ +context("Vline") +# Vertical line + +x1 <- seq(from=0, to=3.5, by=0.5) +x2 <- x1 * 0.95 +df <- data.frame("x1"=x1, "x2"=x2) +gg <- ggplot(df) + geom_point(aes(x=x1, y=x2)) + +test_that("second trace be the vline", { + gg <- gg + geom_vline(xintercept=1.1, colour="green", size=3) + + L <- gg2list(gg) + + expect_equal(length(L), 3) + expect_equal(L[[2]]x[1], 1.1) + expect_true(L[[2]]$y[1] <= 0) + expect_true(L[[2]]$y[2] >= 3.325) + expect_identical(L[[2]]$mode, "lines") + expect_identical(L[[2]]$line$shape, "linear") + expect_equal(L[[2]]$line$width, 3) + expect_identical(L[[2]]$line$color, "rgb(0,255,0)") + + save_outputs(gg, "vline") +}) + +test_that("vector yintercept results in multiple vertical lines", { + gg <- gg + geom_vline(xintercept=1:2, colour="blue", size=3) + + L <- gg2list(gg) + + expect_equal(length(L), 4) + expect_equal(L[[2]]x[1], 1) + expect_equal(L[[3]]x[1], 2) + expect_true(L[[3]]$y[1] <= 0) + expect_true(L[[3]]$y[2] >= 3.325) + expect_identical(L[[3]]$mode, "lines") + expect_identical(L[[3]]$line$shape, "linear") + expect_equal(L[[3]]$line$width, 3) + expect_identical(L[[3]]$line$color, "rgb(0,0,255)") + + save_outputs(gg, "vline-multiple") +}) From 3aed0c4e41b9a9e6d10521815b4c6d560641ff4d Mon Sep 17 00:00:00 2001 From: Marianne Corvellec Date: Wed, 22 Oct 2014 14:23:58 -0400 Subject: [PATCH 3/6] Fix typo and add test for multiple hlines --- tests/testthat/test-ggplot-hline.R | 38 ++++++++++++++++++++++-------- tests/testthat/test-ggplot-vline.R | 4 ++-- 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/tests/testthat/test-ggplot-hline.R b/tests/testthat/test-ggplot-hline.R index ff0f135868..8d9289107c 100644 --- a/tests/testthat/test-ggplot-hline.R +++ b/tests/testthat/test-ggplot-hline.R @@ -1,19 +1,18 @@ context("Hline") - # Horizontal line -test_that("Second trace be the hline", { - - x1 <- seq(from=0, to=3.5, by=0.5) - x2 <- x1 * 0.95 - df <- data.frame("x1"=x1, "x2"=x2) - - gg <- ggplot(df) + geom_point(aes(x=x1, y=x2)) + - geom_hline(yintercept=1.1, colour="green", size=3) +x1 <- seq(from=0, to=3.5, by=0.5) +x2 <- x1 * 0.95 +df <- data.frame("x1"=x1, "x2"=x2) +gg <- ggplot(df) + geom_point(aes(x=x1, y=x2)) + +test_that("second trace be the hline", { + gg <- gg + geom_hline(yintercept=1.1, colour="green", size=3) L <- gg2list(gg) expect_equal(length(L), 3) + expect_equal(L[[2]]$y[1], 1.1) expect_true(L[[2]]$x[1] <= 0) expect_true(L[[2]]$x[2] >= 3.5) expect_identical(L[[2]]$mode, "lines") @@ -22,4 +21,23 @@ test_that("Second trace be the hline", { expect_identical(L[[2]]$line$color, "rgb(0,255,0)") save_outputs(gg, "hline") -}) \ No newline at end of file +}) + +test_that("vector yintercept results in multiple horizontal lines", { + gg <- gg + geom_hline(yintercept=1:3, colour="red", size=3) + + L <- gg2list(gg) + + expect_equal(length(L), 5) + expect_equal(L[[2]]y[1], 1) + expect_equal(L[[3]]y[1], 2) + expect_equal(L[[4]]y[1], 3) + expect_true(L[[4]]$x[1] <= 0) + expect_true(L[[4]]$x[2] >= 3.325) + expect_identical(L[[3]]$mode, "lines") + expect_identical(L[[3]]$line$shape, "linear") + expect_equal(L[[3]]$line$width, 3) + expect_identical(L[[3]]$line$color, "rgb(255,0,0)") + + save_outputs(gg, "hline-multiple") +}) diff --git a/tests/testthat/test-ggplot-vline.R b/tests/testthat/test-ggplot-vline.R index e02305433c..35ac693b2a 100644 --- a/tests/testthat/test-ggplot-vline.R +++ b/tests/testthat/test-ggplot-vline.R @@ -12,7 +12,7 @@ test_that("second trace be the vline", { L <- gg2list(gg) expect_equal(length(L), 3) - expect_equal(L[[2]]x[1], 1.1) + expect_equal(L[[2]]$x[1], 1.1) expect_true(L[[2]]$y[1] <= 0) expect_true(L[[2]]$y[2] >= 3.325) expect_identical(L[[2]]$mode, "lines") @@ -23,7 +23,7 @@ test_that("second trace be the vline", { save_outputs(gg, "vline") }) -test_that("vector yintercept results in multiple vertical lines", { +test_that("vector xintercept results in multiple vertical lines", { gg <- gg + geom_vline(xintercept=1:2, colour="blue", size=3) L <- gg2list(gg) From c5548db8c2408e972c0795898fe2976f78889ea2 Mon Sep 17 00:00:00 2001 From: Marianne Corvellec Date: Wed, 22 Oct 2014 15:57:37 -0400 Subject: [PATCH 4/6] Fix typos --- tests/testthat/test-ggplot-hline.R | 6 +++--- tests/testthat/test-ggplot-vline.R | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/testthat/test-ggplot-hline.R b/tests/testthat/test-ggplot-hline.R index 8d9289107c..665162ad16 100644 --- a/tests/testthat/test-ggplot-hline.R +++ b/tests/testthat/test-ggplot-hline.R @@ -29,9 +29,9 @@ test_that("vector yintercept results in multiple horizontal lines", { L <- gg2list(gg) expect_equal(length(L), 5) - expect_equal(L[[2]]y[1], 1) - expect_equal(L[[3]]y[1], 2) - expect_equal(L[[4]]y[1], 3) + expect_equal(L[[2]]$y[1], 1) + expect_equal(L[[3]]$y[1], 2) + expect_equal(L[[4]]$y[1], 3) expect_true(L[[4]]$x[1] <= 0) expect_true(L[[4]]$x[2] >= 3.325) expect_identical(L[[3]]$mode, "lines") diff --git a/tests/testthat/test-ggplot-vline.R b/tests/testthat/test-ggplot-vline.R index 35ac693b2a..369e77cdf3 100644 --- a/tests/testthat/test-ggplot-vline.R +++ b/tests/testthat/test-ggplot-vline.R @@ -29,8 +29,8 @@ test_that("vector xintercept results in multiple vertical lines", { L <- gg2list(gg) expect_equal(length(L), 4) - expect_equal(L[[2]]x[1], 1) - expect_equal(L[[3]]x[1], 2) + expect_equal(L[[2]]$x[1], 1) + expect_equal(L[[3]]$x[1], 2) expect_true(L[[3]]$y[1] <= 0) expect_true(L[[3]]$y[2] >= 3.325) expect_identical(L[[3]]$mode, "lines") From 37b9f3b617d1c6b602f350a0fd7069c235a96b7c Mon Sep 17 00:00:00 2001 From: Marianne Corvellec Date: Wed, 22 Oct 2014 16:04:26 -0400 Subject: [PATCH 5/6] Update version number --- DESCRIPTION | 2 +- NEWS | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 6338c0b43d..03c0637d45 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: plotly Type: Package Title: Interactive, publication-quality graphs online. -Version: 0.5.3 +Version: 0.5.4 Authors@R: c(person("Chris", "Parmer", role = c("aut", "cre"), email = "chris@plot.ly"), person("Scott", "Chamberlain", role = "aut", diff --git a/NEWS b/NEWS index 7ec01fff83..7614179183 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,7 @@ +0.5.4 -- 22 October 2014. + +Support conversion of geom_vline(). + 0.5.3 -- 21 October 2014. Support conversion of geom_bar() with position_dodge(). From f2b4d969d9523644bb3941cb0f2fdccbb806cec3 Mon Sep 17 00:00:00 2001 From: Marianne Corvellec Date: Wed, 22 Oct 2014 17:20:50 -0400 Subject: [PATCH 6/6] Remove leftover (dev) comment --- R/trace_generation.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/trace_generation.R b/R/trace_generation.R index 6ded6789ed..93670b44d8 100644 --- a/R/trace_generation.R +++ b/R/trace_generation.R @@ -155,7 +155,7 @@ layer2traces <- function(l, d, misc) { # Split hline and vline when multiple if (g$geom == "hline" || g$geom == "vline") { if (nrow(g$data) > 1) { - df.list <- split(basic$data, rep(1:nrow(g$data))) #, drop=TRUE) + df.list <- split(basic$data, rep(1:nrow(g$data))) data.list <- lapply(df.list, function(df) { params <- basic$params list(data=df,