RPlotter
RPlotter

Reputation: 109

How to sum the data using ggplot2 in R?

I am trying to find straightforward methods to sum the data by various factors in ggplot.

Sample data (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'

I have tried the following methods:

  1. qplot(x, y, data=dat1[, sum(y)],by = "key,value", size=V1) Error in [.data.frame(dat1, , sum(y)) : object 'y' not found

  2. ggplot(data = dat1, aes(x = key, y = value)) + stat_summary(fun.y = sum, colour = "red", size = 1) Error in eval(expr, envir, enclos) : object 'key' not found

Can anyone recommend where I may be going wrong and alternatives to the same?

Upvotes: 1

Views: 10498

Answers (1)

akrun
akrun

Reputation: 887811

We can specify the 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

Upvotes: 4

Related Questions