@@ -73,6 +73,13 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all", source = "A
7373 scale_x <- function () scales $ get_scales(" x" )
7474 scale_y <- function () scales $ get_scales(" y" )
7575 panel <- ggfun(" train_position" )(panel , data , scale_x(), scale_y())
76+ # Before mapping x/y position, save the domain (for discrete scales)
77+ # to display in tooltip.
78+ data <- lapply(data , function (d ) {
79+ if (! is.null(scale_x()) && scale_x()$ is_discrete()) d $ x_plotlyDomain <- d $ x
80+ if (! is.null(scale_y()) && scale_y()$ is_discrete()) d $ y_plotlyDomain <- d $ y
81+ d
82+ })
7683 data <- ggfun(" map_position" )(panel , data , scale_x(), scale_y())
7784 # for some geoms (e.g. boxplots) plotly.js needs the "pre-statistics" data
7885 prestats_data <- data
@@ -187,21 +194,18 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all", source = "A
187194 # stat specific mappings
188195 grep(" ^\\ .\\ ." , as.character(x $ stat $ default_aes ), value = TRUE )
189196 )
190- # remove leading/trailing dots in "hidden" stat aes
191- map <- sub(" ^\\ .\\ ." , " " , sub(" \\ .\\ .$" , " " , map ))
197+ # "hidden" names should be taken verbatim
198+ idx <- grepl(" ^\\ .\\ ." , map ) & grepl(" \\ .\\ .$" , map )
199+ hiddenMap <- sub(" ^\\ .\\ ." , " " , sub(" \\ .\\ .$" , " " , map ))
200+ map [idx ] <- hiddenMap [idx ]
201+ names(map )[idx ] <- hiddenMap [idx ]
192202 if (! identical(tooltip , " all" )) {
193203 map <- map [tooltip ]
194204 }
195- # tooltips for discrete positional scales are misleading
196- if (scales $ get_scales(" x" )$ is_discrete()) {
197- map <- map [! names(map ) %in% " x" ]
198- }
199- if (scales $ get_scales(" y" )$ is_discrete()) {
200- map <- map [! names(map ) %in% " y" ]
201- }
202205 map
203206 })
204207
208+
205209 # attach a new column (hovertext) to each layer of data that should get mapped
206210 # to the text trace property
207211 data <- Map(function (x , y ) {
@@ -234,10 +238,11 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all", source = "A
234238 x
235239 }, data , aesMap )
236240
241+
242+
237243 # layers -> plotly.js traces
238- traces <- layers2traces(
239- data , prestats_data , layers , panel $ layout , scales , p $ labels
240- )
244+ traces <- layers2traces(data , prestats_data , panel $ layout , p )
245+
241246 # default to just the text in hover info, mainly because of this
242247 # https://github.com/plotly/plotly.js/issues/320
243248 traces <- lapply(traces , function (tr ) {
0 commit comments