【问题标题】:CI display for ggplot2ggplot2 的 CI 显示
【发布时间】:2016-05-17 05:59:12
【问题描述】:

我有一个看起来像的矩阵

 m =    structure(c(323.779052983988, 468.515895704753, 587.268448071498, 
701.348128517059, 779.979804648318, 727.214175106036, 907.318055915511, 
1115.76665653904, 256.756620668571, 402.701329692437, 487.179245291945, 
490.318053207526, 654.076130682292, 637.123436099074, 722.662552444773, 
792.505947658499, 403.652330928532, 577.534367257774, 900.565634583409, 
920.152416244856, 2357.72405145892, 3587.16328098826, 1452.70178195117, 
1579.28418044468, 338.358847483685, 454.199083058843, 599.97279688233, 
700.985565850218, 741.316909631413, 801.287026382171, 922.197411647728, 
1114.06641511944, 291.406778366111, 395.588263809182, 605.249603657004, 
499.747863299406, 535.230373829629, 542.056360622003, 796.821508497618, 
765.755975092841, 385.004883847313, 658.784861034504, 822.223611208372, 
1145.62924659969, 874.330710055459, 2138.50154766236, 1622.81483333837, 
5233.38890249983, 326.628586231411, 475.233191752907, 584.49028480417, 
700.341240251852, 786.302433055766, 900.413365602976, 964.629943008088, 
1028.82949413297, 338.629636961477, 488.195412653447, 587.800574630787, 
716.597816045267, 822.268333195828, 998.509804262574, 1037.41705762635, 
1070.88757066892, 365.518529559524, 545.02881088877, 675.193759918472, 
850.711841186985, 1006.39383294974, 1155.64170488707, 1318.06948607024, 
1382.22753306512, 326.238411688287, 468.96417383265, 579.601628485875, 
687.695555889267, 781.084920045155, 873.426833221962, 951.816393171732, 
1040.03875034591, 335.355219968458, 486.147238845314, 623.244412005171, 
748.505818444964, 860.86919935457, 983.642351192889, 1139.59065966632, 
1231.14044698001, 350.834428083403, 510.783163477734, 661.443754597131, 
809.775151175334, 937.8698594887, 1095.74073622628, 1275.45665833418, 
1409.44183362004), .Dim = c(8L, 12L))

所以第 1、4、7 和 10 列是条的高度。我想在 ggplot2 中做一个分组条形图,所以我这样做了

library(ggplot2)
library(reshape2)

m[,c(1,4,7,10)]->m1
as.data.frame(m1)->m1
rownames(m1)<-c("t2","t3","t4","t5","t6","t7","t8","t9")
colnames(m1)<-c("a1","a2","a3","a4")
t(m1)->m1
melt(m1)->m1


ggplot(m1, aes(factor(Var2), value, fill = Var1)) + 
  geom_bar(stat="identity", position = "dodge") + 
  scale_fill_brewer(palette = "Set1")

现在我想使用 cols 2,3;5,6;8,9 和 11,12 在绘图上绘制误差线。因此,对于第 1 列中的值,第 2 列提供较低的 CI,第 3 列提供较高的 CI(绝对值)。然后这也适用于第 5 列和第 6 列 - 为第 4 列执行此操作.... 我在这里找到了这个http://docs.ggplot2.org/0.9.3.1/geom_errorbar.html

但它只显示了如何绘制对称误差线 - 有人可以告诉我如何将基于 cols 2,3 .... 的误差线添加到分组条形图中吗?

【问题讨论】:

  • 没人有想法吗?
  • 看看?geom_errorbar的最后一个例子
  • @Sandy Muspratt 抱歉 - 但它说关于画线 - 如果我们想画线,我们需要手动设置定义线的 # 组 - 这里是 # 原始数据框中的组 -所以不确定我是否理解。请提供某种示例来展示如何获得我的想法是否可行?
  • 我不认为你在看最后一个例子。它绘制 geom_bar 和 geom_errorbar。
  • 对不起,但不在此页面上docs.ggplot2.org/0.9.3.1/geom_errorbar.html

标签: r matrix ggplot2


【解决方案1】:

您的示例无法重现,因此这里是来自?geom_errorbar 的示例的略微修改版本。请注意,df 包含变量 upperlower,它们是误差线的上限和下限。

df <- data.frame(
  trt = factor(c(1, 1, 2, 2)),
  resp = c(1, 5, 3, 4),
  group = factor(c(1, 2, 1, 2)),
  upper = c(1.1, 5.3, 3.3, 4.2),
  lower = c(0.8, 4.6, 2.4, 3.6)
)

dodge <- position_dodge(width=0.9)

ggplot(df, aes(trt, resp, fill = group)) +
  geom_bar(position = dodge, stat = "identity") +
  geom_errorbar(aes(ymin = lower, ymax = upper), position = dodge, width = 0.25)

使用您的数据。

# Values for the bar plots
m[,c(1,4,7,10)]->m1
as.data.frame(m1)->m1
rownames(m1)<-c("t2","t3","t4","t5","t6","t7","t8","t9")
colnames(m1)<-c("a1","a2","a3","a4")
t(m1)->m1
melt(m1)->m1

# Upper and lower limits for the error bars
error = m[,c(2,3,5,6,8,9,11,12)]
error = data.frame(error)

rownames(error)<-c("t2","t3","t4","t5","t6","t7","t8","t9")
colnames(error) = rep(c("a1","a2","a3","a4"), each = 2)

error = t(error)
error = melt(error)

error = cbind(error[seq(1, dim(error)[1], 2), ], error[seq(2, dim(error)[1], 2), 3])
names(error)[3:4] = c("lower", "upper")

# Combine the error bars and bar plot data frames
m1 = cbind(m1, error[, 3:4])

# Construct the plot
dodge <- position_dodge(width=0.9)

ggplot(m1, aes(factor(Var2), value, fill = Var1)) + 
  geom_bar(stat="identity", position = dodge) + 
  geom_errorbar(aes(ymin = lower, ymax = upper), position = dodge, width = 0.25) +
  scale_fill_brewer(palette = "Set1")

【讨论】:

  • 嗨 - 你能告诉我,为什么这个例子不是复制品吗?我可以复制粘贴,它可以工作???
  • 所以我已经编辑了您的代码,以便代码运行。而且,我不知道如何操作m 数据框来获取误差线的上限和下限的数据框。
  • 好吧——这也是我目前面临的问题。 u 和 l 值位于 col 2,3;5,6;8,9 和 11,12 for col 1,4,7,10....
  • @kutyw 我使用您的数据添加了一个图表。这是你所期待的吗?
猜你喜欢
  • 1970-01-01
  • 2016-06-26
  • 2023-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多