[R] Drawing a circle on an existing graph.

Greg Snow Greg.Snow at imail.org
Sun Mar 13 04:59:29 CET 2011


If you are always using asp=1 then you can just do something like:

x <- rnorm(100, 1, 5)
y <- rnorm(100, 3, 4)  

plot( x,y, asp=1)

r=2
nseg=360
x.cent <- 5
y.cent <- 7

xx <- x.cent + r*cos( seq(0,2*pi, length.out=nseg) )
yy <- y.cent + r*sin( seq(0,2*pi, length.out=nseg) )

lines(xx,yy, col='red')


-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Allan Kamau
> Sent: Saturday, March 12, 2011 1:08 PM
> To: r-help at r-project.org
> Subject: Re: [R] Drawing a circle on an existing graph.
> 
> On Sat, Mar 12, 2011 at 10:22 PM, David Winsemius
> <dwinsemius at comcast.net> wrote:
> >
> > On Mar 12, 2011, at 10:56 AM, Allan Kamau wrote:
> >
> >>> --- On Sat, 3/12/11, Allan Kamau <kamauallan at gmail.com> wrote:
> >>>
> >>>> From: Allan Kamau <kamauallan at gmail.com>
> >>>> Subject: [R] Drawing a circle on an existing graph.
> >>>> To: r-help at r-project.org
> >>>> Received: Saturday, March 12, 2011, 3:08 AM
> >>>> I would like to draw a circle on a
> >>>> graph I am plotting in R and use
> >>>> the current plot's coordinate system.
> >>>> The most basic functionality I am looking for is the
> >>>> ability to plot a
> >>>> circle on an already existing graph by simply providing the
> >>>> xy
> >>>> coordinates for it's centre and the radius.
> >>>>
> >>>> I am also looking for a way to obtain x,y values of a point
> >>>> (of a
> >>>> circle) by specifying only the origin x,y the angle and
> >>>> radius.
> >>>>
> >>>> Please point me so such a library and perhaps some simple
> >>>> one line code.
> >>>>
> >>>> Allan.
> >>>>
> >> On Sat, Mar 12, 2011 at 5:28 PM, John Kane <jrkrideau at yahoo.ca>
> wrote:
> >>>
> >>> Is this of any use?
> >>> http://tolstoy.newcastle.edu.au/R/help/06/04/25821.html
> >>>
> >>
> >> Thank you John, that is what I was looking for.
> >> The code does draw a neat circle that is not stretched. After
> plotting
> >> the circle on an existing plot I notice that the original plot was
> >> indeed stretched somehow by approximately some 1:1.1 ratio (I
> think).
> >> Now I am looking for a way to enforce none stretching in my original
> >> graph.
> >
> > `plot.window` has an `asp` argument that is useful for that purpose.
> Most
> > plotting with base graphics will pass an asp=1 argument along as part
> of the
> > `...` arguments,
> >
> > --
> >
> > David Winsemius, MD
> > West Hartford, CT
> >
> >
> Thank you David, asp=1 argument in my plot function brought about 1:1
> aspect ratio to the graph and now the circle sits very neatly within
> it. All is well.
> John is there another version of your circle drawing code that accepts
> values in accordance with the current plot's scale units in place of
> the current real distance measurement (inches) units.
> 
> Allan.
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list