Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 在ggplotly()中保留图例和方形纵横比_R_Ggplot2_Plotly - Fatal编程技术网

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

R 在ggplotly()中保留图例和方形纵横比,r,ggplot2,plotly,R,Ggplot2,Plotly,我正在尝试创建一个包含图例和具有相等纵横比的“方形”形状的绘图。我能够使用ggplot2()在下面代码中的“p”对象中实现这一点。但是,当我在“p”对象上运行ggplotly()时,图例消失了,长宽比相等的“正方形”形状也消失了。下面,我展示了两张显示差异的图片。左图显示了带有图例和相等纵横比的“p”对象。红色的x=y线与图像的左下角和右上角完美相交。右图显示了ggplotly(p)输出,其中图例消失,方形纵横比也消失。x=y线不再与图像的左下角和右上角完美相交 我的MWE代码包括在下面: l

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

我的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)

然而,我不确定如何解决这一警告和问题。如有任何建议,将不胜感激

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

宽高比问题很简单,在最新版本中进行了绘图更改,因此现在
高度=
宽度=
进入
ggplotly()
,而不是像以前的版本那样进入
layout()
。不幸的是,一些在线文档似乎仍然指定了旧的格式

我无法让您的自定义图例和比例在plotly中显示,对于某些类型的ggplot,与ggplot图例不兼容似乎是一个记录在案的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) 
#为日志计数添加一列,以便可以使用默认比例/图例

hexdf$log\u counts我删除了代码开头的行
rm(list=ls())
。如果有人无意中将其粘贴到Acritive R会话中,他们将丢失所有现有对象您使用的库是什么?请添加到MWE<未找到代码>hexbin
hcell2xy
# 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)