尽管改变纵横比,但从角度绘制线段

Pas*_*cal 20 r

我试图在R中绘制一个表,列名相对于表的角度.我想添加行来分隔这些列名称,与文本的角度相同.但是,看起来text()函数中指定的角度与绘图的纵横比无关,而我在segments()函数中使用的角度取决于绘图的纵横比.

这是我的意思的一个例子:

nRows <- 5
nColumns <- 3
theta <- 30

rowLabels <- paste('row', 1:5, sep='')
colLabels <- paste('col', 1:3, sep='')

plot.new()
par(mar=c(1,8,5,1), xpd=NA)
plot.window(xlim = c(0, nColumns), ylim = c(0, nRows), asp = 1)
text(labels = rowLabels, x=0, y=seq(from=0.5, to=nRows, by=1), pos=2)
text(labels = colLabels, x = seq(from = 0.4, to = nColumns, by = 1), y = nRows + 0.1, pos = 4, srt = theta, cex = 1.1)
segments(x0 = c(0:nColumns), x1 = c(0:nColumns), y0 = 0, y1 = nRows, lwd = 0.5)
segments(x0 = 0, x1 = nColumns, y0 = 0:nRows, y1 = 0:nRows, lwd = 0.5)

#column name separators, angle converted to radians
segments(x0 = 0:(nColumns - 1), x1 = 1:nColumns, y0 = nRows, y1 = nRows + tan(theta * pi/180), lwd = 0.5)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

但是,如果我希望能够根据自己的喜好调整此绘图窗口的大小而不指定asp,则角度不再匹配:

nRows <- 5
nColumns <- 3
theta <- 30

rowLabels <- paste('row', 1:5, sep='')
colLabels <- paste('col', 1:3, sep='')

plot.new()
par(mar=c(1,8,5,1), xpd=NA)
plot.window(xlim = c(0, nColumns), ylim = c(0, nRows))
text(labels = rowLabels, x=0, y=seq(from=0.5, to=nRows, by=1), pos=2)
text(labels = colLabels, x = seq(from = 0.4, to = nColumns, by = 1), y = nRows + 0.1, pos = 4, srt = theta, cex = 1.1)
segments(x0 = c(0:nColumns), x1 = c(0:nColumns), y0 = 0, y1 = nRows, lwd = 0.5)
segments(x0 = 0, x1 = nColumns, y0 = 0:nRows, y1 = 0:nRows, lwd = 0.5)

#column name separators, angle converted to radians
segments(x0 = 0:(nColumns - 1), x1 = 1:nColumns, y0 = nRows, y1 = nRows + tan(theta * pi/180), lwd = 0.5)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

有没有办法指定一个设定角度,这样当我调整窗口大小时图形看起来正确?

bgo*_*dst 2

30 弧度的值theta是数据空间角度。它仅适用于数据空间计算,例如绘制segments()对角线的调用。

图形srt参数指定设备空间中的文本旋转,这意味着文本将按照物理设备上的指定角度进行渲染,而不管底层绘图区域的纵横比如何。

数据和设备空间之间的关系是动态确定的,并受到许多因素的影响:

  • 设备尺寸(GUI 窗口客户端区域大小或目标文件大小)。
  • 图形多重性(如果使用多重图;请参阅mfrowmfcol图形参数)。
  • 任何内部和外部边距(大多数地块都有内部边距,外部边距很少)。
  • 任何内部间距(参见xaxsyaxs图形参数)。
  • 绘图范围 (xlimylim)。

执行所需操作的正确方法是(1)动态查询以设备空间距离单位测量的数据空间纵横比,以及(2)使用它theta从数据空间角度转换为设备空间角度。

1:长宽比查询

我们可以通过沿 x 轴查找 1 个数据空间单位的设备空间等效项来计算长宽比,对 y 轴执行相同操作,然后获取比率y/x。函数grconvertX()grconvertY()就是为此目的而设计的。

calcAspectRatio <- function() abs(diff(grconvertY(0:1,'user','device'))/diff(grconvertX(0:1,'user','device')));
Run Code Online (Sandbox Code Playgroud)

转换函数对单个坐标进行操作,而不是对距离进行操作。但它们是矢量化的,所以我们可以通过0:1对输入坐标系中相距1个单位的两个坐标进行转换,然后diff()在输出坐标系中取a得到等效的单位距离。

您可能想知道为什么abs()需要拨打电话。对于许多图形设备,y 轴向下而不是向上增加,因此较小的数据空间坐标将转换为较大的设备空间坐标。因此,在这些情况下,第一次调用的结果diff()将为负。理论上,x 轴永远不会发生这种情况,但为了abs()以防万一,我们也可以将整个商包装在调用中。

2:将 theta 从数据空间转换到设备空间

这里可以采用几种数学方法,但我认为最简单的是采用tan()角度的 来获得三角y/x比,将其乘以纵横比,然后使用 转换回角度atan2()

dataAngleToDevice <- function(rad,asp) {
    rad <- rad%%(pi*2); ## normalize to [0,360) to make following ops easier
    y <- abs(tan(rad))*ifelse(rad<=pi,1,-1)*asp; ## derive y/x trig ratio with proper sign for y and scale by asp
    x <- ifelse(rad<=pi/2 | rad>=pi*3/2,1,-1); ## derive x component with proper sign
    atan2(y,x)%%(pi*2); ## use atan2() to derive result angle in (-180,180], and normalize to [0,360)
}; ## end dataAngleToDevice()
Run Code Online (Sandbox Code Playgroud)

顺便说一句,我发现这是一个非常有趣的数学变换。角度0、90、180、270不受影响,这是有道理的;纵横比的变化不应影响这些角度。垂直伸长将角度拉向 y 轴,水平伸长将角度拉向 x 轴。至少我是这样想象的。


因此,将所有这些放在一起,我们得到了以下解决方案。请注意,我重写了您的代码以使其更加简洁,并做了一些细微的更改,但大部分都是相同的。dataAngleToDevice()显然,最重要的变化是我添加了对around 的调用theta,并传递了第二个参数calcAspectRatio()。此外,我使用了较小(字体方向)但较长(字符串方向)的列名称来更清楚地展示文本的角度,我将文本移动到更靠近对角线的位置,我从一开始就以弧度存储theta,并且我对内容进行了一些重新排序。

nRows <- 5;
nColumns <- 3;
theta <- 30*pi/180;

rowLabels <- paste0('row',1:5);
colLabels <- do.call(paste,rep(list(paste0('col',1:3)),5L));

plot.new();
par(mar=c(1,8,5,1),xpd=NA);
plot.window(xlim=c(0,nColumns),ylim=c(0,nRows));
segments(0:nColumns,0,0:nColumns,nRows,lwd=0.5);
segments(0,0:nRows,nColumns,0:nRows,lwd=0.5);
text(0,seq(0.5,nRows,1),rowLabels,pos=2);
## column name separators
segments(0:(nColumns-1),nRows,1:nColumns,nRows+tan(theta),lwd=0.5);
text(seq(0.3,nColumns,1),nRows+0.1,colLabels,pos=4,srt=dataAngleToDevice(theta,calcAspectRatio())*180/pi);
Run Code Online (Sandbox Code Playgroud)

这是一个长宽比大致为正方形的演示:

大致正方形

宽的:

宽的

而且高:

高的


我画了一个转变的情节:

xlim <- ylim <- c(0,360);
xticks <- yticks <- seq(0,360,30);
plot(NA,xlim=xlim,ylim=ylim,xlab='data',ylab='device',axes=F);
box();
axis(1L,xticks);
axis(2L,yticks);
abline(v=xticks,col='grey');
abline(h=yticks,col='grey');
lineParam <- data.frame(asp=c(1/1,1/2,2/1,1/4,4/1),col=c('black','darkred','darkblue','red','blue'),stringsAsFactors=F);
for (i in seq_len(nrow(lineParam))) {
    x <- 0:359;
    y <- dataAngleToDevice(x*pi/180,lineParam$asp[i])*180/pi;
    lines(x,y,col=lineParam$col[i]);
};
with(lineParam[order(lineParam$asp),],
    legend(310,70,asp,col,title=expression(bold(aspect)),title.adj=c(NA,0.5),cex=0.8)
);
Run Code Online (Sandbox Code Playgroud)

数据角度到设备

  • 非常感谢如此完整的解决方案! (2认同)