Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to rotate a plot in R (base graphics)?

Tags:

plot

r

rotation

I searched for this and found that with {grid} there are ways to rotate an image, and that for some plots you can play with their rotation (for example plot(x,y) instead of plot(y,x)).

However, I want to know if there is a generic method to rotate a plot in R (one that would work for ANY plot generated in base graphics) ?

like image 455
Tal Galili Avatar asked Sep 25 '10 06:09

Tal Galili


3 Answers

It's kind of possible via the gridGraphics package, although it feels a bit rough on the edges (the examples in ?grid.echo don't all work for me),

plot(1:10, rnorm(10))

library(gridGraphics)

grab_grob <- function(){
  grid.echo()
  grid.grab()
}

g <- grab_grob()
grid.newpage()
pushViewport(viewport(width=0.7,angle=30))
grid.draw(g)

enter image description here

like image 194
baptiste Avatar answered Oct 23 '22 18:10

baptiste


I'm reasonably certain that there isn't a way with base graphics itself to do this generically. There is however the gridBase package which allows one to mix base graphics and grid graphics in a 'plot'. The vignette for the package has a section on embedding base graphics in grid viewports, so you might look there to see if you can cook up a grid wrapper around your plots and use grid to do the rotation. Not sure if this is a viable route but is, as far as I know, the on only potential route to an answer to your Q.

gridBase is on CRAN and the author is Paul Murrell, the author of the grid package.

After browsing the vignette, I note one of the bullets in the Problems and Limitations section on page, which states that it is not possible to embed base graphics into a rotated grid viewport. So I guess you are out of luck.

like image 30
Gavin Simpson Avatar answered Oct 23 '22 17:10

Gavin Simpson


Spinning 3D Scatterplots

You can also create an interactive 3D scatterplot using the plot3D(x, y, z) function in the rgl package. It creates a spinning 3D scatterplot that can be rotated with the mouse. The first three arguments are the x, y, and z numeric vectors representing points. col= and size= control the color and size of the points respectively.

# Spinning 3d Scatterplot
library(rgl)

plot3d(wt, disp, mpg, col="red", size=3)
like image 26
phil Avatar answered Oct 23 '22 19:10

phil