4

I`m trying to colour ribbon of complex functions. However, this does not go well (see the figure below):

screenshot

I want to paint only the lower part of the function. However, in this case, only the part connected by the vertices is painted, and the painted part contains the curve.

My code:

p1 = ggplot(data.frame(x=c(-2.14344,1.25), y=c(0,12.5)), 
             aes(x, y))
p1 = p1 + stat_function(fun = function(x)(exp( -18.82 + 16.99  * x) + exp(-0.5497 - 0.6572  * x)))
p1 = p1 + xlim(-2.14344,1.25) + ylim(0,12.5)
p1 = p1 + geom_ribbon(fill="blue", alpha = 0.5,
                      aes(ymax = exp( -18.82 + 16.99  * x) + exp(-0.5497 - 0.6572  * x), 
                          ymin = 0))
print(p1)

I tried to solve this problem and searched for the solutions on Google. But I could not find any suggestion.

If you know any suggestion, please tell me how to solve this. Thanks!

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
LandsEco
  • 43
  • 2
  • I assume you mean area under the curve? Check if the answer [here](https://stackoverflow.com/questions/33244629/filling-under-the-a-curve-with-ggplot-graphs) fits your need. – Z.Lin Sep 11 '17 at 13:24
  • Really thanks for teaching me URL and immediate reply! I regret lack of my survey. – LandsEco Sep 11 '17 at 14:11

1 Answers1

3

One option is to remove the geom_ribbon part and add geom = 'area' in your stat_function:

p1 <- ggplot(data.frame(x=c(-2.14344,1.25),y=c(0,12.5)), aes(x,y)) + 
      stat_function(fun=function(x)(exp( -18.82 + 16.99  * x) + exp(-0.5497 - 0.6572  * x)),geom = "area") + 
      xlim(-2.14344,1.25) + ylim(0,12.5) #+ geom_ribbon(fill="blue", alpha=0.5,aes(ymax=exp( -18.82 + 16.99  * x) + exp(-0.5497 - 0.6572  * x), ymin=0))
p1
timfaber
  • 2,060
  • 1
  • 15
  • 17