@@ -759,12 +759,7 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all",
759759 l <- rm_asis(l )
760760
761761 # start build a plotly object with meta information about the ggplot
762- ids <- lapply(seq_along(data ), function (x ) new_id())
763- l $ cur_data <- ids [[layerData ]]
764- l $ visdat <- if (originalData ) lapply(layer_data , function (x ) function (y ) x ) else lapply(data , function (x ) function (y ) x )
765- l $ visdat <- setNames(l $ visdat , ids )
766-
767- # translate layer mappings -> plotly attrs
762+ # first, translate layer mappings -> plotly attrs
768763 mappingFormulas <- lapply(layers , function (x ) {
769764 mappings <- c(x $ mapping , if (isTRUE(x $ inherit.aes )) plot $ mapping )
770765 if (originalData ) {
@@ -774,16 +769,27 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all",
774769 setNames(lapply(nms , function (x ) lazyeval :: f_new(as.symbol(x ))), nms )
775770 }
776771 })
777- # if (!is.null(mappingFormulas[["group"]])) {
778- # dat <- dplyr::group_by_(dat, mappingFormulas[["group"]])
779- # }
772+
773+ return_dat <- if (originalData ) layer_data else data
774+
775+ # translate group aesthetics to data attributes
776+ return_dat <- Map(function (x , y ) {
777+ if (is.null(y [[" group" ]])) return (x )
778+ dplyr :: group_by_(x , y [[" group" ]])
779+ }, return_dat , mappingFormulas )
780+
780781 # don't need to add group as an attribute anymore
781- # mappingFormulas <- mappingFormulas[!grepl("^group$", names(mappingFormulas))]
782+ mappingFormulas <- lapply(mappingFormulas , function (x ) x [! grepl(" ^group$" , names(x ))])
783+
784+ ids <- lapply(seq_along(data ), function (x ) new_id())
782785 l $ attrs <- setNames(mappingFormulas , ids )
783786 l $ attrs <- lapply(l $ attrs , function (x ) structure(x , class = " plotly_eval" ))
784- # the build step remove the first attrs if no type exists
787+ # the build step removes the first attrs if no type exists
785788 l $ attrs [[1 ]][[" type" ]] <- " ggplotly"
786789
790+ l $ cur_data <- ids [[layerData ]]
791+ l $ visdat <- setNames(lapply(return_dat , function (x ) function (y ) x ), ids )
792+
787793 l
788794}
789795
0 commit comments