I'd like to draw equation : (x^2 + y^2 -1)^3 - x^2*y^3 = 0 in R (with passing x as argument) - is is possible and if so - how ? I tried to use function (x) but found too hard to transform it so y was only on one side of it.
-
Why do you want to do this in R? – Hugh Jun 29 '14 at 11:57
2 Answers
Since f(x,y)
is not single valued, a contour plotting approach is probably better. Here's one way with ggplot
.
f <- function(x,y) (x^2 + y^2 -1)^3 - x^2*y^3
x <- seq(-2,2,0.01)
df <- expand.grid(x=x,y=x)
df$z <-with(df,f(x,y))
library(ggplot2)
ggplot(df,aes(x=x, y=y, z=z))+stat_contour(geom="path",breaks=0, col="red")
Setting breaks=0
in the call to stat_contour(...)
causes only the one contour where z=0
to be plotted.
And here's a way using base R (starting with df
defined above).
library(reshape2)
m <- dcast(df,x~y,value.var="z")[-1]
contour(x,x,as.matrix(m),levels=0, col="red")

- 58,004
- 7
- 97
- 140
This isn't a single-valued function of x.
Try x=0
, then it becomes (y^2 -1)^3 = 0
, which is true if y is plus or minus one. If x=1
, then y is 0 or 1.
Its a bit like x^2 + y^2 = 1
, which is a circle, which you can plot by transforming it to a function of r and theta and then r is a single-valued function of theta (from 0 to 360 degrees).
Wolfram Alpha loves you:
http://www.wolframalpha.com/input/?i=%28x%5E2+%2B+y%5E2+-1%29%5E3+-+x%5E2+y%5E3+%3D+0
but doesn't give a parametric form, nor is it given here:
http://mathworld.wolfram.com/HeartCurve.html
So you might be better off asking if there's a parametric form on the maths stackexchange site...

- 92,590
- 12
- 140
- 224
-
@Spacedmans last link reminded me of [this](http://stackoverflow.com/questions/8082429/plot-a-heart-in-r) which can also be usefull (many good answers there) – David Arenburg Jun 29 '14 at 12:22