Q:

summarizing variables with similar but not identical x-values

I have a dataset with three repeats. I want to plot the mean Y variable for each X variable. However, my x-values are not identical.
Here is a small example of what I mean:
Time Value repeat_name
0    5     repeat1
0    5     repeat2
0    5     repeat3
3.1  7     repeat1
3.25 8     repeat2
3    9     repeat3
6.2  5     repeat1
6.5  5     repeat2
6    5     repeat3
9.3  5     repeat1
9.75 5     repeat2
9    5     repeat3

Now I was hoping there would be some way I could bin the time and then put all values that match the binned value inside
so I have the following bins:
Time
0-4 (values (5,5,5,7,8,9))
4-8 (values (5,5,5))
8-12 (values (5,5,5))

and that I could then take the mean of those bins and plot a histogram with ggplot. I am however, not sure how I would achieve my goal.
I am also not sure if there is a better approach to this.
Thank you in advance.

A:

Just some suggestions. The binning is of course the challenge. You can either round, or create cuts. It depends totally on what you want to show. 
I will show two options, one with plyr::round_any, the other using cut
library(tidyverse)

foo <- read_csv("Time, Value, repeat_name
0,    5,     repeat1
0,    5,     repeat2
0,    5,     repeat3
3.1,  7,     repeat1
3.25, 8,     repeat2
3,    9,     repeat3
6.2,  5,     repeat1
6.5,  5,     repeat2
6,    5,     repeat3
9.3,  5,     repeat1
9.75, 5,     repeat2
9,    5,     repeat3")

foo %>% 
  mutate(time_round = plyr::round_any(Time, 5)) %>% 
  ggplot(aes(time_round, Value)) +
  stat_summary() +
  scale_y_continuous(limits =  c(0, NA))
#> No summary function supplied, defaulting to `mean_se()`

foo %>% 
  mutate(time_cut = cut(Time, c(-Inf,4,8,Inf))) %>% 
  ggplot(aes(time_cut, Value)) +
  stat_summary() +
  scale_y_continuous(limits =  c(0, NA))
#> No summary function supplied, defaulting to `mean_se()`

Created on 2020-04-15 by the reprex package (v0.3.0)

A:

If you would like to plot the mean of a variable based on the grouping of another variable, a histogram is not the proper chart, given that a histogram reflects counts of observations within a bin range. 
As Tjebo points out above, ggplot has some stat functions that can compute summary statistics on data. 
The stat_summary_bin() function can compute summary statistics across binned ranges. To compute the mean of the bins of Time from above you can use the following code:
ggplot(df, aes(x=Time, y = Value)) +
  stat_summary_bin(geom = "point", 
                   fun.y = mean, 
                   fun.ymin = NULL, 
                   fun.ymax= NULL, 
                   breaks=seq(0, 12,by = 4))

where mean is the summary function computed on the data and the bin ranges are set using seq(0, 12, by = 4) to match the bin ranges from above. It is also possible to provide a default bin width using the binwidth argument or the  number of bins using the bins argument.

Alternatively, you can calculate group means with a dplyr::case_when() statement to generate a grouping variable, group the data frame by that newly created grouping variable and finally summarize the values. 
df %>%
  mutate(
    group_var = case_when(
      Time >=0 & Time < 4 ~ 1,
      Time >= 4 & Time < 8 ~ 2,
      Time >= 8 & Time < 12 ~ 3
    )
  ) %>%
  group_by(group_var) %>% 
  summarize(grouped_mean = mean(Value)

