Unexpected output using plotly and geom_tile

2019-08-13 10:14发布

问题:

I'm trying to print a heatmap using ggplot2's geom_tile and display it using the package plotly, but I get some unexpected behavior.

There is a reproducible example in plotly's website https://plot.ly/ggplot2/geom_tile/

To make it easier, I paste the code here:

library(plotly)
library(reshape2)

p <- volcano %>%
  melt() %>% 
  ggplot(aes(Var1, Var2, fill = value)) + geom_tile()

ggplotly(p)

According to the website, I'm supposed to get something like this:

But the result I get is the next one:

The funny thing is that if I print the ggplot object p with print(p) I get the outcome I was supposed to get:

which makes me think that the problem is plotly and not ggplot.

I've run the code from other examples using ggplot and plotly and it all works perfectly, the issue seems to be with geom_tile.

My SessionInfo() is:

R version 3.2.4 Revised (2016-03-16 r70336)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 15.10

And the versions of ggplot an plotly are:

  • ggplot2: 2.1.0
  • plotly: 3.4.1

I also tested this code with a Mac and got the same unexpected results.

回答1:

So, I found three possible solutions for this:

  1. As Laterow said, downgrading to the 2.0.16 version works.
  2. The people from plotly told me that installing the development version 3.4.13 with devtools::install_github('ropensci/plotly') should fix it. I tried it and it does work.
  3. Transposing the original data and changing the order of variables in the aes in geom_tile also works.

The code for the third solution would be:

p <- t(volcano) %>%
melt() %>% 
ggplot(aes(x=Var2, y=Var1)) + geom_tile(aes(fill=value))
ggplotly(p)