I am a graphics programmer from the GKS days trying to use R graphics. I have two questions that relate to transformations in R:
-
I was wondering if there is an equivalent for building a viewing pipeline in R where one could map a window in world coordinates [wc] to a viewport in device coordinates [dc]. For example I could specify a transofrmation t which maps a window of (wcxmin, wcxmax, wcymin, wcymax) to (vpxmin, vpxmax, vpymin, vpymax) where wc is (1000, -50, 40, 90) and vp is (0, 800, 0, 600). The objective being that all graphics calculations are done in wc but the graphics engine renders it in dc. In this case it would scale the coordinates appropriately and also flip the x-axis as wcxmin > wcxmax.
-
Is there an equivalent of graphics segments which could then be transformed [sclae, shift, rotate, and possibly shear] via a transformation matrix.
I am sure I am missing something very basic in R graphics. I could successfully build such transforms in SVG without any issues. I have been looking at packages like grid, lattice, ggplot2 but have not been able to make much progress.
Thanks.
Here’s some sample code for something I am trying to do:
distn<-rnorm(100)
distw<-rweibull(100, shape=2)
dret<-stack(list(norm=distn, weib=distw))
n<-0
for (idx in levels(dret$ind)) {
pct<-dret[dret$ind == idx,c('values')]
# scale and shift the data
pct<-(pct-min(pct))/(max(pct) - min(pct))
if (n == 0) {
# top left
par(fig=c(0,0.5,0.5,1))
limx<-c(0,1)
} else {
# bottom right
par(fig=c(0.5,1,0,0.5), new=TRUE)
limx<-c(1,0)
}
fp<-density(pct)
sfx<-fp$x
sfy<-(fp$y-min(fp$y))/(max(fp$y)-min(fp$y))
sortpct<-sort(pct)
ecdfpct<-(1:length(sortpct))/length(sortpct)
plot(sortpct, ecdfpct, xlim=limx, type="l", col="green")
lines(sfx, sfy, xlim=limx, type="l", col="red")
n<-n+1
}
I would like to rotate the figure in the bottom right quadrant by -90 degrees.
The ‘grid’ package does that all the time. The viewports are represented as [0, 1] in both X and Y directions(and sometimes Z) and the functions
convertXandconvertYare called to move from user-coordinates to grid-coordinates. Typehelp(grid)for a full list of facilities. A third dimension is also represented when using wireframe or levelplot. Transformations via homogeneous coordinates are accomplished via 4 x 4 matrices stored as an item accessed ascurrent.transform( current.viewport()). You can get more detail regarding how those transformation matrices are handled in R by looking at the code intrans3d. I see that @nograpes has already pointed you to the high-level rotation facility in thegrid::pushViewportfunction.