如何在 R ggplot2 中创建看起来立方体(3D)的堆栈条形图?

Bio*_*eek 2 r bar-chart ggplot2 stacked-chart

我想要如下编码的堆栈图,但我希望它们看起来是立方体的(附后)。在 R 中甚至可能吗?

set.seed(123)
specie <- c(rep("sorgho" , 3) , rep("poacee" , 3) , rep("banana" , 3) , rep("triticum" , 3))
condition <- rep(c("normal" , "stress" , "Nitrogen", "T") , 3)
value <- abs(rnorm(12, 0 ,15))
data <- data.frame(specie,condition,value)
 
# Stacked
ggplot(data, aes(fill=condition, y=value, x=specie)) + 
    geom_bar(position="stack", stat="identity", alpha = 0.7, width = 0.3) + theme_classic()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

在此输入图像描述

All*_*ron 5

这可以通过未发布的ggrgl包等实现。将其应用到您的示例中,我们有:

remotes::install_github('coolbutuseless/devout')
remotes::install_github('coolbutuseless/devoutrgl')
remotes::install_github('coolbutuseless/ggrgl', ref='main')

library(rgl)
library(devout)
library(devoutrgl)
library(ggrgl)
library(ggplot2)

p <- ggplot(data, aes(fill=condition, y=value, x=specie, z =2,
                 extrude_face_fill = condition)) + 
    geom_bar_z(position="stack", stat="identity", width = 0.3, extrude = TRUE,
               color = "black") + 
  theme_classic()

devoutrgl::rgldev()
p
invisible(dev.off())
Run Code Online (Sandbox Code Playgroud)

结果是一个可旋转的 3D ggplot:

在此输入图像描述