在R中画圆 [英] Drawing circles in R

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

问题描述

我正在使用plotrix软件包绘制圆圈.

I'm using plotrix package to draw circles.

我不明白我的代码有什么问题...:-(

And I don't get what is wrong with my code... :-(

我有三点.第一个点(1,1)应该是圆的中心.以下两个点(1,4)(4,1)到中心的距离/半径相同. 所以情节中的圆应该经过这些点,对吗?

I have three points. The first point (1,1) should be the center of the circle. The following two points (1,4) and (4,1) have the same distance/radius to the center. So the circle in the plot should go through these points, right?

我不知道为什么圆圈看起来不对.有解释吗?

And I don't know why the circle looks wrong. Is there an explanation?

  p1 <- c(1,1)
  p2 <- c(4,1)
  p3 <- c(1,4)
  r <- sqrt(sum((p1-p2)^2))

  plot(x=c(p1[1], p2[1], p3[1]),
       y=c(p1[2], p2[2], p3[2]), 
       ylim=c(-5,5), xlim=c(-5,5))
  draw.circle(x=p1[1], y=p1[2], radius=(r))
  abline(v=-5:5, col="#0000FF66")
  abline(h=-5:5, col="#0000FF66")

看看产生的输出此处

推荐答案

正如@Baptiste所说,您可以使用plot(...,asp=1).不过,这仅在x和y范围恰好相同时才起作用(因为它将图的 physical 纵横比设置为1).否则,您可能要使用MASS包中的eqscplot函数.每当您尝试仔细绘制几何对象时,都会出现类似的问题. 绘制不相交的圆圈

As @Baptiste says above, you can use plot(...,asp=1). This will only work if your x and y ranges happen to be the same, though (because it sets the physical aspect ratio of your plot to 1). Otherwise, you probably want to use the eqscplot function from the MASS package. A similar issue arises whenever you try to do careful plots of geometric objects, e.g. Drawing non-intersecting circles

此图是通过在上面的代码中将MASS::eqscplot替换为plot产生的:

This plot is produced by substituting MASS::eqscplot for plot in your code above:

请注意,根据R对显示器配置的看法等细节,在R的图形窗口中作图时,圆看起来可能有点挤(即使它穿过了点),但对我来说确实如此- -但应该在图形输出中看起来不错.

Note that depending on the details of what R thinks about your monitor configuration etc., the circle may look a bit squashed (even though it goes through the points) when you plot in R's graphics window -- it did for me -- but should look OK in the graphical output.

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

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