保留ggplotly()

时间:2017-07-02 17:27:51

标签: r ggplot2 plotly

我正在尝试创建一个包含图例和具有相同宽高比的“方形”形状的图。我能够使用ggplot2()在下面的代码中的“p”对象中实现这一点。然而,当我在“p”对象上运行ggplotly()时,图例消失了,同样宽高比的“方形”形状也消失了。下面,我展示两个显示差异的图像。左图显示带有图例的“p”对象和相等的纵横比。红色的x = y线与图像的左下角和右上角完美相交。右图显示了ggplotly(p)输出,其中图例消失了,方形宽高比也消失了。 x = y线不再与图像的左下角和右上角完美相交。

Comparison of the two plots

我的MWE代码包含在下面:

library(hexbin)
library(ggplot2)
library(plotly)
set.seed(1)
dat <- 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 <- which(sapply(colnames(dat), function(x) unlist(strsplit(x,"[.]"))[1]) %in% c("A", "C"))
datSel <- dat[,c(1, sampleIndex)]

sampleIndex1 <- which(sapply(colnames(datSel), function(x) unlist(strsplit(x,"[.]"))[1]) %in% c("A"))
sampleIndex2 <- 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 <- c()
y <- c()
for (i in 1:length(sampleIndex1)){
  for (j in 1:length(sampleIndex2)){
    x <- c(x, unlist(datSel[,(sampleIndex1[i])]))
    y <- c(y, unlist(datSel[,(sampleIndex2[j])]))
  }
}

h <- hexbin(x=x, y=y, xbins=xbins, shape=1, IDs=TRUE, xbnds=maxRange, ybnds=maxRange)
hexdf <- data.frame (hcell2xy (h),  hexID = h@cell, counts = h@count)
attr(hexdf, "cID") <- h@cID

my_breaks = c(2, 4, 6, 8, 20, 1000)
p <- 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 <- p + scale_fill_gradient(name = "count", trans = "log", breaks = my_breaks, labels = my_breaks, guide="legend")

ggplotly(p)
ggplotly(p) %>% layout(height = 200, width = 200)
ggplotly(p, height=400, width=400)

如您所见,我尝试了几种不同的方法来创建ggplotly(p)输出。我收到如下警告:

 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() 

但是,我不确定如何解决此警告和问题。任何建议将不胜感激!

1 个答案:

答案 0 :(得分:4)

这是一个部分解决方案,它修复了x = y线和方形宽高比,但对图例问题使用了一些变通方法。

纵横比问题很简单,在最新版本中进行了图表更改,现在height =width =进入ggplotly(),而不是layout(),就像之前的版本一样。不幸的是,一些在线文档似乎仍然指定了旧的格式。

我无法将您的自定义图例和比例显示在图中,并且与ggplot图例的不兼容似乎是某些类型的ggplots的文档错误。我能想到的最佳解决方案是在数据框中创建一个日志计数列,然后绘制日志计数,以便默认图例显示您想要的颜色和比例。

# add a column for log count so default scale/legend can be used
hexdf$log_counts <- log(hexdf$counts)

p <- ggplot(hexdf, aes(x = x, y = y)) +
  geom_hex(stat="identity", aes(fill = log_counts)) + # log counts, not  counts
  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 

# set width > height to allow room for legend
# plot looks close to 1:1 to me, but may need to adjust width slightly
ggplotly(p, height = 400, width = 500) 

哪个产生

enter image description here