如何在R中绘制旋转轴?

5 plot r

我想将六因素人格测试的结果作为一个循环来绘制.

有关的测试是Allgemeiner Interessen-Struktur-Test(AIST-R; Bergmann&Eder,2005)[一般兴趣结构测试],它根据JL Holland(荷兰代码,RIASEC)的理论测量职业选择.您可以使用下面的答案绘制手册中推荐的"Felddarstellung"[字段表示],而不是兴趣特征,以更好地可视化差异向量.

生成的图形看起来应该类似于:

在此输入图像描述

测试结果以角度和长度给出.

  • 如何从具有特定长度的起点在R中绘制轴或几何向量,而不定义结束坐标(根据需要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))
Run Code Online (Sandbox Code Playgroud)

jba*_*ums 9

以下使用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, ...)
}
Run Code Online (Sandbox Code Playgroud)

get.coords接受参数a(角度的矢量), d(段长度的矢量),并且x0y0,已知点的坐标.载体ad根据需要进行回收.该函数返回data.frame带有元素的a ,xy给出与每个角度/长度对相对应的坐标.

rotatedAxis绘制一个轴x0, y0和点之间的轴d沿着直线的角度a.如果symmetricalTRUE,则轴d以相反方向延伸单元.高度ticklen的刻度线以tickdist单位为单位绘制.

圆的绘图get.coords用于计算沿圆周的坐标,并绘制连接这些坐标的线polygon(受@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)
Run Code Online (Sandbox Code Playgroud)

(编辑:我大量编辑了这篇文章 - 没有大幅改变它的一般信息 - 为了使其更容易阅读和更普遍适用.添加/更改包括我现在定义一个函数来绘制旋转轴,通过计算坐标绘制圆形图沿着圆周的顶点并polygon用@timriffe的灵感绘制.)

在此输入图像描述

  • 可以通过更改为"ticks.locs < - lapply(seq(60,360,60),get.coords,d = 10*(1:6))"来调整刻度的比例.为了与我的其余代码兼容,我们必须进行一些其他调整.我没时间解释代码,但会回来.您的机器上的曲线看起来也是波浪状的吗?我总是遇到Windows上的抗锯齿问题(请注意,即使我的多边形有波浪线) - 认为它在其他人的系统上看起来还不错. (2认同)

小智 5

一个解决方案基于托马斯的评论和jbaums的回答.

  • 我使用jbaums的方法绘制轴,因为我不想要plotrix提供的完整的圆形网格.
  • 我没有使用jbaums的方法绘制圆圈,因为它有一条波浪/凹凸不平的线条.
  • 我打了par(new = TRUE)两次电话,因为jbaums答案中的比例是真实比例的十分之一,我无法弄清楚如何调整它.
  • 我手动放置了标签,我不满意.
  • 那里还有很多多余的代码,但我留下它以防万一有人想用它来处理自己的版本.

这是代码:

# test results
R <- 95
I <- 93
A <- 121
S <- 111
E <- 114
C <- 80

dimensions <- c("R", "I", "A", "S", "E", "C")
values <- c(R, I, A, S, E, C)

RIASEC   <- data.frame(
                "standard.values" = values,
                "RIASEC" = dimensions
            )

person.typ   <- paste(
                    head(
                        RIASEC[
                            with(
                                RIASEC,
                                order(-standard.values)
                            ),
                        ]$RIASEC,
                        3
                    ),
                    collapse = ""
                )

# length of vector
vi1 <- 0
vi2 <- I
va1 <- 0.8660254 * A
va2 <- 0.5 * A
vs1 <- 0.8660254 * S
vs2 <- -0.5 * S
ve1 <- 0
ve2 <- -E
vc1 <- -0.8660254 * C
vc2 <- -0.5 * C
vr1 <- -0.8660254 * R
vr2 <- 0.5 * R
vek1 <- va1 + vi1 + vr1 + vc1 + ve1 + vs1  # x-axix
vek2 <- vr2 + vi2 + va2 + vs2 + ve2 + vc2  # y-axis
vektor <- sqrt(vek1^2 + vek2^2)            # vector length

# angle of vector
if (vek1 == 0) {tg <- 0} else {tg <- vek2 / vek1}
wink <- atan(tg) * 180 / pi
if (vek1 > 0) {
    winkel <- 90 - wink
} else if (vek1 == 0) {
    if (vek2 >= 0) {winkel <- 360}
    else if (vek2 < 0) {winkel <- 180}
} else if (vek1 < 0) {
    if (vek2 <= 0) {winkel <- 270 - wink}
    else if (vek2 >= 0) {winkel <- 270 - wink}
}

library(plotrix)
axis.angle <- c(0, 60, 120, 180, 240, 300)
axis.rad <- axis.angle * pi / 180
value.length <- values - 70
dev.new(width = 5, height = 5)
radial.plot(value.length, axis.rad, labels = dimensions, start = pi-pi/6, clockwise=TRUE,
    rp.type="p", poly.col = "grey", show.grid = TRUE, grid.col = "transparent", radial.lim = c(0,60))
radial.plot.labels(value.length + c(4, 2, -2, 1, 1, 4), axis.rad, radial.lim = c(0,60), start = pi-pi/6, clockwise = TRUE, labels = values, pos = c(1,2,3,1,2,1))

get.coords <- function(a, d, x0=0, y0=0) {
    a <- ifelse(a <= 90, 90 - a, 450 - a)
    data.frame(x = x0 + d * cos(a / 180 * pi), y = y0+ d * sin(a / 180 * pi)  )
}
par(new = TRUE)
plot(NA, xlim = c(-6, 6), ylim=c(-6, 6), type='n', xlab='', ylab='', asp = 1,
     axes=FALSE, new = FALSE, bg = "transparent")
circumf.pts <- get.coords(seq(60, 360, 60), 6)
segments(circumf.pts$x[1:3], circumf.pts$y[1:3],
         circumf.pts$x[4:6], circumf.pts$y[4:6])
ticks.locs <- lapply(seq(60, 360, 60), get.coords, d=1:6)

ticks <- c(apply(do.call(rbind, ticks.locs[c(1, 4)]), 1, function(x)
             get.coords(150, c(-0.1, 0.1), x[1], x[2])),
           apply(do.call(rbind, ticks.locs[c(2, 5)]), 1, function(x)
             get.coords(30, c(-0.1, 0.1), x[1], x[2])),
           apply(do.call(rbind, ticks.locs[c(3, 6)]), 1, function(x)
             get.coords(90, c(-0.1, 0.1), x[1], x[2])))

lapply(ticks, function(x) segments(x$x[1], x$y[1], x$x[2], x$y[2]))

par(new = TRUE)
plot(NA, xlim = c(-60, 60), ylim=c(-60, 60), type='n', xlab='', ylab='', asp = 1,
     axes=FALSE, new = FALSE, bg = "transparent")
segments(0, 0, vek1, vek2, lwd=3)
points(vek1, vek2, pch=20, cex=2)
symbols(c(0, 0, 0), c(0, 0, 0), circles = c(60, 2, 1.3), inches = FALSE, add = TRUE, fg = c("black", "white", "black"), bg = c("transparent", "white", "black"))
Run Code Online (Sandbox Code Playgroud)

这是图形:

在此输入图像描述