【问题标题】:Axis and legend problems using hist3D in r在 r 中使用 hist3D 的轴和图例问题
【发布时间】:2021-06-16 12:59:13
【问题描述】:

底部有进度更新。

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

目前我正在摆脱原始轴标签并添加 text3D(也来自 plot3D 库)来替换它们,但我不知道如何为 x 和 y 标签提供足够的空间.我已经解决了这个问题,现在将 x 标签放在情节中,但理想情况下我希望它们在下面。不过,我还没有想出任何关于 y 轴的临时工作。我猜这是 par() 修复的问题,但我无法弄清楚。

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

  1. 将图例移到绘图区域之外
  2. 增加 x 轴和标题之间的空间以适应标签
  3. 从 x 轴和 y 轴上移除刻度线。

任何帮助将不胜感激。如果您认为您可以使用不同的软件包获得我想要的东西,那也一样好。我认为我试图复制的情节的作者使用了 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)

【问题讨论】:

    标签: r plot legend margins par


    【解决方案1】:

    所以我发现我可以在 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-13
      • 1970-01-01
      • 2021-05-30
      • 2018-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多