title: Retain legend and square aspect ratio in ggplotly()
body: <p>I am trying to create a plot that contains a legend and a "square" shape with equal aspect ratio. I was able to achieve this in the "p" object in the code below using ggplot2(). However, when I ran ggplotly() on the "p" object, the legend disappeared and the "square" shape with equal aspect ratio also disappeared. Below, I show two images showing the difference. The left image shows the "p" object with the legend and an equal aspect ratio. The x=y line in red perfectly intersects the bottom-left and top-right corners of the image. The right image shows the ggplotly(p) output where the legend is gone and the square aspect ratio is also gone. The x=y line no longer perfectly intersects the bottom-left and top-right corners of the image.</p>
  
  <p><a href="https://i.stack.imgur.com/oPwQs.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/oPwQs.png" alt="Comparison of the two plots"></a></p>
  
  <p>My MWE code is included below:</p>
  
  <pre><code>library(hexbin)
  library(ggplot2)
  library(plotly)
  set.seed(1)
  dat &lt;- data.frame(ID = paste0("ID", 1:1010), A.1 = c(rep(0.5, 1000), abs(rnorm(10))), A.2 = c(rep(0.5, 1000), abs(rnorm(10))), B.1 = c(rep(0.5, 1000), abs(rnorm(10))), B.2 = c(rep(0.5, 1000), abs(rnorm(10))), C.1 = c(rep(0.5, 1000), abs(rnorm(10))), C.2 = c(rep(0.5, 1000), abs(rnorm(10))), C.3 = c(rep(0.5, 1000), abs(rnorm(10))), stringsAsFactors = FALSE
  )
  
  sampleIndex &lt;- which(sapply(colnames(dat), function(x) unlist(strsplit(x,"[.]"))[1]) %in% c("A", "C"))
  datSel &lt;- dat[,c(1, sampleIndex)]
  
  sampleIndex1 &lt;- which(sapply(colnames(datSel), function(x) unlist(strsplit(x,"[.]"))[1]) %in% c("A"))
  sampleIndex2 &lt;- which(sapply(colnames(datSel), function(x) unlist(strsplit(x,"[.]"))[1]) %in% c("C"))
  minVal = min(datSel[,-1])
  maxVal = max(datSel[,-1])
  maxRange = c(minVal, maxVal)
  xbins= 10
  buffer = (maxRange[2]-maxRange[1])/(xbins/2)
  x &lt;- c()
  y &lt;- c()
  for (i in 1:length(sampleIndex1)){
    for (j in 1:length(sampleIndex2)){
      x &lt;- c(x, unlist(datSel[,(sampleIndex1[i])]))
      y &lt;- c(y, unlist(datSel[,(sampleIndex2[j])]))
    }
  }
  
  h &lt;- hexbin(x=x, y=y, xbins=xbins, shape=1, IDs=TRUE, xbnds=maxRange, ybnds=maxRange)
  hexdf &lt;- data.frame (hcell2xy (h),  hexID = h@cell, counts = h@count)
  attr(hexdf, "cID") &lt;- h@cID
  
  my_breaks = c(2, 4, 6, 8, 20, 1000)
  p &lt;- ggplot(hexdf, aes(x=x, y=y, fill = counts, hexID=hexID)) + geom_hex(stat="identity") + geom_abline(intercept = 0, color = "red", size = 0.25) + labs(x = "A", y = "C") + coord_fixed(xlim = c(-0.5, (maxRange[2]+buffer)), ylim = c(-0.5, (maxRange[2]+buffer))) + theme(aspect.ratio=1)
  p &lt;- p + scale_fill_gradient(name = "count", trans = "log", breaks = my_breaks, labels = my_breaks, guide="legend")
  
  ggplotly(p)
  ggplotly(p) %&gt;% layout(height = 200, width = 200)
  ggplotly(p, height=400, width=400)
  </code></pre>
  
  <p>As you can see, I tried a few different approaches to creating the ggplotly(p) output. I received warnings as follows:</p>
  
  <pre><code> Warning messages:
  1: Aspect ratios aren't yet implemented, but you can manually set a suitable height/width 
  2: Aspect ratios aren't yet implemented, but you can manually set a suitable height/width 
  3: Specifying width/height in layout() is now deprecated.
  Please specify in ggplotly() or plot_ly() 
  </code></pre>
  
  <p>However, I am uncertain how to resolve this warning and the problem. Any suggestions would be greatly appreciated!</p>