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
  • 47,778
  • 59
  • 172
  • 269

3 Answers3

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
  • 12,513
  • 15
  • 63
  • 87
6

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,471
  • 1
  • 24
  • 38
  • 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
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,142
  • 3
  • 41
  • 63