rotation - Rotate grid of plot with R -
for example :
plot(1:10, 1:10) grid(col="red")
is possible rotate red grid around intersection of x axis , y axis (the origin {0,0}) , arbitrary angle ? doesn't mean example want that.
the grid function cannot in base r anyway. it's not object-oriented plotting paradigm. think come close 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"))
it's minor application of coordinate geometry rotate arbitrary angle.
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"))
this break down when angle = pi/2 might want check if building function , in case use grid
. 1 problem found spacing pleasing. if 1 iterates on y- , x-axes same intervals "compression" of 1 of set of gridlines. think why method breaks down @ high angles.
i'm thinking more general solution might construct set of grid end-points spans , extends beyond plot area factor of @ least sqrt(2) , apply rotation matrix. , use segments
or lines
. here implementation:
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) # rot function construct rotation matrix rot <- function(theta) matrix(c( cos( theta ) , sin( theta ) , -sin( theta ), cos( theta ) ), 2) # leave origianal set of point untouched 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 write rotate 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")
Comments
Post a Comment