Search code examples
rggplot2plotlyr-plotly

R geom_tile width and height are ignored in ggplotly


The height and width parameters of geom_tile are not being taken into account when converted to ggplotly, as shown in the example below. Is there a work around? Please note that my dataset is in fact very large (+80k data points), so I would favor an efficient solution, if there is one.

library(plotly)

X <- data.frame(x = c(1, 2, 1, 2), y = c(1, 1, 2, 2), z = 1:4)
gg <- ggplot(X, aes(x, y)) + geom_tile(aes(fill = z), width = .5, height = .5)

gg
ggplotly(gg)

enter image description here


Solution

  • This seems to be a bug in ggplotly. The easiest thing would be to convert your geom_tile to geom_rect. I don't think this will be any less efficient than geom_tile, since you are just replicating what geom_tile does internally.

    library(plotly)
    
    X <- data.frame(x = c(1, 2, 1, 2), y = c(1, 1, 2, 2), z = 1:4)
    
    width <- .5
    height <- .5
    
    gg <- ggplot(X, aes(x, y)) + 
      geom_rect(aes(xmin = x - width/2, xmax = x + width/2, 
                    ymin = y - height/2, ymax = y + height/2, fill = z))
    
    ggplotly(gg)
    

    enter image description here