Reputation: 159
I want to draw ellipses, hyperbolas in R. How can I do it using ggplot2? Let me give an example
x^2 +3xy+ 4x+ 2y+ 2y^2 = 0
for
x between -2 and 2
y between -2 and 2
Upvotes: 2
Views: 2243
Reputation: 226182
Construct a data field:
d <- transform(expand.grid(x=seq(-2,2,length=51),
y=seq(-2,2,length=51)),z=x^2+3*x*y+4*x+2*y+2*y^2)
Make a basic geom_contour
plot with each level coloured differently:
g1 <- qplot(x,y,z=z,data=d,colour=factor(..level..),geom="contour")
Now get rid of the lines for all but the "0" level. This depends on stat_contour
picking zero as one of its contour levels, which might be fragile under some circumstances ... (It's not obvious to me that stat_contour()
allows control of what levels are chosen, equivalent to the levels
argument to contour()
or contourLines()
in base R -- if it did, this process would be a little bit easier. One might submit a wishlist item to https://github.com/hadley/ggplot2/issues , if one cared enough about this ...)
g1 + scale_colour_discrete(breaks="0",limits=c("0","0"),na.value=NA,
guide="none")
Upvotes: 6