6

Plotting a function in the wolfram-alpha-website looks like this:

enter image description here

http://www.wolframalpha.com/link

Plotting the same function in R looks like this:

plot( function(x) x^2 - 3*x - 10 ) enter image description here

The default plot from Wolfram is much easier to understand. I think this is because it shows the x-axis (at y=0), and centers the parabola.

I am not good enough at math to just look at the formula of a function and see where I should center the plot, and I am plotting the functions to learn about how different functions create different lines, so I need this centering to be done automatically, because otherwise I might misunderstand a plot.

Is it possible to create the Wolfram-plot automatically i.e. without me telling R where it would be sensible to center the plot?

Rasmus Larsen
  • 5,721
  • 8
  • 47
  • 79
  • 2
    there is definitely not a built-in way to do this in base R. It's an interesting challenge to design an algorithm that automatically figures out the interesting range of a function, for some definition of "interesting" ... – Ben Bolker Nov 27 '13 at 21:06
  • The `polynom` package offers some functionality here. – mnel Nov 27 '13 at 22:05
  • 2
    Picking domains and ranges automatically is challenging. Lee Wilkinson proposes one approach in http://www.cs.uic.edu/~wilkinson/Publications/plotfunc.pdf – hadley Nov 27 '13 at 22:13

3 Answers3

4

The polynom package will create some sensible defaults.

eg.

library(polynom)

# your polynomial (coefficients in ascending powers of x order)
p <- polynomial(c(-10,-3,1))
plot(p)

enter image description here

 # a more complicated example, a polynomial crossing the x axis at -1,0,1,2,3,4,5

 p2 <- poly.calc(-1:5)
 p2 
 # -120*x + 154*x^2 + 49*x^3 - 140*x^4 + 70*x^5 - 14*x^6 + x^7 
 plot(p2)

enter image description here

mnel
  • 113,303
  • 27
  • 265
  • 254
2

You can set the desired interval to plot over, as described in ?plot.function. Also see curve and abline.

plot( function(x) x^2 - 3*x - 10 , -15, 15) ; abline(h=0,v=0,lty=3)

or

curve(x^2 - 3*x - 10 , -15, 15) ; abline(h=0,v=0,lty=3)
Aaron left Stack Overflow
  • 36,704
  • 7
  • 77
  • 142
  • Thanks. I've edited the question so it is more clear that I am looking for a way to do this automatically. – Rasmus Larsen Nov 27 '13 at 20:14
  • 2
    As far as I know, R does not have built-in way to do this automatically. However, if you have some idea of what would be "sensible", a function could be written to change the plotting interval automatically. – Aaron left Stack Overflow Nov 27 '13 at 20:58
0

This is quite an old post post but I was trying to fit a polynomial curve based on the coefficients of my model.

The original in base R :

plot( y ~ x) 
curve(3*x - 2*x^2 + 2*x^3)   ##  random coefficients for easy example 

I use ggplot2 - so I wanted to use the curve generated from the coefficients rather than a + geom_smooth (This also works but I prefer the curve below)

bestfit <- geom_smooth(method = "loess", se = T, size = 1) 

ggplot2 
+ bestfit

Instead I created a function with the coefficients above

test 
test <- function(x) {3*x - 2*x^2 + 2*x^3} 

I have then added it as a layer in the ggplot

ggplot2
+ stat_function(fun = test)

It gives me the same curve as the base plot function but I can add all the additional layers in ggplot

Tunaki
  • 132,869
  • 46
  • 340
  • 423
Julie
  • 1