【发布时间】:2018-05-16 13:21:33
【问题描述】:
在基础 R(和sp)中,我希望创建具有预定义形状但以提供的坐标为中心的灵活旋转的箭头。我想出了以下功能:
my_arrow <- function(x,y, rotate=0, col=par("fg"), cex=1) {
xbase <- c(1.2,0.2,0.2,-1.2, -1.2, 0.2, 0.2)
ybase <- c(0,1,0.5,0.5,-0.5,-0.5,-1)
rotM <- matrix(c(cos(rotate*pi/180), sin(rotate*pi/180), -sin(rotate*pi/180), cos(rotate*pi/180)), nrow=2)
transf <- function(x1,y1) cex * rotM %*% c(x1,y1) + c(x,y)
ans <- t(mapply(transf, xbase, ybase))
polygon(x=ans[,1], y=ans[,2], col=col)
}
如果rotation=0,这会产生我想要的箭头,但是当我旋转时它会变形。例如,
plot(1:2, type="p", col="white", xlim=c(-5,5), ylim=c(-10,10))
my_arrow(0,0, rotate=45)
生成下面的图表。
我想我需要应用一些特殊类型的坐标,但我被卡住了。有什么想法吗?
(arrows 函数对我不起作用,因为我想到了另一种形状。使用gridBase 和一些旋转的视口对我来说听起来有点矫枉过正。)
【问题讨论】:
标签: r graphics coordinate-systems