Drawing circles in R
Asked Answered
M

1

5

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

Moxie answered 27/9, 2012 at 10:23 Comment(1)
set the aspect ratio asp in ?plotCanea
K
7

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.

Kast answered 27/9, 2012 at 12:20 Comment(2)
Thanks! The MASS eqscplot function is close to what I need. Still, it would be nice to be ablte to define own xlim and ylim.Moxie
The tricky part is that you're typically juggling (1) the dimensions of the plotting device (typically user-adjustable, but not adaptive from within R's graphic framework); (2) dimensions of the figure region within the plotting region (depends on margins and sizes of titles, annotations, etc.); (3) user x- and y- limits. With care, you can get these the way you want them, but it's a bit hard to get it all adjusted automa[gt]ically ...Kast

© 2022 - 2024 — McMap. All rights reserved.