[R] how to draw a circle

Prof Brian Ripley ripley at stats.ox.ac.uk
Sat Apr 22 22:05:17 CEST 2006


On Sat, 22 Apr 2006, John Fox wrote:

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

The solution via symbols is preferable, because drawing a circle is a 
primitive of the R graphics model, and symbols is the only way to get 
to it in base graphics (cf grid.circle).  As a result is optimized to look 
good (the PDF device uses a dingbat for small circles, for example).

>
> 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
>
> ______________________________________________
> 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
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list