Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting curves given by equations in R

Tags:

r

graphics

In R, is there a way to plot 2D curves given by equations? For example, how can I plot the hyperbola given by equation x^2 - 3*y^2 + 2*x*y - 20 = 0?

like image 483
Leo Avatar asked Apr 17 '12 21:04

Leo


People also ask

How do I plot multiple curves in R?

To draw multiple curves in one plot, different functions are created separately and the curve() function is called repeatedly for each curve function. The call for every other curve() function except for the first one should have added an attribute set to TRUE so that multiple curves can be added to the same plot.

What does curve () do in R?

curve() function in R Language is used to draw a curve for the equation specified in the argument.

How is the plot () function is invoked?

The plot() function in R isn't a single defined function but a placeholder for a family of related functions. The exact function being called will depend upon the parameters used. At its simplest, plot() function simply plots two vectors against each other. This gives a simple plot for y = x^2.


1 Answers

Just for the record - a ggplot version

library(ggplot2)
library(dplyr)
f <- function(x,y) x^2 - 3*y^2 + 2*x*y - 20
seq(-10,+10,length=100) %>%
    expand.grid(x=.,y=.) %>%
    mutate(z=f(x,y)) %>%
    ggplot +
    aes(x=x,y=y,z=z) +
    stat_contour(breaks=0)

enter image description here

like image 167
Vlad Avatar answered Oct 14 '22 07:10

Vlad