1

I'm integrating a function f(t) = 2t (just an example) and would like to plot the integral as a function of time t using

awesome_thing <- function(t) {2*t}
integrate(awesome_thing, lower=0, upper=10)

However, I would like to plot the integral as a function of time in ggplot2, so for this example the plotted points would be (1,1), (2,4), (3,9), ..., (10,100).

Is there an easy way to do this in ggplot (e.g., something similar to how functions are plotted)? I understand I can "manually" evaluate and plot the data for each t, but I thought i'd see if anyone could recommend a simpler way.

Thomas
  • 2,484
  • 8
  • 30
  • 49
  • Is that what you're looking for? http://stackoverflow.com/a/12429538/1898580 – Marat Talipov Feb 02 '15 at 23:31
  • Dear Marat, thanks so much for your help! Unfortunately, that is not what I want. Essentially, i'm trying to plot the integral of the function evaluated at each value of t, from 0 to 10. So in the above example, the plotted points would be (1,1), (2,4), (3,9), ..., (10,100). Does that make sense? – Thomas Feb 02 '15 at 23:35
  • 2
    I think you're likely to be better off evaluating outside of `ggplot2` and then plotting. – Ben Bolker Feb 02 '15 at 23:37
  • Thanks Ben, was hoping there was some quick n' dirty way, but thanks for your help! – Thomas Feb 02 '15 at 23:38

2 Answers2

2

Here is a ggplot solution and stat_function

 # create a function that is vectorized over the "upper" limit of your
 # integral
int_f <- Vectorize(function(f = awesome_thing, lower=0,upper,...){
    integrate(f,lower,upper,...)[['value']] },'upper')


ggplot(data.frame(x = c(0,10)),aes(x=x)) + 
   stat_function(fun = int_f, args = list(f = awesome_thing, lower=0))
mnel
  • 113,303
  • 27
  • 265
  • 254
  • Thanks mnel! this gives an error however "Error: unexpected '}' in "}"". What am I missing here? thanks! – Thomas Feb 03 '15 at 00:10
  • mnel, I need to integrate over a (VERY) long time period and am getting an error "error in integrate(f, lower, upper, ...) : maximum number of subdivisions reached". Can you tell me how I can add a subdivisions parameter here? I've tried adding it to the function(), integrate(), and list() like for f and lower, however it throws another error "Error in f(x, ...) : unused argument (0) Error in exists(name, envir = env, mode = mode) : argument "env" is missing, with no default" – Thomas Mar 17 '15 at 22:07
  • 1
    @Thomas, Naming the argument in the list works for me... `ggplot(data.frame(x = c(0,10)),aes(x=x)) + stat_function(fun = int_f, args = list(f = awesome_thing, lower = 0, subdivisions = 1e5))` – mnel Mar 17 '15 at 22:18
  • Thanks again mnel, you're correct, it works! my error was i was adding the argument in int_f as well. :) – Thomas Mar 17 '15 at 22:38
0

Not ggplot2 but shouldn't be difficult to adapt by creating a dataframe to pass to that paradgm:

 plot(x=seq(0.1,10, by=0.1), 
      y= sapply(seq(0.1,10, by=0.1) ,  
            function(x) integrate(awesome_thing, lower=0, upper=x)$value ) , 
         type="l")

The trick with the integrate function is that it retruns a list and you need to extract the 'value'-element for various changes in the upper limit.

IRTFM
  • 258,963
  • 21
  • 364
  • 487