【问题标题】:Add +/- standard error bars to a 3D bar plot in latticeExtra将 +/- 标准误差条添加到 latticeExtra 中的 3D 条形图
【发布时间】:2016-05-05 00:42:24
【问题描述】:

我正在尝试找到一种方法,使用云函数将 +/- 标准误差条添加到 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)

【问题讨论】:

  • 您应该提供您的数据。粘贴来自dput(dd) 的输出。我假设它不会太长。还应该提供myColoursTrans
  • 添加了数据,希望我每次编辑帖子都没有收到通知!
  • 不,刚刚通知了您的评论。也许很乱,但dd &lt;- structure(list(AgeCage ... 给了我们你的数据,所以我们可以重现你的情节。没有承诺,但我会看看我是否可以尝试一些想法。

标签: r 3d bar-chart lattice


【解决方案1】:

您可以尝试使用来自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)。您可以尝试不使用颜色,只尝试最小值和最大值的线条。

【讨论】:

    猜你喜欢
    • 2016-01-04
    • 1970-01-01
    • 2014-07-04
    • 2020-12-31
    • 1970-01-01
    • 2014-05-24
    • 1970-01-01
    • 2023-03-19
    • 2011-01-09
    相关资源
    最近更新 更多