Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2: add line for sum of group values at each x

Tags:

r

ggplot2

Let's say I collect the posts in Stack Overflow and I classify them in N categories. My goal is to plot the N percentage every day and a line with the total number of posts per day.

To play with, I'll use a toy dataframe. I can plot the percentage of every category per day:

data(beav1)
beav1$day <- as.factor(beav1$day)
beav1[beav1$day==346,]$time <- 1:sum(beav1$day==346)
beav1[beav1$day==347,]$time <- 1:sum(beav1$day==347)
beav1 <- filter(beav1, time<23)
ggplot(beav1, aes(x=time, y=temp, group=day, fill=day, color=day)) + 
  geom_line()

enter image description here

But how can I add the line with the total temperature? Or the mean?

Edit: The difference with this other question is that I would like one single line for all the groups and not a line per group.

dataset

dput(beav1)
structure(list(day = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L), .Label = c("346", "347"), class = "factor"), 
    time = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 
    13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 1L, 2L, 
    3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 
    16L, 17L, 18L, 19L, 20L, 21L, 22L), temp = c(36.33, 36.34, 
    36.35, 36.42, 36.55, 36.69, 36.71, 36.75, 36.81, 36.88, 36.89, 
    36.91, 36.85, 36.89, 36.89, 36.67, 36.5, 36.74, 36.77, 36.76, 
    36.78, 36.82, 36.93, 36.83, 36.8, 36.75, 36.71, 36.73, 36.75, 
    36.72, 36.76, 36.7, 36.82, 36.88, 36.94, 36.79, 36.78, 36.8, 
    36.82, 36.84, 36.86, 36.88, 36.93, 36.97), activ = c(0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L)), class = "data.frame", row.names = c(NA, 
-44L), .Names = c("day", "time", "temp", "activ"))
like image 989
alberto Avatar asked Mar 14 '23 06:03

alberto


1 Answers

Ok, notice that group, fill and color are not in ggplot but in geom_line, this way you can use stat_summarywithout redefining the groups.

ggplot(beav1, aes(x=time, y=temp)) + 
    geom_line(aes(group=day, fill=day, color=day))+
    stat_summary(fun.y = mean, na.rm = TRUE, group = 3, color = 'black', geom ='line')

And if you want the sum just place fun.y = sum

like image 109
Mamoun Benghezal Avatar answered Mar 15 '23 19:03

Mamoun Benghezal