2016-12-10 33 views
0

我试图找到简单的方法来汇总由ggplot中的各种因素的数据。如何在R中使用ggplot2求和数据?

样本数据(DAT):

A B C 
1 2 3 
1 2 3 
1 2 3 

library(ggplot2) 
library(reshape) 
dat1 <- gather(dat) #convert into long form with 'key' and 'value' 

我曾尝试以下方法:

  1. qplot(X,Y,数据= DAT1 [,总和(Y)],由= “键,值”,大小= V1) 错误[.data.frame(DAT1,和(Y)):对象的 'y' 未找到

  2. ggplot(数据= DAT1,AES(X =键,Y =值))+ stat _Summary(fun.y =总和,颜色= “红”,大小= 1) 错误的eval(expr中,ENVIR,enclos):对象 '钥匙' 未找到

谁能推荐其中I可以是出错和替代方法相同?

+0

在哪里? – akrun

+1

请建立一个更可重复的例子。包括'tidyr'库。创建'dat'数据框:'dat < - data.frame(A = rep(1,3),B = rep(2,3),C = rep(3,3))'。你在ggplot调用中引用'dat2'。什么是'dat2'? – timtrice

+0

我的不好。我已经改变了它 – BioBuilder

回答

1

我们可以指定为'dat2`创建geom

library(ggplot2) 
ggplot(data = dat1, aes(x = key, y = value)) + 
     stat_summary(fun.y = sum, geom="point", colour = "red", size = 1) 

enter image description here