如何R用ggplot stat_summary呈现两个colsums? [英] How to R present two colsums with ggplot stat_summary?

查看:306
本文介绍了如何R用ggplot stat_summary呈现两个colsums?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为R为taks设计的工具是 ggplot2 stat_summary ,所以我拒绝了 barplot ,因为链接的线程在

这里的问题是使用列标题 ECG 1 和<$ c声明R表结构$ c> ECG 2 用于总和 M.1.sum M.2.sum 我试着用 means.long< - melt(M.1.sum,M.2.sum)来做。
每个项目 M.1.sum M.2.sum 具有相应的行式我认为它应该包含在数据结构本身中的 ids 中的id。
我的表列和行声明的建议是使用 aes(x = ids,y = value)其中 value 是关于 ggplot 声明中的总和。
代码

  library('ggplot2')
library('reshape2')
$ (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 < - 结构(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))

id< - seq(1,6)
M.1.sum< - colSums (M)
M.2.sum < - colSums(M2)

#http://stackoverflow.com/q/22305023/54964
means.long< - 熔化(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 = m in,fun.ymax = max,geom =errorbar,
color =grey80,position = position_dodge(1),width = 0.2)+
xlab(ID)+ ylab Sum potential)

#deprecated因为stat_summary为这种情况而设计
#barplot(M.1.sum,ids)
#barplot(M.2.sum,ids )

输出看起来不正确



预期输出:6x两列并列两个项目的图例

不知道如何使用这个 fill = factor(ids))),因为我没有标记表中的任何列。
如何让桌面更好?



R:3.3.1

操作系统:Debian 8.5

$ b $使用 ggplot ,必须有一个包含所有内容的单个数据框(至少对于单个绘图层,例如,绘图中的所有条)。你创建一个数据框的列总和,然后尝试使用外部向量的id和分组,这使得事情变得困难。



这就是我将要做的它:

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


表示$ col =因子(意味着$ col)
##一个不错的数据框,包含了所有需要的数据
表示
#表示源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 ='身份',position ='dodg e')



你似乎也想要错误栏。我不知道什么会定义这些错误栏 - 如果你看看 geom_errorbar ,它期望美学 ymin YMAX 。如果你计算出你想要的任何值,并将它们作为列添加到上面的数据框中,那么将错误栏添加到图中应该很容易。


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

解决方案

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')

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.

这篇关于如何R用ggplot stat_summary呈现两个colsums?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆