I know how to create regular plots with error bars for, say, one factor (e.g. experiment
) and one measurement (e.g. quality
). I first summarize the data to get mean and CI using the summarySE
function given on this site. For example:
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
...
That way I could plot:
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")
However, now I have to do the same with two measurements—not just quality
, but also confidence
. For example, my data might look as follows:
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
...
How would I go about plotting confidence
(with confidence_ci
) and quality
(with quality_ci
) next to each other, for each hrc_id
?
I thought I could melt
the dataframe so that confidence
and quality
would be the measurement variables, but then I lose the CI values that belong to them.
You can convert your dataframe to long format with grouped columns in one step using reshape(...)
. Assuming your dataframe is 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
As far as I know you cannot do this with melt(...)
- you'd have to use the rbind
approach mentioned in your comment.