在R中绘制圆

在R中绘制圆,r,plot,R,Plot,我不知道为什么下面的代码没有给出完整的循环,只给出部分循环。另外,我不知道如何在圆上或圆外以(0,0)为中心,r=1和a=2的正方形内显示我的点 library("plotrix") n<-1000 plot.new() frame() x<-runif(n,-1,1) y<-runif(n,-1,1) for (i in 1:n) { plot(x[i],y[i])} draw.circle(0,0,1,nv=1000,border=NULL,col=NA,lty=1,lwd

我不知道为什么下面的代码没有给出完整的循环,只给出部分循环。另外,我不知道如何在圆上或圆外以(0,0)为中心,r=1和a=2的正方形内显示我的点

library("plotrix")
n<-1000
plot.new()
frame()
x<-runif(n,-1,1)
y<-runif(n,-1,1)
for (i in 1:n) { plot(x[i],y[i])}
draw.circle(0,0,1,nv=1000,border=NULL,col=NA,lty=1,lwd=1)

多亏了费尔南多,我修正了这个图,现在看起来像这样,但我希望x的范围是(-1到1),就像y的范围一样。xlim没有工作。你知道怎么了吗

magnitude = function(x, y) {
  stopifnot(isTRUE(all.equal(length(x),length(y))))
  return (sqrt(x^2 + y^2))
}
library("plotrix")
monte.carlo.pi<-function(n,draw=FALSE)
{
  circle.points<-0
  square.points<-0
  x<-runif(n,-1,1)
  y<-runif(n,-1,1)
  for (i in 1:n)
  {
    #if ((x[i])^2 + (y[i])^2 <=1)
    if (magnitude(x[i],y[i])<=1)
    {
      circle.points<-circle.points+1
      square.points<-square.points+1
    } else
    {
      square.points<-square.points+1
    }
  }
  if (draw==TRUE)
  {
    plot.new()
    frame()
    plot(x,y,asp=1,xlim=c(-1,1),ylim=c(-1,1))
    draw.circle(0,0,1,nv=1000,border=NULL,col=NA,lty=1,lwd=1)
    rect(-1,-1,1,1)
    return(4*circle.points / square.points)
  }
}
当前绘图如下所示:

您需要指定
asp=1

x = runif(100, -1, 1)
y = runif(100, -1, 1)
plot(x, y, asp = 1, xlim = c(-1, 1))
draw.circle(0, 0, 1, nv = 1000, border = NULL, col = NA, lty = 1, lwd = 1)

编辑:只是一个旁注,您可以使您的蒙特卡罗函数更有效:

mc.pi = function(n) {

  x = runif(n, -1, 1)
  y = runif(n, -1, 1)
  pin = sum(ifelse(sqrt(x^2 + y^2 <= 1), 1, 0))
  4 * pin/n
}
mc.pi=函数(n){
x=runif(n,-1,1)
y=runif(n,-1,1)

pin=sum(ifelse(sqrt)(x^2+y^2Fernando的答案很好,如果你想让这个圆在用户看来看起来像一个圆。这个答案包括在数据维度中画一个圆

如果x轴和y轴的比例相同,例如。, 如果将纵横比设置为1(
asp=1
),则这两种方法是等效的

# initialize a plot
plot(c(-1, 1), c(-1, 1), type = "n")

# prepare "circle data"
radius = 1
center_x = 0
center_y = 0
theta = seq(0, 2 * pi, length = 200) # angles for drawing points around the circle

# draw the circle
lines(x = radius * cos(theta) + center_x, y = radius * sin(theta) + center_y)

正如Gregor已经指出的,在绘制圆时,必须区分x和y是否具有相同的比例。 如果x和y具有相同的比例,我更喜欢用它在R中绘制一个圆。这样做不需要指定顶点,也不需要额外的库

n <- 1000
set.seed(0)
x <- runif(n, -1, 1)
y <- runif(n, -1, 1)

#x and y have the same scale -> Circle
plot(x, y, asp=1)
symbols(x=0, y=0, circles=1, inches=F, add=T)

#In case x and y don't have the same scale -> Ellipse
#Use Gregor's Answer
plot(x,y)
radius <- 1
theta <- seq(0, 2 * pi, length = 200)
lines(x = radius * cos(theta), y = radius * sin(theta))

#Using plotrix
library("plotrix")
plot(x, y, asp=1)
draw.circle(x=0, y=0, radius=1)

plot(x,y)
draw.ellipse(x=0, y=0, a=1, b=1)

n您只绘制了最后一点,请尝试在不使用for循环的情况下绘制(x,y)。我不确定,但请尝试
plot(x,y,asp=1)
。如果出现错误,请检查此项。@Fernando您能看一下更新后的问题吗?当然,我会看一看。对于我来说,xlim工作正常,但如果使用RStudio“缩放”按钮,x轴将展开(可能是RStudio的一个bug,我不确定)。我想你想要
sqrt(x^2+y^2)这是创建圆所涉及的数学中的一个很好的提醒。+1关于数据和视觉坐标之间的区别。我想知道是否有更直接的方法在数据坐标中绘制圆。当然,只需添加
x=h+radius*cos(θ),y=k+半径*sin(θ)
绘制以
(h,k)
为中心的圆,实际上解释
θ
很有用,嗨@MichaelChirico:P
# initialize a plot
plot(c(-1, 1), c(-1, 1), type = "n")

# prepare "circle data"
radius = 1
center_x = 0
center_y = 0
theta = seq(0, 2 * pi, length = 200) # angles for drawing points around the circle

# draw the circle
lines(x = radius * cos(theta) + center_x, y = radius * sin(theta) + center_y)
n <- 1000
set.seed(0)
x <- runif(n, -1, 1)
y <- runif(n, -1, 1)

#x and y have the same scale -> Circle
plot(x, y, asp=1)
symbols(x=0, y=0, circles=1, inches=F, add=T)

#In case x and y don't have the same scale -> Ellipse
#Use Gregor's Answer
plot(x,y)
radius <- 1
theta <- seq(0, 2 * pi, length = 200)
lines(x = radius * cos(theta), y = radius * sin(theta))

#Using plotrix
library("plotrix")
plot(x, y, asp=1)
draw.circle(x=0, y=0, radius=1)

plot(x,y)
draw.ellipse(x=0, y=0, a=1, b=1)