@@ -26,7 +26,8 @@ plotly_build.list <- function(p) {
2626
2727# ' @export
2828plotly_build.gg <- function (p ) {
29- ggplotly(p )
29+ p <- ggplotly(p )
30+ supply_defaults(p )
3031}
3132
3233# ' @export
@@ -71,6 +72,24 @@ plotly_build.plotly <- function(p) {
7172 p $ x $ attrs [[1 ]] <- NULL
7273 }
7374 }
75+
76+ # trace type checking and renaming for plot objects
77+ if (is_mapbox(p ) || is_geo(p )) {
78+ p $ x $ attrs <- lapply(p $ x $ attrs , function (tr ) {
79+ tr [[" x" ]] <- tr [[" x" ]] %|| % tr [[" lat" ]]
80+ tr [[" y" ]] <- tr [[" y" ]] %|| % tr [[" lon" ]]
81+ if (! grepl(" scatter|choropleth" , tr [[" type" ]] %|| % " scatter" )) {
82+ stop(" Cant add a '" , tr [[" type" ]], " ' trace to a map object" , call. = FALSE )
83+ }
84+ if (is_mapbox(p )) {
85+ tr [[" type" ]] <- " scattermapbox"
86+ }
87+ if (is_geo(p )) {
88+ tr [[" type" ]] <- if (! is.null(tr [[" z" ]])) " choropleth" else " scattergeo"
89+ }
90+ tr
91+ })
92+ }
7493
7594 dats <- Map(function (x , y ) {
7695
@@ -270,16 +289,18 @@ plotly_build.plotly <- function(p) {
270289 }
271290 }
272291
273- # attribute naming correction for "geo-like" traces
274- if (is_geo(p ) || is_mapbox(p )) {
275- p $ x $ layout [grepl(" ^[x-y]axis" , names(p $ x $ layout ))] <- NULL
276- p $ x $ data <- lapply(p $ x $ data , function (tr ) {
292+ # supply trace anchor and domain information
293+ p <- supply_defaults(p )
294+
295+ # attribute naming corrections for "geo-like" traces
296+ p $ x $ data <- lapply(p $ x $ data , function (tr ) {
297+ if (isTRUE(tr [[" type" ]] %in% c(" scattermapbox" , " scattergeo" ))) {
277298 tr [[" lat" ]] <- tr [[" lat" ]] %|| % tr [[" y" ]]
278299 tr [[" lon" ]] <- tr [[" lon" ]] %|| % tr [[" x" ]]
279300 tr [c(" x" , " y" )] <- NULL
280- tr
281- })
282- }
301+ }
302+ tr
303+ })
283304
284305 # polar charts don't like null width/height keys
285306 if (is.null(p $ x $ layout [[" height" ]])) p $ x $ layout [[" height" ]] <- NULL
0 commit comments