Drawing non-intersecting circles
Asked Answered
D

1

18

I'm trying to plot two non-intersecting touching circles, but I think I'm missing something quite basic...

jpeg(file="test.jpg")
diam <- sqrt (2)
plot (c(-1,1), c(1,-1), xlim=c(-5,5), ylim=c(-5,5))

symbols (c(-1,1), c(1,-1), circles=c(diam,diam), add=TRUE, inches=FALSE)
dev.off()

Can anyone explain to me why these circles overlap?

alt text

Dharana answered 3/1, 2011 at 19:53 Comment(0)
D
16

Set the aspect ratio via asp:

diam <- sqrt (2)
plot (c(-1,1), c(1,-1), xlim=c(-3,3), ylim=c(-3,3), asp=1)
symbols (c(-1,1), c(1,-1), circles=c(diam,diam), add=TRUE, inches=FALSE)

Updated to add Gavin Simpson's excellent insights from the comments and chat. My answer may be correct, but Gavin provides the very helpful reasons why asp=1 works and why it isn't the default behavior. Many thanks to him.

The default plotting device settings attempt to display the data without assuming anything about the scale of the relationship between the variables. To directly quote Gavin:

The reason asp = 1 is not the default is that asp = 1 doesn't make sense for data that do not share a common unit of measurement, such as height vs weight. Why should a change of 1m in height be represented as a change of 1kg in weight?

and

As a result, distance along the x axis bears no relationship to those on the y axis. As such, what is plotted is a transformation of real circles - they really are circles, just translated because the coordinate system you are plotting them into isn't appropriate.

A way to illustrate Gavin's points would be to plot the circles on the default device (not the jpeg device), then re-size the device. You can make the circles look all sorts of weird.

Denominational answered 3/1, 2011 at 20:7 Comment(7)
You're welcome. I glanced at ?plot and "'asp' the y/x aspect ratio, see 'plot.window'" looked promising. ?plot.window contained the details.Denominational
You are right, it's definately written there. Having seen this plot behavior, I guess I'm not sure why asp=1 isn't the default. I'm sure there is some logical reason, but right now this logic escapes me.Dharana
So for the record, the logic of asp=NA as default is to scale to x and y axes to min and max values rather than "natural" scale. It's all coming clear now. See stat.ethz.ch/pipermail/r-help/2010-August/249812.htmlDharana
@OneWhoIsUnnamed; I don't think that is strictly true; the limits are min and max plus/minus a small fudge factor (4% IIRC). This is due to parameter xaxs and yaxs being set to "r" (for regular); see ?par. The reason asp = 1 is not the default is that asp = 1 doesn't make sense for data that do not share a common unit of measurement, such as height vs weight. Why should a change of 1m in height be represented as a change of 1kg in weight? So the idea is to display the data as best as possible assuming there is no special relationship between the variables plotted.Rinarinaldi
@Gavin Simpson; Thanks for the clarification and your work, I'm starting to understand the issue more and more. Please forgive me for repeated query, but why in my scaleless example do the circles overlap? I understand that asp=1 will fix this, but I guess I'm still unclear why on this plot the distance between the centers of the two circle is not twice the radius of the circles. I guess this suggest that the circles are not really circles?Dharana
@OneWhoIsUnnamed: In you example, the plotting region isn't square and the aspect ratio is not 1. As a result, distance along the x axis bears no relationship to those on the y axis. As such, what is plotted is a transformation of real circles - they really are circles, just translated because the coordinate system you are plotting them into isn't appropriate. What is drawn isn't a circle, but the numbers created by symbols do represent a circle.Rinarinaldi
In short, symbols with circles= will always draw something that looks like a perfect circle to the user, even if its not a circle in the coordinate space of the plot. If you want a circle in plot coordinates, then plot rsin(t),rcos(t) for t from 0 to 2*pi in small steps. That won't look like a circle when you stretch the plot though! (Unless asp=1 of course!)Evslin

© 2022 - 2024 — McMap. All rights reserved.