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

标签 r ggplot2 bar-chart 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()

enter image description here

enter image description here

最佳答案

这可以通过未发布的 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())

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

enter image description here

关于r - 如何在 R ggplot2 中创建看起来立方体(3D)的堆栈条形图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72720874/

相关文章:

r - 将 n 个 ggplots 排列成下三角矩阵形状

r - 单个变量前面的波形符 (~) 是什么意思 (facet_wrap)?

python - python matplotlib 中带有日期时间的 barplot 的奇怪行为

javascript - 有没有办法从 Chart.js 的条形图中删除多余的空间和水平线?

r - 图均值和标准差

r - 防止 R 自动获取已安装软件包的最新版本?

R 函数没有返回值

r - ggplot2 y 轴刻度未显示在对数刻度上

r - 如何计算观察总数(n)并将其添加到ggplot(geom_point)

python - 为大量数据绘制条形图