簡體   English   中英

用R旋轉繪圖網格

[英]Rotate grid of plot with R

例如 :

plot(1:10, 1:10)
grid(col="red")

是否可以圍繞x軸和y軸(原點{0,0})的交點旋轉紅色網格任意角度? 這個例子並不意味着什么,但我想這樣做。

無論如何,網格函數可能無法在基數R中執行此操作。 它不是一個面向對象的繪圖范例。 我想您會abline

plot(1:10, 1:10,xlim=c(0,10), ylim=c(0,10))
sapply(seq(0,20,by=2), function(a) abline(a=a, b=-1,lty=3,col="red"))
sapply(seq(-10,10,by=2), function(a) abline(a,b=1,lty=3,col="red"))

在此處輸入圖片說明

這是坐標幾何的一個小應用,可以旋轉任意角度。

angle=pi/8; rot=tan(angle); backrot=tan(angle+pi/2)
sapply(seq(-10,10,by=2), function(inter) abline(a=inter, 
                                                b=rot,lty=3,col="red"))
sapply(seq(0,40,by=2), function(inter) abline(a=inter, 
                                           b=backrot, lty=3,col="red"))

當angle = pi / 2時,它的確崩潰了,因此如果要構建函數,則可能要檢查一下,在這種情況下,只需使用grid 我發現的一個問題是使間距令人滿意。 如果以相同的間隔在y軸和x軸上進行迭代,則會“壓縮”一組網格線之一。 我認為這就是為什么該方法在大角度下分解的原因。

我在想一個更通用的解決方案,可能是構建一組柵格端點,該端點以至少sqrt(2)的因數跨越並超出繪圖區域,然后應用旋轉矩陣。 然后使用segmentslines 這是該實現:

plot(1:10, 1:10,xlim=c(0,10), ylim=c(0,10)); angle=pi/8; rot=tan(angle);backrot=tan(angle+pi/2)
x0y0 <- matrix( c(rep(-20,41), -20:20), 41)
x1y1 <- matrix( c(rep(20,41), -20:20), 41)
# The rot function will construct a rotation matrix
rot <- function(theta) matrix(c( cos( theta ) , sin( theta ) ,
 -sin( theta ), cos( theta ) ), 2)
# Leave origianal set of point untouched but create rotated version
 rotx0y0 <- x0y0%*%rot(pi/8)
 rotx1y1 <- x1y1%*%rot(pi/8)
 segments(rotx0y0[,1] ,rotx0y0[,2], rotx1y1[,1], rotx1y1[,2], col="blue")
# Use originals again, ... or could write to rotate the new points
 rotx0y0 <- x0y0%*%rot(pi/8+pi/2)
 rotx1y1 <- x1y1%*%rot(pi/8+pi/2)
 segments(rotx0y0[,1] ,rotx0y0[,2], rotx1y1[,1], rotx1y1[,2], col="blue")

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM