How to build a circle with points inside it in R?

I need to build a circle centered at (0,0) in R. Then I would like to build points in this circle given in radius and degrees. Can someone point me in the right direction for this task?

+2
source share
3 answers

In the base chart:

 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))) 

Please note that at least one of the points will be on the border of the circle, so if you do not want this, you will replace the max(r) statutes with something like 1.1*max(r)

+3
source

To do this with ggplot2, you need to use coord_polar , ggplot2 will do all the transformations for you. 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

+2
source

Use the polar coordinate system. ( wiki link ).

then translate it into a Cartesian coordinate system ( link )

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

0
source

Source: https://habr.com/ru/post/1203632/


All Articles