[R] plotting circles
Ross Ihaka
ihaka at stat.auckland.ac.nz
Mon May 28 07:11:38 CEST 2001
Here is quick polar scatterplot function (attached) which might give you
some ideas. This is just a sketch, but shows how to do the basics.
(It contains a number of circles.)
--
Ross Ihaka Email: ihaka at stat.auckland.ac.nz
Department of Statistics Phone: (64-9) 373-7599 x 5054
University of Auckland Fax: (64-9) 373-7018
Private Bag 92019, Auckland
New Zealand
-------------- next part --------------
# Polar coordinate plot.
# It is assumed that "r" is given in radians.
polar.plot <-
function(r, theta,
pch = NULL,
col = NULL,
angle.axis = -90)
{
## Determine the range of data value.
## Choose pretty cutpoints which include the range.
rpretty <- pretty(range(abs(r), 0, na.rm=TRUE))
rmax <- rpretty[length(rpretty)]
## Begin a new plot.
plot.new()
## Set up the plot coordinates.
## We choose a square region which includes a circle
## large enough to include all the data.
## Note the use of asp = 1, so that circles will
## appear as circles, even if a graphics window is resized.
plot.window(xlim = c(-rmax, rmax),
ylim = c(-rmax, rmax),
asp = 1)
## Draw a circular grid.
## (a) The circles are really polygons with many vertices.
## (b) The magic number "5" is choosen because a change of
## a change of direction of less than 5 degrees appears smooth.
grid <- seq(0, 2 * pi, length = 360 / 5 + 1)
for(rad in rpretty) {
if(rad > 0)
lines(rad * cos(grid), rad * sin(grid), col = "gray")
}
## Draw a radial grid in "gray".
## The use of "12" gives divisions into 30 degree slices.
rad <- seq(0, 2 * pi, length = 12 + 1)[-1]
segments(0, 0, rmax * cos(rad), rmax * sin(rad), col = "gray")
## Basic axis labelling.
## Labels appear along the ray at angle "angle.axis" to the x axis.
text(rpretty[-1] * cos(angle.axis * pi / 180),
rpretty[-1] * sin(angle.axis * pi / 180),
rpretty[-1])
## Plot the data values - this is the easy bit.
points(r * cos(theta),
r * sin(theta),
col = col, pch = pch)
}
## Example plot for fake data
group <- sample(3, 100, replace = TRUE)
theta <- 0.5 * rnorm(100) + 0.5 * group
r <- rnorm(100, 4)
polar.plot(r, theta,
col = c("red","green4","blue")[group],
pch = 20)
More information about the R-help
mailing list