[R] how to draw a circle
John Fox
jfox at mcmaster.ca
Sat Apr 22 21:03:45 CEST 2006
Dear Jian,
Here's an answer to the first part of what I understood to be your original
question, which entails drawing a circle of a given (absolute) size in cm
(i.e., regardless of the physical dimensions of the device and the units of
the axes):
circle <- function(x, y, radius, units=c("cm", "in"), segments=100){
units <- match.arg(units)
if (units == "cm") radius <- radius/2.54
plot.size <- par("pin")
plot.units <- par("usr")
units.x <- plot.units[2] - plot.units[1]
units.y <- plot.units[4] - plot.units[3]
ratio <- (units.x/plot.size[1])/(units.y/plot.size[2])
size <- radius*units.x/plot.size[1]
angles <- (0:segments)*2*pi/segments
unit.circle <- cbind(cos(angles), sin(angles))
shape <- matrix(c(1, 0, 0, 1/(ratio^2)), 2, 2)
ellipse <- t(c(x, y) + size*t(unit.circle %*% chol(shape)))
lines(ellipse)
}
Regards,
John
--------------------------------
John Fox
Department of Sociology
McMaster University
Hamilton, Ontario
Canada L8S 4M4
905-525-9140x23604
http://socserv.mcmaster.ca/jfox
--------------------------------
> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Jian Zhang
> Sent: Saturday, April 22, 2006 12:43 PM
> To: r-help
> Subject: [R] how to draw a circle
>
> how to draw a circle (e.g. radius=10cm) of one point?
> And how to choose these points in the circle?
> Thanks!
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
More information about the R-help
mailing list