r - 带有两个测量变量和一个因子的误差线的 ggplot

标签 r ggplot2 reshape2

我知道如何为一个因素(例如 experiment)和一个测量(例如 quality)创建带有误差条的常规图。我首先使用给定 on this sitesummarySE 函数汇总数据以获得平均值和 CI .例如:

   hrc_id experiment  N  quality        sd         se         ci
        0      FB_IS 77 3.584416 0.6757189 0.07700532 0.15336938
        0     FB_ACR 77 3.779221 0.6614055 0.07537416 0.15012064
        1      FB_IS 77 3.038961 0.7854191 0.08950681 0.17826826
        1     FB_ACR 77 3.129870 0.8483831 0.09668223 0.19255935
...

这样我就可以绘制:

ggplot(d, aes(hrc_id, quality), quality, color = experiment)) + 
    geom_point(position = position_dodge(width = .5)) +
    geom_errorbar(aes(ymin = quality - ci, ymax = quality + ci), width = .5, position = "dodge")


但是,现在我必须对两个测量做同样的事情——不仅是质量,还有置信度。例如,我的数据可能如下所示:

hrc_id confidence confidence_ci  quality quality_ci
     0   3.573718    0.02068321 4.576923 0.02864818
     1   3.403846    0.03193104 1.658120 0.04441434
    10   3.160256    0.02520483 3.038462 0.04476492
...

我将如何在旁边绘制置信度(使用confidence_ci)和quality(使用quality_ci)彼此,对于每个 hrc_id?

我以为我可以融化数据框,这样confidencequality将成为测量变量,但后来我失去了CI值属于他们。

最佳答案

您可以使用 reshape(...) 一步将数据框转换为包含分组列的长格式。假设您的数据框是 df:

gg <- reshape(df,idvar="hrc_id",               # idvar: identifies cases
              times=c("confidence","quality"), # group of columns to be reshaped
              timevar="measurement",           # column name to use for grouping vars
              varying=2:5,                     # columns are to be reshaped
              v.names=c("value","value.ci"),   # column names for reshaped values
              direction="long")                # convert to long format
gg
#               hrc_id measurement    value   value.ci
# 0.confidence       0  confidence 3.573718 0.02068321
# 1.confidence       1  confidence 3.403846 0.03193104
# 10.confidence     10  confidence 3.160256 0.02520483
# 0.quality          0     quality 4.576923 0.02864818
# 1.quality          1     quality 1.658120 0.04441434
# 10.quality        10     quality 3.038462 0.04476492

据我所知,您不能使用 melt(...) 执行此操作 - 您必须使用评论中提到的 rbind 方法。

关于r - 带有两个测量变量和一个因子的误差线的 ggplot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20846168/

相关文章:

从长到宽 reshape 数据 - 了解 reshape 参数

r - 在拥有 `melt` 之前必须先对其进行 `cast` 数据帧吗?

r - 使用dplyr创建命名向量

r - data.table 的 tables() 函数运行我的一些 .Rprofile 函数

r - 有没有办法限制 ggplot2 中的 vline 长度

r - 不同 Y 轴的组合柱图和折线图

r - 如何将reshape2包中的melt.data.frame函数返回 "variable"列更改为 "character"类?

所有 NA 的 rowSums

r - 如何在不添加 "Row.names"列的情况下按行名合并数据帧?

r - 排列 ggplot 多个对象,同时保持高度不变