以下用途base
函数和一些我们自己定义的函数。
虽然您请求一种不需要计算线段端点坐标的方法,但我认为这是不可能的。然而,我们可以定义一个简单的辅助函数,它使用一些基本的三角学来计算给定角度(从正 y 轴顺时针方向)和线段长度的坐标。我们在下面执行此操作,并定义一个绘制旋转轴的函数。
get.coords <- function(a, d, x0, y0) {
a <- ifelse(a <= 90, 90 - a, 450 - a)
data.frame(x = x0 + d * cos(a / 180 * pi),
y = y0+ d * sin(a / 180 * pi))
}
rotatedAxis <- function(x0, y0, a, d, symmetrical=FALSE, tickdist, ticklen, ...) {
if(isTRUE(symmetrical)) {
axends <- get.coords(c(a, a + 180), d, x0, y0)
tick.d <- c(seq(0, d, tickdist), seq(-tickdist, -d, -tickdist))
} else {
axends <- rbind(get.coords(a, d, x0, y0), c(x0, y0))
tick.d <- seq(0, d, tickdist)
}
invisible(lapply(apply(get.coords(a, d=tick.d, x0, y0), 1, function(x) {
get.coords(a + 90, c(-ticklen, ticklen), x[1], x[2])
}), function(x) lines(x$x, x$y, ...)))
lines(axends$x, axends$y, ...)
}
get.coords
接受参数a
(角度向量),d
(段长度向量),以及x0
and y0
,已知点的坐标。向量a
and d
必要时进行回收。该函数返回一个data.frame
有元素x
and y
给出每个角度/长度对对应的坐标。
rotatedAxis
在之间绘制一个轴x0, y0
和重点d
沿线以一定角度远离单位a
. If symmetrical
is TRUE
,轴延伸d
方向相反的单位。刻度线,高度ticklen
被绘制tickdist
单位分开。
绘制圆的用途get.coords
计算沿圆周的坐标,并绘制连接这些坐标的线polygon
(灵感来自@timriffe https://stackoverflow.com/a/9838248/489704).
下面我们使用这些函数来复制 OP 提供的绘图。
# Set up plotting device
plot.new()
plot.window(xlim=c(-70, 70), ylim=c(-70, 70), asp=1)
# Plot circle with radius = 60 units and centre at the origin.
polygon(get.coords(seq(0, 360, length.out=1000), 60, 0, 0), lwd=2)
# Plot a polygon with vertices along six axes, at distances of 17, 34, 44, 40,
# 35, and 10 units from the centre.
poly.pts <- get.coords(seq(0, 300, 60), c(17, 34, 44, 40, 35, 10), 0, 0)
polygon(poly.pts$x, poly.pts$y, col='gray', lwd=2)
# Plot the rotated axes
rotatedAxis(0, 0, a=60, d=60, symmetrical=TRUE, tickdist=10, ticklen=1)
rotatedAxis(0, 0, a=120, d=60, symmetrical=TRUE, tickdist=10, ticklen=1)
rotatedAxis(0, 0, a=180, d=60, symmetrical=TRUE, tickdist=10, ticklen=1)
# Add text labels to circumference
text.coords <- get.coords(seq(0, 300, 60), 65, 0, 0)
text(text.coords$x, text.coords$y, c('I', 'A', 'S', 'E', 'C', 'R'))
# Plot a second point and connect to centre by a line
point2 <- get.coords(145, 50, 0, 0)
points(point2, pch=20, cex=2)
segments(0, 0, point2$x, point2$y, lwd=3)
# Plot central point
points(0, 0, pch=21, bg=1, col=0, lwd=2, cex=2)
(Edit:我对这篇文章进行了大量编辑 - 没有彻底改变它的一般信息 - 以便使其更易于阅读和更普遍适用。添加/更改包括我现在定义一个函数来绘制旋转轴,通过计算沿圆周的顶点坐标并用polygon
,受到@timriffe 的启发。)