如何使用plot()删除圆周上的水平线

问题描述 投票:1回答:1

我想绘制一个圆周。在圆周上绘制点的功能是:

d_cir = function (a=1){
x = seq(-a,a,.005)
y = sqrt(a^2-x^2)
x = matrix(c(x,x), ncol=1)
y = matrix(c(y,-y), ncol=1)
matrix(c(x, y), ncol = 2)
}

然后我绘图:

plot(d_cir(), asp = 1, type="l")

enter image description here

如何从绘图中删除水平线?谢谢,曼努埃尔

r plot
1个回答
2
投票

一个小的调整。您希望c(x,x)从-1到1到1到-1,因此您需要第二次反转x的顺序。由于y是对称的y和rev(y)是相同的,因此您无需将其反转:

d_cir = function (a){
x = seq(-a,a,.005)
y = sqrt(a^2-x^2)
x = matrix(c(x, rev(x)), ncol=1)
y = matrix(c(y,-y), ncol=1)
matrix(c(x, y), ncol = 2)
}
plot(d_cir(1), asp = 1, type="l")

这也做同样的事情,但是代码要简单一些:

d_cir <- function (a){
     x <- seq(-a, a, .005)
     y <- sqrt(a^2 - x^2)
     x <- c(x, rev(x))
     y <- c(y, -y)
     cbind(x, y)
}
plot(d_cir(1), asp = 1, type="l")
© www.soinside.com 2019 - 2024. All rights reserved.