Difficulty understanding the ggplotly interface for custom geoms

164 views Asked by At

I've had some success extending ggflags to use SVGs, and now I'm trying to make this extension play nice with ggplotly(). The Plotly documentation has a nice section on translating custom geoms, and it essentially comes down to either providing a to_basic method for your geom or a geom2trace method.

I'm gonna be honest: I'm outta my depth here, and I'm trying to cobble something together based on the two named examples: ggmosaic and ggalt. But something simple like:

#' @name plotly_helpers
#' @title Plotly helpers
#' @description Helper functions to make it easier to automatically create plotly charts
#' @importFrom plotly to_basic
#' @export
to_basic.GeomFlag <- getFromNamespace("to_basic.GeomPoint", asNamespace("plotly"))

Gives me an error when running devtools::document():

object 'to_basic.GeomPoint' not found

if I change it to GeomLine the package documents and installs, but a simple plot displays like this:

df = data_frame(
    f = rep(1:5, each = 4),
    x = rep(1:4, times = 5),
    y = c(1:2, 2:1, 2:1, 1:2, 1:2, 2:1, 2:1, 1:2, 2:1, 1:2),
    s = c(1:4, 4:1, 1:4, 4:1, 1:4),
    c = rep(c('au', 'us', 'gb', 'de'), 5))

p1 = ggplot(df) + geom_flag(aes(x = x, y = y, size = s, frame = f, country = c)) + scale_country()
ggplotly(p1)

enter image description here

Obviously GeomLine isn't right—GeomPoint seems like a more natural point for this extension—but it could simply be that I can't reduce it and I need to implement geom2trace. But I don't really have a great idea of what needs to go in or out of either method. Can anyone help? Are there other examples of ggplot2 extensions that implement the Plotly interfaces?

EDIT: I've had a look at some of the built-in examples of geom2trace, but I'm not sure I can go much further here. I was kind of hoping I might be able to substitute custom grobs for the GeomPoint points, like with grid, but it seems like the Plotly API expects just basic point attributes.

0

There are 0 answers