1
votes

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?

1
where is dat2 created?akrun
Please build a more reproducible example. Include the tidyr library. Create the dat dataframe: dat <- data.frame(A = rep(1, 3), B = rep(2, 3), C = rep(3, 3)). You are referencing dat2 in your ggplot calls. What is dat2?timtrice
My bad. I have changed itBioBuilder
The dat[, sum(y)] will not work because there is no 'y' column and it is not a data.tableakrun

1 Answers

2
votes

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