【发布时间】:2016-01-18 19:24:29
【问题描述】:
我正在尝试在 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)
但是,如果我希望能够根据自己的喜好调整此绘图窗口的大小而不指定 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)
有没有办法指定一个设定的角度,这样当我调整窗口大小时图形看起来正确?
【问题讨论】:
-
如果可以的话,在网格中可能更容易实现
-
我不认为这是可能的(虽然我可能错了)因为
asp的工作方式。来自plot.window帮助文件:“如果 asp 是有限正值,则设置窗口,以便 x 方向上的一个数据单元的长度等于 asp * y 方向上的一个数据单元”现在,如果您不要指定 asp,y1 = nRows + tan(theta * pi/180)中断,因为弧度取决于通过窗口大小调整缩放的半径,与角度无关(保持不变)。所以,我没有在 base R 中找到解决方案,但我再次希望得到证明! -
选择一个
asp并在制作标题行时将theta与x1 = 1:nColumns * asp一起缩放
标签: r