在latticeExtra

时间:2016-05-05 00:42:25

标签: r 3d bar-chart lattice

我试图找到一种方法,使用云函数将+/-标准误差线添加到latticeExtra中的3D条形图。我知道3D图不是可视化数据的最佳方式,但我的主管已经要求我将它们放在一起以查看它的外观。我花了几天时间搜索本网站和其他人的帖子,但找不到答案。它甚至可以在3D空间中使用吗?

我的数据正在观察不同驯化治疗中3种不同年龄组合的3种不同大小等级的x种类的覆盖率。我的颜色按大小(sml,med,lg)和年龄/笼子(4,8,12周;笼子,没笼子)进行颜色编码。

我的代码是:

library(latticeExtra)

cloud(Sol.Ascid ~ AgeCage + Size, dd,
  panel.3d.cloud = panel.3dbars,
  col.facet = level.colors(dd$CageSize, at = 0:6,
                           col.regions = myColoursTrans,
                           colors = TRUE),
  xbase = 0.6, ybase = 0.6, zlim = c(0, 6),
  xlab = NULL, ylab = NULL, zlab = NULL, main = "Solitary Ascidians",
  lty = 0,  #got rid of lines since they're not right.. not sure why..
  screen = list(z = 50, x = -50, y = 0),
  scales = list(arrows = FALSE, col = 1), 
  par.settings = list(axis.line = list(col = "transparent")))

dd是一个数据框,包含所有物种的均值和标准误差。

3D bar plot from the code above

如果这是一个愚蠢的问题我很抱歉,我是r和这个论坛的新手。

此致

DaveTurek - myColoursTrans的代码(如下),我只是将数据复制并粘贴到这个窗口中(看起来有点乱)?

structure(list(AgeCage = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 
3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L), .Label = c("4 NC", 
"4 C", "8 NC", "8 C", "12 NC", "12 C"), class = "factor"), Size = structure(c(1L, 
2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 
3L), .Label = c("Small", "Medium", "Large"), class = "factor"), 
CageSize = c(2, 4, 6, 1, 3, 5, 2, 4, 6, 1, 3, 5, 2, 4, 6, 
1, 3, 5), Sol.Ascid = c(0, 0, 0, 0.1, 0, 0, 0, 0, 0, 0.2, 
0.2, 0.5, 0, 0, 0.3, 0.2, 0.9, 4.7), Sol.Ascid.se = c(0, 
0, 0, 0.1, 0, 0, 0, 0, 0, 0.133333333333333, 0.133333333333333, 
0.30731814857643, 0, 0, 0.152752523165195, 0.133333333333333, 
0.406885187191123, 1.19303534454489), Barnacles = c(0.5, 
0.2, 0, 0.5, 0.3, 0.4, 0.4, 0.1, 0, 0.7, 0.5, 0, 0.2, 0.5, 
0.9, 0.4, 0.2, 0.8), Barnacles.se = c(0.30731814857643, 0.133333333333333, 
0, 0.341565025531987, 0.3, 0.4, 0.221108319357027, 0.1, 0, 
0.395811402901264, 0.223606797749979, 0, 0.2, 0.223606797749979, 
0.433333333333333, 0.4, 0.133333333333333, 0.416333199893227
), Dead.Barnacles = c(0, 0.1, 0, 0.7, 0.2, 0, 0.9, 3.1, 0.5, 
4.4, 7.7, 1.9, 1.55, 3.35, 6.4, 1.7, 2.7, 8.3), Dead.Barnacles.se = c(0, 
0.1, 0, 0.334995854037363, 0.2, 0, 0.406885187191123, 1.19675487140108, 
0.401386485959743, 1.70749979664876, 1.9035055380359, 0.737111479583199, 
0.216666666666667, 1.39851111305321, 1.38403596613511, 0.7, 
0.989388138643722, 3.33683149776018), Barnacle.Scar = c(1.3, 
0.3, 0, 0, 0, 0, 3.3, 18, 6.9, 0, 0.3, 0.1, 1.7, 1.9, 6, 
0, 0.3, 0), Barnacle.Scar.se = c(0.448454134902457, 0.213437474581095, 
0, 0, 0, 0, 0.7, 2.67083008320135, 1.8405916923038, 0, 0.3, 
0.1, 0.53851648071345, 0.706320670013903, 1.25609624542778, 
0, 0.3, 0), Sponges = c(0, 0, 0, 0.1, 0, 0, 0.1, 0.3, 0.1, 
0, 0.5, 1.4, 0, 0.6, 1.3, 0.3, 1.2, 2.5), Sponges.se = c(0, 
0, 0, 0.1, 0, 0, 0.1, 0.152752523165195, 0.1, 0, 0.268741924943285, 
0.561743318211757, 0, 0.221108319357027, 0.53851648071345, 
0.3, 0.3590109871423, 0.819891591749923), Serpulids = c(0.8, 
0.3, 0, 0.9, 0.1, 0, 1.7, 2.4, 0.95, 0.9, 2.3, 1.4, 1, 2.4, 
1.5, 1.5, 2.1, 1.9), Serpulids.se = c(0.32659863237109, 0.213437474581095, 
0, 0.585946527708232, 0.1, 0, 0.683942817622773, 0.956846672960488, 
0.320156211871642, 0.31446603773522, 0.683942817622773, 0.791622805802528, 
0.298142396999972, 0.581186525805423, 0.5, 1.17615191762516, 
0.674124947205223, 0.706320670013903), Serpulid.Scars = c(1.7, 
0.5, 0, 0, 0, 0, 0.9, 1.7, 0.4, 0, 0, 0, 1.2, 1.5, 1.4, 0, 
0, 0), Serpulid.Scars.se = c(0.472581562625261, 0.401386485959743, 
0, 0, 0, 0, 0.406885187191123, 0.517472489875334, 0.221108319357027, 
0, 0, 0, 0.32659863237109, 0.30731814857643, 0.42687494916219, 
0, 0, 0)), .Names = c("AgeCage", "Size", "CageSize", "Sol.Ascid", 
"Sol.Ascid.se", "Barnacles", "Barnacles.se", "Dead.Barnacles", 
"Dead.Barnacles.se", "Barnacle.Scar", "Barnacle.Scar.se", "Sponges", 
"Sponges.se", "Serpulids", "Serpulids.se", "Serpulid.Scars", 
"Serpulid.Scars.se"), row.names = c(NA, -18L), class = "data.frame")

library(RColorBrewer)                 # load some colour palettes
display.brewer.all()                  # check them out visually
myColours <- brewer.pal(6, "Paired")  # choose the first 6 from the one with paired colours

## function to add transparency to colours
add.alpha <- function(col, alpha=1){
  apply(sapply(col, col2rgb)/255, 2, 
        function(x) 
        rgb(x[1], x[2], x[3], alpha=alpha))  
}

myColoursTrans <- add.alpha(c(myColours), alpha = 0.7)

1 个答案:

答案 0 :(得分:0)

您可以尝试使用latticeExtra中的图层。 定义上限和下限值。

dd = within(dd, {
  SAmax = Sol.Ascid+Sol.Ascid.se
  SAmin = Sol.Ascid-Sol.Ascid.se
  })

定义单独的颜色。

myColoursTrans1 <- add.alpha(c(myColours), alpha = 0.1)
myColoursTrans2 <- add.alpha(c(myColours), alpha = 0.3)
myColoursTrans3 <- add.alpha(c(myColours), alpha = 0.9)

然后分三层建造。

cloud(SAmax ~ AgeCage + Size, dd,
  panel.3d.cloud = panel.3dbars,
  col.facet = level.colors(dd$CageSize, at = 0:6,
                           col.regions = myColoursTrans1,
                           colors = TRUE),
  xbase = 0.6, ybase = 0.6, zlim = c(0, 6),
  xlab = NULL, ylab = NULL, zlab = NULL, main = "Solitary Ascidians",
 # lty = 0,  #got rid of lines since they're not right.. not sure why..
  screen = list(z = 50, x = -50, y = 0),
  scales = list(arrows = FALSE, col = 1), 
  par.settings = list(axis.line = list(col = "transparent"))) +

  as.layer(cloud(Sol.Ascid ~ AgeCage + Size, dd,
  panel.3d.cloud = panel.3dbars,
  col.facet = level.colors(dd$CageSize, at = 0:6,
                           col.regions = myColoursTrans2,
                           colors = TRUE),
    xlab = NULL, ylab = NULL, zlab = NULL, main = "",
  xbase = 0.6, ybase = 0.6, zlim = c(0, 6),#lty=0,
    screen = list(z = 50, x = -50, y = 0))) +

  as.layer(cloud(SAmin ~ AgeCage + Size, dd,
  panel.3d.cloud = panel.3dbars,
  col.facet = level.colors(dd$CageSize, at = 0:6,
                           col.regions = myColoursTrans3,
                           colors = TRUE),
    xlab = NULL, ylab = NULL, zlab = NULL, main = "",
    xbase = 0.6, ybase = 0.6, zlim = c(0, 6),#lty=0,
    screen = list(z = 50, x = -50, y = 0)))

上表面不能很好地重叠(见中12 C)。您可以尝试不使用颜色,仅使用最小和最大的行