I think R designed tool for the taks is ggplot2 stat_summary
so I rejected barplot
because of the linked thread in the body.
The problem here is the declaration of R table structure with column headers ECG 1
and ECG 2
for the sums M.1.sum
and M.2.sum
, respectively, I think.
I try to do it with means.long <- melt(M.1.sum, M.2.sum)
.
Each item, M.1.sum
and M.2.sum
, has corresponding row-wise ids in ids
which should also included in the data structure itself, I think.
My proposal for its table column and row declarations is with aes(x=ids, y=value)
where value
is about the sums in ggplot
declaration.
Code
library('ggplot2')
library('reshape2')
M <- structure(c(-0.21, -0.205, -0.225, -0.49, -0.485, -0.49,
-0.295, -0.295, -0.295, -0.56, -0.575, -0.56, -0.69, -0.67,
-0.67, -0.08, -0.095, -0.095), .Dim = c(3L, 6L))
M2 <- structure(c(-0.121, -0.1205, -0.1225, -0.149, -0.485, -0.49,
-0.295, -0.295, -0.295, -0.56, -0.1575, -0.56, -0.69, -0.67,
-0.117, -0.08, -0.1095, -0.1095), .Dim = c(3L, 6L))
ids <- seq(1,6)
M.1.sum <- colSums(M)
M.2.sum <- colSums(M2)
# http://stackoverflow.com/q/22305023/54964
means.long <- melt(M.1.sum, M.2.sum)
ggplot(means.long, aes(x=ids, y=value ))+ # ,fill=factor(ids))) +
stat_summary(fun.y=mean, geom="bar",position=position_dodge(1)) +
scale_fill_discrete(name="ECG",
breaks=c(1, 2),
labels=c("1", "2"))+
stat_summary(fun.ymin=min,fun.ymax=max,geom="errorbar",
color="grey80",position=position_dodge(1), width=.2) +
xlab("ID")+ylab("Sum potential")
#deprecated because stat_summary designed for the case
#barplot(M.1.sum, ids)
#barplot(M.2.sum, ids)
Output does not look right
Expected output: 6x two columns side by side with legend of two items
Not sure how to use this one fill=factor(ids)))
because I did not label any columns in the table.
How can you better make the table?
R: 3.3.1
OS: Debian 8.5
See Question&Answers more detail:
os