在R中绘制椭圆/双曲线 [英] drawing ellipses/hyperbolas in R

查看:1208
本文介绍了在R中绘制椭圆/双曲线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在R中绘制椭圆和双曲线。
如何使用ggplot2来做到这一点?
让我举个例子

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


推荐答案

构建数据字段:

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)

制作每个级别的基本 geom_contour 图不同颜色:

Make a basic geom_contour plot with each level coloured differently:

g1 <- qplot(x,y,z=z,data=d,colour=factor(..level..),geom="contour")

现在摆脱所有行0级别。这取决于 stat_contour 选择零作为其轮廓线之一,在某些情况下可能会很脆弱......(这对我来说并不明显, stat_contour ()允许控制选择哪些级别,相当于级别参数< contour()或 contourLines()在基础R中 - 如果有的话,这个过程会容易一些。 ://github.com/hadley/ggplot2/issuesrel =nofollow noreferrer> https://github.com/hadley/ggplot2/issues ,如果对此有足够的关心......) p>

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")

这篇关于在R中绘制椭圆/双曲线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆