Skip to content

ggplotly design, internals, technical stuff

Carson edited this page Feb 16, 2015 · 7 revisions

converting the group aesthetic

In ggplot2 the group aesthetic is used to designate separate lines, polygons, paths, ribbons (but not points) that all look the same. For example in the canada cities viz the borders are a geom_polygon with aes(group).

> borders(regions="canada", name="borders")
mapping: x = long, y = lat, group = group 
geom_polygon: colour = grey50, fill = NA 
stat_identity: name = borders 
position_identity: (width = NULL, height = NULL)

It is important that this is translated as 1 trace with NAs (not a separate trace for each lake/island), because

  • it is much more efficient (the plotly web site is slow when there are many traces to render).
  • each trace gets its own legend entry and it doesn't make sense to have separate legend entries for each one of the lakes/islands/etc.

A test for this functionality is here

legends

ggplot2 legends are more sophisticated than plotly legends, so it is impossible to translate everything. In particular there may be several legends in ggplot2 (color, linetype, etc) but only 1 in plotly. Plotly takes the legend labels from the names of the traces.

themes

theme stuff you are correct that there are many things not supported. It is more because I first wanted to focus on implementing data-driven elements like geoms. We would like the plotly version to look as similar as possible to the ggplot, and I encourage you to contribute if you notice any differences, but please be aware that there are some ggplot features again that plotly does not support. One example is minor gridlines.

ggplotly development function

copy/paste the following code into your ~/.Rprofile

library(plotly)
Plotly <- plotly("YOUR_USERNAME", "YOUR_KEY")
ggplotly <- function(gg, p=Plotly){
  if(!is.ggplot(gg)){
    stop("gg must be a ggplot")
  }
  if(!is.function(p$plotly)){
    stop("p must be a plotly interface object")
  }
  pargs <- gg2list(gg)
  resp <- do.call(p$plotly, pargs)
  browseURL(resp$url)
  invisible(list(data=pargs, response=resp))
}

Ordinarily package users will do Plotly$ggplotly() to send their last ggplot to plotly, but for development I find that it is easier to try new code by first editing plotly/R/ggplotly.R, then doing source("plotly/R/ggplotly.R"), then ggplotly(g) with some ggplot g. Otherwise you have the additional step of re-making the Plotly <- plotly(...) object every time you edit the code.

Clone this wiki locally