While a reprex would help, if I'm following you, the main trick is to assign a "fake" color aesthetic rather than manually overriding the color in the stat_function call. That means that you either have to use scale_color_manual if you really want a black line, or you have to accept stat_function being some automatically chosen color.
suppressPackageStartupMessages(library(tidyverse))
dat <- tribble(
~ Group, ~ x, ~ y,
"A", -1, -1,
"A", 1, 1,
"B", -1, 1,
"B", 1, -1
)
ggplot(data = dat) +
geom_line(aes(x = x, y = y, color = Group)) +
stat_function(fun = cos, aes(color = "cos")) +
# The next line could be left out if any color is acceptable
scale_color_manual(values = c(A = "red", B = "green", cos = "black"))
