Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
701 views
in Technique[技术] by (71.8m points)

ggplot2 - How to R present two colsums with ggplot stat_summary?

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

enter image description here

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

With ggplot, it is essential to have a single data frame with everything in it (at least for a single plotting layer, e.g., all the bars in a plot). You create a data frame of the column sums, and then try to use external vectors for the id and the grouping, which makes things difficult.

This is how I would do it:

means = rbind(
    data.frame(mean = colSums(M), source = "M", col = 1:ncol(M)),
    data.frame(mean = colSums(M2), source = "M2", col = 1:ncol(M2))
)

means$col = factor(means$col)
## one nice data frame with everything needed for the plot    
means
#       mean source col
# 1  -0.6400      M   1
# 2  -1.4650      M   2
# 3  -0.8850      M   3
# 4  -1.6950      M   4
# 5  -2.0300      M   5
# 6  -0.2700      M   6
# 7  -0.3640     M2   1
# 8  -1.1240     M2   2
# 9  -0.8850     M2   3
# 10 -1.2775     M2   4
# 11 -1.4770     M2   5
# 12 -0.2990     M2   6

ggplot(means, aes(x = col, y = mean, fill = source)) +
    geom_bar(stat = 'identity', position = 'dodge')

enter image description here

You seem to want error bars too. I have no idea what would define those error bars - if you look at geom_errorbar it expects aesthetics ymin and ymax. If you calculate whatever values you want and add them as column to the data frame above, adding the error bar to the plot should be easy.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...