ggplot with stat_summary for mean along time represented by days

I have this data representing the value of a variable Q1 along time. The time is not represented by dates, it is represented by the number of days since one event.

https://www.mediafire.com/file/yfzbx67yivvvkgv/dat.xlsx/file

I'm trying to plot the mean value of Q1along time, like in here

Plotting average of multiple variables in time-series using ggplot

But I'm getting this plot with the code below

 library(Hmisc)
    ggplot(dat,aes(x=days,y=Q1,colour=type,group=type)) +
      stat_summary(fun.data = "mean_cl_boot", geom = "smooth")

Hi @anacho. The mean_cl_boot is to take mean of each days and your data have one Q1 value for each day, so the plot like geom_line only. I think of one way to calculate the mean among time before plotting. But there should be a more simple way to do it.

library(tidyverse)
#> Warning: package 'ggplot2' was built under R version 3.4.4
#> Warning: package 'tidyr' was built under R version 3.4.4
#> Warning: package 'readr' was built under R version 3.4.4
#> Warning: package 'purrr' was built under R version 3.4.4
#> Warning: package 'stringr' was built under R version 3.4.4
#> Warning: package 'forcats' was built under R version 3.4.3

dat <- structure(list(Q1 = c(5, 5, 3, 5, 4, 1, 4, 4, 2, 4, 5, 4, 3, 
                             4, 2, 2, 3, 2, 4, 1, 4, 4, 3, 3, 4, 4, 4, 3, 4, 3, 4, 2, 2, 2, 
                             4, 5, 2, 4, 3, 2, 2, 3, 1, 3, 3, 3, 4, 4, 2, 4, 2, 2, 4, 2, 3, 
                             3, 3, 4, 4, 3, 3, 1, 2, 3, 3, 5, 1, 3, 2, 4, 2, 4, 4, 4, 2, 2, 
                             4, 4, 3, 4, 2, 3, 3, 3, 3, 1, 1, 4, 4, 3, 4, 3, 4, 3, 2, 4, 4, 
                             3, 2, 4, 4, 2, 3, 4, 3, 4, 4, 2, 3, 4, 5, 4, 4, 4, 2, 5, 3, 3, 
                             5, 4, 4, 3, 3, 4, 4, 3, 3, 4, 3, 3, 5, 4, 3, 3, 4, 3, 4, 3, 3, 
                             3, 3, 2, 4, 3, 3, 3, 3, 5, 3, 2, 3, 4, 3, 3, 4, 3, 4, 4, 1, 4, 
                             3, 2, 2, 2, 5, 2, 2, 2, 3, 3, 1, 3, 4, 4, 3, 3, 3, 3, 3, 3, 2, 
                             4, 3, 1, 5, 3, 3, 5, 4, 3, 5, 2, 3, 4, 2, 3, 4, 3, 4, 3, 3, 2, 
                             3, 3, 2, 2, 3, 4), days = c(16, 24, 59, 62, 69, 84, 86, 103, 
                                                         124, 125, 133, 152, 159, 161, 177, 177, 180, 187, 197, 198, 205, 
                                                         207, 212, 215, 215, 230, 243, 249, 251, 253, 259, 261, 263, 272, 
                                                         287, 292, 296, 312, 313, 320, 327, 329, 331, 335, 337, 343, 344, 
                                                         344, 354, 356, 358, 358, 362, 368, 371, 372, 381, 391, 400, 405, 
                                                         422, 428, 429, 437, 439, 451, 451, 454, 458, 462, 465, 466, 470, 
                                                         480, 484, 487, 488, 488, 489, 502, 510, 514, 515, 516, 518, 519, 
                                                         521, 523, 528, 533, 536, 542, 564, 570, 571, 572, 573, 575, 577, 
                                                         580, 583, 584, 584, 595, 595, 597, 598, 603, 605, 606, 606, 607, 
                                                         607, 610, 613, 614, 617, 619, 626, 628, 630, 633, 637, 639, 640, 
                                                         642, 643, 643, 644, 645, 646, 650, 653, 654, 654, 655, 658, 661, 
                                                         662, 663, 665, 667, 668, 669, 670, 671, 672, 673, 675, 679, 680, 
                                                         681, 684, 687, 687, 687, 691, 694, 695, 698, 702, 702, 706, 706, 
                                                         709, 709, 711, 712, 714, 719, 721, 721, 722, 723, 724, 726, 726, 
                                                         727, 728, 728, 729, 730, 730, 731, 731, 734, 735, 735, 736, 736, 
                                                         739, 742, 743, 744, 747, 748, 750, 751, 752, 753, 753, 753, 755, 
                                                         755, 755, 755, 757, 757), type = c("H", "D", "H", "H", "H", "D", 
                                                                                            "H", "H", "D", "H", "H", "H", "D", "D", "D", "H", "D", "H", "H", 
                                                                                            "D", "D", "H", "H", "H", "H", "D", "H", "H", "H", "H", "D", "H", 
                                                                                            "D", "H", "D", "D", "H", "H", "D", "H", "D", "H", "H", "H", "H", 
                                                                                            "D", "H", "H", "H", "H", "H", "D", "H", "H", "H", "D", "H", "H", 
                                                                                            "H", "H", "H", "H", "H", "H", "H", "D", "H", "H", "H", "H", "H", 
                                                                                            "H", "D", "D", "H", "H", "H", "H", "H", "H", "D", "H", "D", "H", 
                                                                                            "H", "D", "H", "H", "H", "H", "D", "H", "D", "H", "D", "H", "H", 
                                                                                            "H", "D", "D", "D", "H", "H", "H", "H", "D", "D", "H", "D", "H", 
                                                                                            "H", "D", "H", "H", "H", "H", "H", "H", "H", "H", "H", "H", "D", 
                                                                                            "H", "D", "H", "H", "D", "H", "H", "H", "D", "H", "D", "D", "D", 
                                                                                            "D", "D", "H", "D", "H", "D", "H", "D", "H", "D", "D", "H", "D", 
                                                                                            "D", "H", "H", "H", "H", "D", "H", "H", "H", "D", "D", "D", "H", 
                                                                                            "H", "D", "H", "H", "D", "D", "H", "H", "H", "H", "H", "H", "D", 
                                                                                            "D", "D", "H", "H", "D", "D", "H", "H", "H", "H", "H", "H", "H", 
                                                                                            "D", "H", "H", "H", "H", "D", "D", "H", "D", "H", "D", "D", "H", 
                                                                                            "H", "H", "D", "H", "H", "H", "H")), .Names = c("Q1", "days", 
                                                                                                                                            "type"), row.names = c(NA, -208L), class = c("tbl_df", "tbl", 
                                                                                                                                                                                         "data.frame"))

dat %>%
  arrange(type, days) %>%
  group_by(type) %>%
  mutate(vectorQ1 = accumulate(Q1, ~{c(.x, .y)})) %>%
  rowwise() %>%
  mutate(mean = mean(unlist(vectorQ1))) %>%
  ggplot(aes(x=days,y=mean,colour=type,group=type)) +
  geom_smooth()
#> `geom_smooth()` using method = 'loess' and formula 'y ~ x'

Created on 2019-09-21 by the reprex package (v0.3.0)

1 Like

THANKS so much!!!!!!!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.