r - 如何使用 R(最好是 ggplot2)生成堆叠平面或重叠钻石的图形?

标签 r ggplot2 plot visualization

在看提升自己的时候,我在看真的很优秀的ggplot2 workshop通过从根本上了解它的工作原理,让自己更好地使用该软件包。

作为该研讨会的一部分,我对研讨会中使用的一种可视化效果感到震惊,因为它对于解释依赖关系的分层层次结构特别有用,我正在寻找如何生成这样的图片(理想情况下)使用 R)。

这两张图片显示了我试图重现的可视化的两个部分: 带有标签的堆叠平面: Stacked Planes

堆叠的平面,大部分都带有透明胶片和标签(适当突出显示): Stacked Planes with transparency

我已经能够使用 rgl 制作类似的东西,但它几乎没有那么好。鉴于我正在尝试提高自己在 ggplot2 中的技能,我希望能够使用 ggplot2(或其中一个扩展)来生成它,因为这将使我能够更轻松地控制图形的一些“细节”)。

是否可以使用 ggplot2 或扩展包?

在rgl中产生它的代码是:

library(rgl)
# Create some dummy data
dat <- replicate(2, 1:3)

# Initialize the scene, no data plotted
# hardcoded user matrix of a particular view (so I can go straight to that view each time)
userMatrix_orig <- matrix(c(-0.7069399, -0.2729415, 0.6524867, 0.0000000, 0.7072651, -0.2773000, 0.6502926, 0.0000000, 0.003442926, 0.921199083, 0.389076293, 0.000000000, 0, 0, 0, 1), nrow = 4 )

plot3d(dat, type = 'n', xlim = c(-1, 1), ylim = c(-1, 1), zlim = c(-10, 10), 
       xlab = '', ylab = '', zlab = '', axes=FALSE) 
view3d(userMatrix=userMatrix_orig)
material3d(alpha=1.0)
# Add planes
planes3d(1, 1, 1, -2, col = 'paleturquoise', alpha = 0.8, name="hello")
planes3d(1, 1, 1, -4, col = 'palegreen', alpha = 0.8)
planes3d(1, 1, 1, -6, col = 'palevioletred', alpha = 0.8)
planes3d(1, 1, 1, -8, col = 'midnightblue', alpha = 0.8)
planes3d(1, 1, 1, 0, col = 'red', alpha = 0.8)
planes3d(1, 1, 1, 2, col = 'green', alpha = 0.8)
planes3d(1, 1, 1, 4, col = 'orange', alpha = 0.8)
planes3d(1, 1, 1, 6, col = 'blue', alpha = 0.8)

# Label the planes
family_val <- c("sans")
adj_val <- 1
cex_val <- 2.5
text3d(x=1, y =-1, z = -6, texts="data", adj = adj_val, family = family_val, cex = cex_val )
text3d(x=1, y =-1, z = -4, texts="mapping", adj = adj_val, family = family_val, cex = cex_val )
text3d(x=1, y =-1, z = -2, texts="statistics", adj = adj_val, family = family_val, cex = cex_val )
text3d(x=1, y =-1, z = 0, texts="scales", adj = adj_val, family = family_val, cex = cex_val )
text3d(x=1, y =-1, z = 2, texts="geometries", adj = adj_val, family = family_val, cex = cex_val )
text3d(x=1, y =-1, z = 4, texts="facets", adj = adj_val, family = family_val, cex = cex_val )
text3d(x=1, y =-1, z = 6, texts="coordinates", adj = adj_val, family = family_val, cex = cex_val )
text3d(x=1, y =-1, z = 8, texts="theme", adj = adj_val, family = family_val, cex = cex_val )

我使用它制作的图形是: rgl stacked planes

最佳答案

我会用这样的函数在 ggplot 中重新创建图像:

make_graphic <- function(highlight = 1:8) {
  
  library(ggplot2)
  alpha_vals <- c(0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2)
  
  alpha_vals[highlight] <- 1
  
  df <- data.frame(x = rep(c(0.5, 0.75, 1, 0.75, 0.5), 8),
                   y = rep(c(0.5, 0, 0.5, 1, 0.5), 8) + rep(0:7, each = 5)/2,
                   z = rep(LETTERS[1:8], each = 5))
  
  ggplot(df, aes(x, y)) +
    geom_polygon(aes(fill = z, alpha = z)) +
    geom_text(data = data.frame(x = 0.48, y = rev(0.5 + (0:7)/2),
                                z = rev(LETTERS[1:8]),
                                a = c("THEME", "COORDINATES", "FACETS",
                                      "GEOMETRIES", "SCALES", "STATISTICS", 
                                      "MAPPING", "DATA")), fontface = 2,
              family = "opencondensed",
              aes(label = a, alpha = z), colour = "white", size = 10, hjust = 1) +
    scale_x_continuous(limits = c(0.2, 1)) +
    scale_fill_manual(values = c("#a6aaa9", "#ef4e47", "#34a5da", "#ff9d35", 
                                 "#8abe5e", "#ffe989", "#c52060", "#3f969a")) +
    scale_alpha_manual(values = alpha_vals) +
    theme_void() +
    theme(legend.position = "none",
          plot.background = element_rect(fill = "#222222"))
}

这允许通过以下操作轻松重新创建图形:

make_graphic()

enter image description here

如果你只想突出显示底部的第二个项目,你可以这样做:

make_graphic(2)

enter image description here

关于r - 如何使用 R(最好是 ggplot2)生成堆叠平面或重叠钻石的图形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70988573/

相关文章:

r - 点阵点图条件填充颜色

r - 基于多因素交互创建具有自定义标题和轴美观的自动化个人ggplots?

r - ggplot 中的希腊字母注释

r - 使用带分位数的 geom_boxplot

r - 计算向量中具有 x 值的元素数量

r - 关闭刻面标签的剪裁

r - 为什么scale_linetype_manual()会巧妙地改变线型的美感?

R - ggplot2 - geom_histogram 中的右间隔选项

r - 有没有一种方法或内置的 R 函数可以对列表中重复名称的值求和?

Rattle R 未加载数据