2

I found the plotrix package in R but cannot yet find how to do this simple circle in R. Basically, how can I do a polar-plot with radius of 1 and 0:360 angles in degree, generating a circle?

Example

$$r\cos\left(\frac{2\pi}{3}\left(\frac{3\theta}{2\pi}-\left\lfloor\frac{3\theta}{2\pi}\right\rfloor\right) -\frac{\pi}{3}\right) = 1$$

Perhaps related

  1. Trying to plot the above function, more here, the LaTex with this hack here visible.

  2. Draw a circle with ggplot2

  3. Regular polygons in polar coordinates

Community
  • 1
  • 1
hhh
  • 50,788
  • 62
  • 179
  • 282

3 Answers3

7

You can also make circles using geometry

circle <- function(x, y, rad = 1, nvert = 500, ...){
    rads <- seq(0,2*pi,length.out = nvert)
    xcoords <- cos(rads) * rad + x
    ycoords <- sin(rads) * rad + y
    polygon(xcoords, ycoords, ...)
}

# asp = 1 due to Hans' comment below- wouldn't let me leave a comment just saying 'thanks'
plot(-5:5, type="n", xlim = c(-5,5), ylim = c(-5,5), asp = 1)
circle(0,0,4)
circle(-1.5,1.5,.5)
circle(1.5,1.5,.5)
circle(0,0,1)
segments(-2,-2,2,-2)
tim riffe
  • 5,651
  • 1
  • 26
  • 40
  • 4
    You'd better not forget the aspect ratio, `asp = 1`, when calling the plot command to see that it's really a circle. – Hans W. Mar 23 '12 at 11:40
6

You can easily get polar coordinate graphs in ggplot2.

From the ggplot2 website:

library(ggplot2)    

cxc <- ggplot(mtcars, aes(x = factor(cyl))) + 
           geom_bar(width = 1, colour = "black") 

cxc <- cxc + coord_polar()

print(cxc)

enter image description here

Etienne Low-Décarie
  • 13,063
  • 17
  • 65
  • 87
3

You can do very nice circular statistics with package circular. Below is one of examples from the package:

require(circular)
x <- circular(runif(50, 0, 2*pi))
rose.diag(x, bins = 18, main = 'Uniform Data',col=2)
points(x)

enter image description here

Geek On Acid
  • 6,330
  • 4
  • 44
  • 64