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?

1
  • Do you mean radius and degrees? Commented Dec 12, 2011 at 10:58

3 Answers 3

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)

Sign up to request clarification or add additional context in comments.

1 Comment

Add , asp = 1 to the plot statement for the final image to display as a circle instead of an oval.
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

4 Comments

Thanks, that looks perfect, but I keep getting Error in match.arg(theta, c("x", "y")) : 'arg' should be one of “x”, “y”
the problem with this is that I can't specify location in degrees
Please post a reproducible example if you want more to the point feedback.
That example will work if coord_polar(theta = "dist") is changed to coord_polar(theta = "y")
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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.