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

时间:2016-01-18 19:24:29

标签: 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)

enter image description here

但是,如果我希望能够根据自己的喜好调整此绘图窗口的大小而不指定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)

enter image description here

有没有办法指定设定角度,以便在调整窗口大小时图形看起来正确?

1 个答案:

答案 0 :(得分:3)

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

srt图形参数指定设备空间中的文本旋转,这意味着无论底层绘图区域的纵横比如何,文本都将呈现为遵循物理设备上的指定角度。

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

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

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

1:查询宽高比

我们可以通过在x轴上找到相当于1个数据空间单位的设备空间来计算纵横比,对y轴执行相同的操作,然后取比率y/x。函数grconvertX() and grconvertY()是为此目的而创建的。

calcAspectRatio <- function() abs(diff(grconvertY(0:1,'user','device'))/diff(grconvertX(0:1,'user','device')));

转换功能在各个坐标上运行,而不是距离。但它们是矢量化的,因此我们可以通过0:1转换输入坐标系中相距1个单位的两个坐标,然后取diff()得到输出坐标系中的等效单位距离。 / p>

您可能想知道为什么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()

简而言之,我认为这是一个非常有趣的数学变革。角度0,90,180和270不受影响,这是有道理的;纵横比的变化不应影响这些角度。垂直伸长率朝向y轴拉动角度,并且水平伸长率朝向x轴拉动角度。至少我是如何看待它的。

所以,把这一切放在一起,我们有以下解决方案。请注意,我重写了您的代码以获得更简洁并进行了一些小的更改,但大多数情况都是相同的。显然,最重要的变化是我在dataAngleToDevice()附近调用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);

这是一个大致方形宽高比的演示:

roughly-square

宽:

wide

高个子:

tall

我制作了转型图:

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

data-angle-to-device