r - 如何在R中绘制旋转轴?

标签 r plot

我想将六因素性格测试的结果绘制为一个循环。

所讨论的测试是 Allgemeiner Interessen-Struktur-Test (AIST-R; Bergmann & Eder, 2005) [General Interest Structure Test],它根据 J. L. Holland (Holland codes, RIASEC) 的理论衡量职业选择。您可以使用下面的答案来绘制手册中推荐的“Felddarstellung”[字段表示],而不是兴趣剖面,以更好地可视化微分向量。

生成的图形应类似于以下内容:

enter image description here

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

  • 如何从具有特定长度的起点在 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))
    

    最佳答案

    以下使用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 (段长度的向量)和 x0y0 ,已知点的坐标。矢量 ad必要时回收。该函数返回 data.frame带元素 xy给出对应于每个角度/长度对的坐标。
    rotatedAxisx0, y0 之间绘制一个轴和点 d单位距离沿线的角度 a .如 symmetricalTRUE , 轴延伸 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 的启发。)

    enter image description here

    关于r - 如何在R中绘制旋转轴?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22064611/

    相关文章:

    r - 将不同的 grViz 组合成一个图

    r - ifelse 具有多个条件,用于在 data.table R 中创建新变量

    r - 使用 source 与 parse 和 eval 相比有哪些注意事项?

    r - 想要使用 R 在列中组合日期和时间

    python - 限制 Matplotlib 图例最佳位置选项

    r - 如何在R中对这些数据进行排序

    r - 绘制两种正态分布混合的密度曲线

    python - 带有由 c 选项指定的颜色标签和图例的 matplotlib 散点图

    matlab - 我们如何在金融烛台图表上绘制红点(代表交易)?

    r - 如何水平对齐图(ggplot2)?