Skip to content

Commit 9902be5

Browse files
author
Toby Dylan Hocking
committed
Merge pull request #178 from ropensci/toby-rect
Toby rect
2 parents 09f9dac + 10c8bcc commit 9902be5

File tree

6 files changed

+384
-116
lines changed

6 files changed

+384
-116
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: plotly
22
Type: Package
33
Title: Interactive, publication-quality graphs online.
4-
Version: 0.5.25
4+
Version: 0.5.26
55
Authors@R: c(person("Chris", "Parmer", role = c("aut", "cre"),
66
email = "[email protected]"),
77
person("Scott", "Chamberlain", role = "aut",

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
0.5.26 -- 18 Mar 2015
2+
3+
Implemented geom_rect #178
4+
15
0.5.25 -- 10 March 2015
26

37
Implemented geom_smooth() #183

R/ggplotly.R

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@ markLegends <-
5252
# list(point=c("colour", "fill", "shape", "size"),
5353
list(point=c("colour", "fill", "shape"),
5454
path=c("linetype", "size", "colour", "shape"),
55-
polygon=c("colour", "fill", "linetype", "size", "group"),
55+
## NOTE: typically "group" should not be present here, since
56+
## that would mean creating a separate plotly legend for each
57+
## group, even when they have the exact same visual
58+
## characteristics and could be drawn using just 1 trace!
59+
polygon=c("colour", "fill", "linetype", "size"),
5660
bar=c("colour", "fill"),
5761
errorbar=c("colour", "linetype"),
5862
errorbarh=c("colour", "linetype"),

R/trace_generation.R

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -322,14 +322,18 @@ toBasic <- list(
322322
})
323323
group2NA(g, "path")
324324
},
325-
polygon=function(g){
326-
if(is.null(g$params$fill)){
327-
g
328-
}else if(is.na(g$params$fill)){
329-
group2NA(g, "path")
330-
}else{
331-
g
332-
}
325+
rect=function(g){
326+
g$data$group <- 1:nrow(g$data)
327+
used <- c("xmin", "ymin", "xmax", "ymax")
328+
others <- g$data[!names(g$data) %in% used]
329+
g$data <- with(g$data, {
330+
rbind(cbind(x=xmin, y=ymin, others),
331+
cbind(x=xmin, y=ymax, others),
332+
cbind(x=xmax, y=ymax, others),
333+
cbind(x=xmax, y=ymin, others))
334+
})
335+
g$geom <- "polygon"
336+
g
333337
},
334338
path=function(g) {
335339
group2NA(g, "path")
@@ -411,7 +415,6 @@ toBasic <- list(
411415
}
412416
)
413417

414-
415418
#' Drawing ggplot2 geoms with a group aesthetic is most efficient in
416419
#' plotly when we convert groups of things that look the same to
417420
#' vectors with NA.
@@ -421,16 +424,41 @@ toBasic <- list(
421424
#' @return list of geom info.
422425
#' @author Toby Dylan Hocking
423426
group2NA <- function(g, geom) {
424-
poly.list <- split(g$data, g$data$group)
427+
poly.list <- split(g$data, g$data$group, drop=TRUE)
425428
is.group <- names(g$data) == "group"
426-
poly.na.df <- data.frame()
427-
for (i in seq_along(poly.list)) {
429+
poly.na.list <- list()
430+
forward.i <- seq_along(poly.list)
431+
## When group2NA is called on geom_polygon (or geom_rect, which is
432+
## treated as a basic polygon), we need to retrace the first points
433+
## of each group, see https://github.com/ropensci/plotly/pull/178
434+
retrace.first.points <- g$geom == "polygon"
435+
for (i in forward.i) {
428436
no.group <- poly.list[[i]][, !is.group, drop=FALSE]
429437
na.row <- no.group[1, ]
430438
na.row[, c("x", "y")] <- NA
431-
poly.na.df <- rbind(poly.na.df, no.group, na.row)
439+
retrace.first <- if(retrace.first.points){
440+
no.group[1,]
441+
}
442+
poly.na.list[[paste(i, "forward")]] <-
443+
rbind(no.group, retrace.first, na.row)
444+
}
445+
if(retrace.first.points){
446+
backward.i <- rev(forward.i[-1])[-1]
447+
for(i in backward.i){
448+
no.group <- poly.list[[i]][1, !is.group, drop=FALSE]
449+
na.row <- no.group[1, ]
450+
na.row[, c("x", "y")] <- NA
451+
poly.na.list[[paste(i, "backward")]] <- rbind(no.group, na.row)
452+
}
453+
if(length(poly.list) > 1){
454+
first.group <- poly.list[[1]][1, !is.group, drop=FALSE]
455+
poly.na.list[["last"]] <- rbind(first.group, first.group)
456+
}
457+
}
458+
g$data <- do.call(rbind, poly.na.list)
459+
if(is.na(g$data$x[nrow(g$data)])){
460+
g$data <- g$data[-nrow(g$data), ]
432461
}
433-
g$data <- poly.na.df
434462
g$geom <- geom
435463
g
436464
}
@@ -477,10 +505,12 @@ geom2trace <- list(
477505
line=paramORdefault(params, aes2line, line.defaults))
478506
},
479507
polygon=function(data, params){
480-
list(x=c(data$x, data$x[1]),
481-
y=c(data$y, data$y[1]),
508+
g <- list(data=data, geom="polygon")
509+
g <- group2NA(g, "polygon")
510+
list(x=g$data$x,
511+
y=g$data$y,
482512
name=params$name,
483-
text=data$text,
513+
text=g$data$text,
484514
type="scatter",
485515
mode="lines",
486516
line=paramORdefault(params, aes2line, polygon.line.defaults),

0 commit comments

Comments
 (0)