Creating a function to display mean geom_line on geom_point graph?

Hi, I am trying to produce a function() that will allow me to plot the mean as a geom_line over the top of a geom_point graph like this

tibble::tribble(
~Subject, ~Symptoms, ~Day, ~Test, ~Measure,
"AA", "Y", 1, "Prog", 106.6865,
"AA", "Y", 2, "Prog", 143.811,
"AA", "Y", 3, "Prog", 368.6435,
"AB", "Y", 1, "Prog", 241.0645,
"AB", "Y", 2, "Prog", 306.979,
"AB", "Y", 3, "Prog", 235.0865
)
#> # A tibble: 6 x 5
#> Subject Symptoms Day Test Measure
#>
#> 1 AA Y 1 Prog 107.
#> 2 AA Y 2 Prog 144.
#> 3 AA Y 3 Prog 369.
#> 4 AB Y 1 Prog 241.
#> 5 AB Y 2 Prog 307.
#> 6 AB Y 3 Prog 235.

I have tried to make a function to show the mean of progesterone (y), each day (x) like this:

geom_mean_prog <- function() {stat_summary(fun.y = Progesterone_Summary(mean), geom = "line")}

I have also tried like this:

geom_mean_prog <- function(formula = y~x) {stat_summary(fun.y = Progesterone_Summary(mean), geom = "line")}

And a couple of other variations

However, when I try to add this function, an error message appears saying R doesn't know how to add this to a plot.

Does anyone know what I am doing wrong with my function?

Doyou want the mean at each x value, like this:

library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 3.5.3
library(tibble)
#> Warning: package 'tibble' was built under R version 3.5.3
DF <- tibble::tribble(
  ~Subject, ~Symptoms, ~Day, ~Test, ~Measure,
  "AA", "Y", 1, "Prog", 106.6865,
  "AA", "Y", 2, "Prog", 143.811,
  "AA", "Y", 3, "Prog", 368.6435,
  "AB", "Y", 1, "Prog", 241.0645,
  "AB", "Y", 2, "Prog", 306.979,
  "AB", "Y", 3, "Prog", 235.0865
)
ggplot(DF,aes(Day, Measure)) + geom_point() + stat_summary(fun.y = "mean", geom="line")

Created on 2019-12-16 by the reprex package (v0.3.0.9000)

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