在 r 中使用 hist3D 的轴和图例问题

Axis and legend problems using hist3D in r

底部的进度更新。

我正在尝试使用 plot3D 库的 hist3D 函数获得漂亮的 3D 双变量直方图。我的绘图和颜色工作正常,但无法弄清楚如何正确操作边距和轴标签,并且图例也受到影响。

目前我正在摆脱原来的轴标签并添加一个 text3D(也来自 plot3D 库)来替换它们,但我不知道如何提供足够的 space 用于 x 和 y 标签。我已经解决了这个问题,现在将 x 标签放在图中,但理想情况下我希望它们在下面。不过,我还没有想出 y 轴的任何此类临时解决方法。我猜它是用 par() 修复的,但我想不通。

简单地说,这就是我希望帮助实现的目标:

  1. 将图例进一步向右移出绘图区域
  2. 增加 x-axis 和标题之间的 space 以适合标签
  3. 删除 x- 和 y-axis 的刻度线。

任何帮助将不胜感激。如果您认为使用不同的软件包可以得到我想要的东西,那也一样好。我认为我正在尝试复制的情节的作者使用了 Matplotlib,但我在 R 中需要它。

下面是我现在所在的位置、示例数据和代码。

谢谢!

library(plot3D)
library(viridis)

mydata <- matrix(sample(1:6,18,replace=TRUE), nrow=6)
rownames(mydata) <- c("biological regulation","biological_process",
                         "cellular aromatic compound metabolic process",
                         "cellular metabolic process",
                         "cellular nitrogen compound metabolic process",
                         "cellular process"    )
colnames(mydata) <- c("Group1","Group2","Group3")
mydata

m <- matrix(rep(seq(1:ncol(mydata)),each=nrow(mydata)), 
            nrow = nrow(mydata))

hist3D(z =mydata, scale = FALSE, expand = 0.1, bty = "g", 
       theta = 5, phi = 5, d = 2, r=20,
       border = "black", shade = 0.2, ltheta = 80, space = 0.8, 
       ticktype = "detailed", zmin=0, zlim=c(0,max(mydata)), 
       col= viridis(ncol(mydata)), colvar = m, colkey = F, 
       xlab = "GO Term", ylab = "", zlab = "Frequency", cex.axis = 1e-9)
#xlabs
text3D(x = seq(0,1,0.2), y = rep(-0.5, 6), z = rep(max(mydata)/2, 6),
       labels = rownames(mydata),
       add = TRUE, adj = 0.5,
       srt = 90, cex = 0.5)
#ylabs
text3D(x = rep(1.4, 3),   y = seq(-.2,0.8,0.5), z = rep(0, 3),
       labels  = colnames(mydata),
       add = TRUE, adj = 1)
#zlabs
text3D(x = rep(-0.18, 7),   y = rep(0,7), z = seq(-0.1, 5.9,1),
       labels  = seq(0,max(mydata)),
       add = TRUE, adj = 1)

legend("topright",
       legend = as.factor(colnames(mydata)), # category labels
       col = viridis(ncol(mydata)),  # color key
       fill = viridis(ncol(mydata)))

进度更新

我使用 par(xpd) 对文本剪辑进行排序,使用 par(mar) 对大小进行排序,并使用 inset 对图例进行排序。仍然找不到如何去除蜱虫。我认为这与 persp().

有关

library(plot3D)
library(viridis)

mydata <- matrix(sample(1:6,18,replace=TRUE), nrow=6)
rownames(mydata) <- gsub("( \S+) ", "\1\n", 
                         c("biological regulation","biological process",
                         "cellular aromatic compound metabolic process",
                         "cellular metabolic process",
                         "cellular nitrogen compound metabolic process",
                         "cellular process"))
colnames(mydata) <- c("Group1","Group2","Group3")
mydata

m <- matrix(rep(seq(1:ncol(mydata)),each=nrow(mydata)), 
            nrow = nrow(mydata))

opar <- par(xpd=TRUE, mar=c(7,4,4,4))
hist3D(z =mydata, scale = FALSE, expand = 0.1, bty = "g", 
       theta = 5, phi = 5, d = 2, r=20,
       border = "black", shade = 0.2, ltheta = 80, space = 0.8, 
       ticktype = "detailed", zmin=0, zlim=c(0,max(mydata)), 
       col= viridis(ncol(mydata)), colvar = m, colkey = F, 
       xlab = "", ylab = "", zlab = "Frequency", cex.axis = 1e-9)
#xlabs
text3D(x = seq(0,1,0.2), y = rep(-0.5, 6), z = rep(-0.5, 6),
       labels = rownames(mydata),
       add = TRUE, adj = 1,
       srt = 90, cex = 0.75)
#ylabs
text3D(x = rep(1.4, 3),   y = seq(-.2,0.8,0.5), z = rep(0, 3),
       labels  = colnames(mydata),
       add = TRUE, adj = 1)
#zlabs
text3D(x = rep(-0.18, 7),   y = rep(0,7), z = seq(-0.1, 5.9,1),
       labels  = seq(0,max(mydata)),
       add = TRUE, adj = 1)

legend("topright",
       legend = as.factor(colnames(mydata)), # category labels
       col = viridis(ncol(mydata)),  # color key
       fill = viridis(ncol(mydata)), 
       inset=c(-0.1,0))
par(opar)

所以我发现我可以在 hist3d 调用中使用 axes = F 关闭所有轴,这对我来说已经足够了。然后我可以随意添加轴标题。

library(plot3D)
library(viridis)

mydata <- matrix(sample(1:6,18,replace=TRUE), nrow=6)
rownames(mydata) <- gsub("( \S+) ", "\1\n", 
                         c("biological regulation","biological process",
                           "cellular aromatic compound metabolic process",
                           "cellular metabolic process",
                           "cellular nitrogen compound metabolic process",
                           "cellular process"))
colnames(mydata) <- c("Group1","Group2","Group3")
mydata

m <- matrix(rep(seq(1:ncol(mydata)),each=nrow(mydata)), 
            nrow = nrow(mydata))

opar <- par(xpd=TRUE, mar=c(7,4,4,4))
hist3D(z =mydata, scale = FALSE, expand = 0.1, bty = "g", 
       theta = 5, phi = 5, d = 2, r=20,
       border = "black", shade = 0.2, ltheta = 80, space = 0.8, 
       ticktype = "detailed", zmin=0, zlim=c(0,max(mydata)), 
       col= viridis(ncol(mydata)), colvar = m, colkey = F, axes = F)
#xlabs
text3D(x = seq(0,1,0.2), y = rep(-0.5, 6), z = rep(-0.5, 6),
       labels = rownames(mydata),
       add = TRUE, adj = 1,
       srt = 90, cex = 0.75)
#zlabs
text3D(x = rep(-0.18, 7),   y = rep(0,7), z = seq(-0.1, 5.9,1),
       labels  = seq(0,max(mydata)),
       add = TRUE, adj = 1)
#z title
text3D(x = -0.6,   y = max(mydata)/2 , z = 0,
       labels  = "Frequency",
       add = TRUE, adj = 0.5, srt = 90)

legend("topright",
       legend = as.factor(colnames(mydata)), # category labels
       col = viridis(ncol(mydata)),  # color key
       fill = viridis(ncol(mydata)), 
       inset=c(-0.1,0))
par(opar)