Keep aspect ratio when using ggplotly

て烟熏妆下的殇ゞ 提交于 2019-12-07 10:38:46

问题


I have a ggplot hexbin chart that I want to show with ggplotly. However, when I use the ggplotly function the aspect ratio, previously fixed for the gplot chart, is lost.

Minimal working example:

library(ggplot2)
library(plotly)
library(RColorBrewer)

set.seed(11)
df<-data.frame(xvar=rnorm(1000,0,4),yvar=rnorm(1000))

#nicer colours
palette <- colorRampPalette(rev(brewer.pal(11,'Spectral')))
colours<-palette(32)

p1<-ggplot(df,aes(x=xvar,y=yvar))+ coord_fixed(ratio=1)+
  stat_binhex(bins=50)+scale_fill_gradientn(colours=colours)
p1
ggplotly(p1)

I have approximated the ratio with the following code but the ratio is still slightly wrong because of the legend.

ratioaxes<-(max(df$yvar)-min(df$yvar))/(max(df$xvar)-min(df$xvar))
ggplotly(p1,height=ratioaxes*500,width=500)

Is there any way that I can preserve this ratio?

Thanks in advance!

来源:https://stackoverflow.com/questions/39767054/keep-aspect-ratio-when-using-ggplotly

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!