我想将六因素性格测试的结果绘制为一个循环。
所讨论的测试是 Allgemeiner Interessen-Struktur-Test (AIST-R; Bergmann & Eder, 2005) [General Interest Structure Test],它根据 J. L. Holland (Holland codes, RIASEC) 的理论衡量职业选择。您可以使用下面的答案来绘制手册中推荐的“Felddarstellung”[字段表示],而不是兴趣剖面,以更好地可视化微分向量。
生成的图形应类似于以下内容:
测试结果以角度和长度给出。
arrows
所要求)? 我当然可以计算端点,但我想避免这种情况。另外,我不知道如何在箭头上添加刻度线。
我的尝试无效:
par(pin = c(4, 4))
plot(0, 0, type = "n", xlim = c(-60, 60), ylim = c(-60, 60))
symbols(c(0, 0, 0), c(0, 0, 0), circles = c(60, 1.5, 1.5), inches = FALSE, add = TRUE, fg = c("black", "black", "white"), bg = c("transparent", "#000000", "transparent"))
arrows(0, 0, length = c(60, 60, 60, 60, 60, 60), angle = c(0, 60, 120, 180, 240, 300))
最佳答案
以下使用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
和 y0
,已知点的坐标。矢量 a
和 d
必要时回收。该函数返回 data.frame
带元素 x
和 y
给出对应于每个角度/长度对的坐标。rotatedAxis
在 x0, y0
之间绘制一个轴和点 d
单位距离沿线的角度 a
.如 symmetrical
是 TRUE
, 轴延伸 d
方向相反的单位。刻度线,高度 ticklen
已绘制 tickdist
单位分开。绘制圆使用
get.coords
计算沿圆周的坐标,并绘制连接这些与 polygon
的线( inspired by @timriffe )。下面我们使用这些函数来复制 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)
( 编辑: 我大量编辑了这篇文章 - 没有彻底改变它的一般信息 - 为了使其更易于阅读和更普遍适用。添加/更改包括我现在定义了一个函数来绘制旋转轴,通过计算沿圆周的顶点坐标并使用
polygon
绘制圆,受到@timriffe 的启发。)关于r - 如何在R中绘制旋转轴?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22064611/