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

后端 未结 6 1747
灰色年华
灰色年华 2020-11-30 12:45

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

相关标签:
6条回答
  • 2020-11-30 12:53

    Given that its possible to write your own plot functions using base graphics, I can't see how a single solution could exist. Is what you want really just a way to rep lace x data with y data? What exactly do you mean by "rotate"?

    0 讨论(0)
  • 2020-11-30 12:57

    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)
    

    0 讨论(0)
  • 2020-11-30 12:58

    A function rotate_plot to be used like

    rotate_plot(some_base_plot(x, y, ...))
    

    isn't possible because most of the base plot don't return a value.

    Some of the plots contain a horiz argument to allow you to choose which way round you want the plot drawing. Take a look at barplot.default to see how to implement this. (Warning: it's messy.)

    @ucfagls's suggestion to use gridBase is your best bet. There are some examples of its use in Appendix B of Murrell's R Graphics.

    0 讨论(0)
  • 2020-11-30 13:05

    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.

    0 讨论(0)
  • 2020-11-30 13:07

    you could export the graphic, read it back in, and display it rotated as a rasterGrob, say, (or a rasterImage after rotating the matrix, or a grImport grob if you want vector paths)

    plot(1:10, rnorm(10))
    library(grid)
    cap <- grid.cap()
    grid.newpage()
    grid.raster(cap, vp=viewport(angle=30))
    

    The new gridGraphics package may now be a better alternative.

    Note: this doesn't seem to work with Rstudio's graphics device, presumably they haven't implemented grid.cap.

    enter image description here

    0 讨论(0)
  • 2020-11-30 13:14

    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)
    
    0 讨论(0)
提交回复
热议问题