How to draw a complex ggplot2 function?

I am trying to colorize a tape of complex functions. However, this is not very good (see Figure below):

screenshot

I want to draw only the bottom of the function. However, in this case, only the part associated with the vertices is colored, and the painted part contains a curve.

My code is:

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 solutions on Google. But I could not find any suggestions.

If you know a suggestion, please tell me how to solve it. Thank!

+4
source share
1 answer

One option is to remove the part geom_ribbonand add geom = 'area'to 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
+2
source

Source: https://habr.com/ru/post/1685458/


All Articles