Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
521 views
in Technique[技术] by (71.8m points)

plot - Drawing circles in R

I'm using plotrix package to draw circles.

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

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

Take a look at the produced output here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

enter image description here

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...