1

I would like to highlight the area between two function curves plotted with geom_function.

For example, I create two curves:

ggplot() + geom_function(fun = function(x){x^2}) + geom_function(fun = function(x){x^2 + 1})

How can I highlight the area between these curves? I think geom_ribbon might be the answer, but all the example code I saw for geom_ribbon used a dataset for mapping. How can I do that with geom_function, i.e. curves only based on a function and not on actual data?

  • I feel like this question was prematurely closed - the OP asked if this could be done without a data frame (like `geom_function`). The answer shared creates a data frame with y values used for `ymin` and `ymax` based on an `x` column. – chemdork123 Apr 21 '21 at 21:15
  • 2
    @chemdork123 If there is a way to do it without creating a data.frame, you can add it an answer to the existing question as well. But generally ggplot is good at ploting data, not abstract functions. If you want to manipulate those functions, you're going to need to calculate those values yourself in a data.frame – MrFlick Apr 21 '21 at 21:20

0 Answers0