如何在R中绘制旋转轴? [英] How to draw rotated axes in R?

查看:62
本文介绍了如何在R中绘制旋转轴?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

所讨论的测试是 Allgemeiner Interessen-Struktur-Test(AIST-R;Bergmann & Eder,2005)[一般兴趣结构测试],它根据以下理论衡量职业选择JL Holland().

下面我们使用这些函数来复制 OP 提供的绘图.

#设置绘图设备情节.new()plot.window(xlim=c(-70, 70), ylim=c(-70, 70), asp=1)# 绘制半径 = 60 个单位且以原点为中心的圆.多边形(get.coords(seq(0, 360, length.out=1000), 60, 0, 0), lwd=2)# 绘制一个多边形,顶点沿六个轴,距离为 17, 34, 44, 40,# 35,距离中心 10 个单位.poly.pts <- get.coords(seq(0, 300, 60), c(17, 34, 44, 40, 35, 10), 0, 0)多边形(poly.pts$x,poly.pts$y,col='gray',lwd=2)# 绘制旋转轴旋转轴(0、0、a=60、d=60、对称=真、tickdist=10、ticklen=1)旋转轴(0、0、a=120、d=60、对称=真、tickdist=10、ticklen=1)旋转轴(0、0、a=180、d=60、对称=真、tickdist=10、ticklen=1)# 给圆周添加文字标签text.coords <- get.coords(seq(0, 300, 60), 65, 0, 0)文本(text.coords$x, text.coords$y, c('I', 'A', 'S', 'E', 'C', 'R'))# 绘制第二个点并通过一条线连接到中心point2 <- get.coords(145, 50, 0, 0)点(点2,pch=20,cex=2)段(0、0、point2$x、point2$y、lwd=3)# 绘制中心点点(0、0、pch=21、bg=1、col=0、lwd=2、cex=2)

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

I want to plot the results from a six factor personality test as a circumplex.

The test in question is the Allgemeiner Interessen-Struktur-Test (AIST-R; Bergmann & Eder, 2005) [General Interest Structure Test], which measures vocational choice based on the theory of J. L. Holland (Holland codes, RIASEC). You can use the answers below to plot the "Felddarstellung" [field representation] recommended in the manual in stead of the interest profile to better visualize the vector of differentiation.

The resulting graphic should look similar to this:

The test results are given as angles and lengths.

  • How can I draw an axis or geometric vector in R from a starting point with a specific length, without defining the end coordinates (as required by arrows)?

  • How can I add tickmarks to such a vector?

  • How can I define the points of a polygon (here in grey) in a similar manner, i.e. by providing an angle and a distance from the origin, instead of coordinates)?

I can of course calculate the endpoints, but I would like to avoid this. Also, I wouldn't know how to add tick marks to an arrow.


My attempts that did not work:

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))

解决方案

The following uses base functions and a couple of functions that we define ourselves.

While you requested a method that doesn't require calculating coordinates of segments' end points, I think this is impossible. However, we can define a simple helper function that uses some basic trigonometry to calculate the coordinates given the angle (clockwise from the positive y-axis) and the segment length. We do this below, as well as defining a function that plots a rotated axis.

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 takes arguments a (a vector of angles), d (a vector of segment lengths), and x0 and y0, the coordinates of the known point. Vectors a and d are recycled as necessary. The function returns a data.frame with elements x and y giving the coordinates corresponding to each angle/length pair.

rotatedAxis plots an axis between x0, y0 and the point d units away along the line at angle a. If symmetrical is TRUE, the axis extends d units in opposite directions. Tick marks, of height ticklen are plotted tickdist units apart.

Plotting of the circle uses get.coords to calculate coordinates along the circumference, and plots the line connecting these with polygon (inspired by @timriffe).

Below we use these functions to replicate the plot provided by the 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: I heavily edited this post - without changing it's general message drastically - in order to make it easier to read and more generally applicable. Additions/changes include that I now define a function to plot rotated axes, plot the circle by calculating coordinates of vertices along the circumference and plotting with polygon, as inspired by @timriffe.)

这篇关于如何在R中绘制旋转轴?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆