2

I need to plot a circle centered at (0,0) in R. Then I would like to plot points in that circle specified in radius and degrees. Could anyone point me in the right direction for this task?

wfbarksdale
  • 7,498
  • 15
  • 65
  • 88

3 Answers3

4

In base graphics:

r <- 3*runif(10)
degs <- 360*runif(10)

# First you want to convert the degrees to radians

theta <- 2*pi*degs/360

# Plot your points by converting to cartesian

plot(r*sin(theta),r*cos(theta),xlim=c(-max(r),max(r)),ylim=c(-max(r),max(r)))

# Add a circle around the points

polygon(max(r)*sin(seq(0,2*pi,length.out=100)),max(r)*cos(seq(0,2*pi,length.out=100)))

Note that at least one of the points will be on the border of the circle, so if you dont want this you would have replace of the max(r) statments with something like 1.1*max(r)

James
  • 65,548
  • 14
  • 155
  • 193
2

To do this with ggplot2, you need to use coord_polar, ggplot2 will do all the transformations for you. An example in code:

library(ggplot2)
# I use the builtin dataset 'cars'
# Normal points plot
ggplot(aes(x = speed, y = dist), data = cars) + geom_point() 

enter image description here

# With polar coordinates
ggplot(aes(x = speed, y = dist), data = cars) + geom_point() + 
     coord_polar(theta = "dist")

enter image description here

Paul Hiemstra
  • 59,984
  • 12
  • 142
  • 149
0

Use a polar system of coords.(link to wiki).

then transle it into Cartesian coordinate system (link)

and then translate to screen coords ( for example 0,0 is a center of your monitor)

Yuriy Vikulov
  • 2,469
  • 5
  • 25
  • 32