gpt4 book ai didi

在 ggplotly() 中保留图例和方形纵横比

转载 作者:行者123 更新时间:2023-12-04 09:48:54 25 4
gpt4 key购买 nike

我正在尝试创建一个包含图例和具有相等纵横比的“正方形”形状的图。我能够使用 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()

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

最佳答案

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

纵横比问题很简单,在最新版本中发生了很大的变化,现在height =width =进去 ggplotly() ,不是 layout()和以前的版本一样。不幸的是,一些在线文档似乎仍然指定了旧格式。

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

# 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

关于在 ggplotly() 中保留图例和方形纵横比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44873421/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com