qplot and anti-aliasing in R

后端 未结 4 647
被撕碎了的回忆
被撕碎了的回忆 2021-02-07 03:47

I am using ggplot2 library and am working with the qplot command I know I can save my output as an anti-aliased image file by using the following command after my qplot

相关标签:
4条回答
  • 2021-02-07 04:13

    As others have mentioned, R's built-in Windows graphics device does not do anti-aliasing. But nowadays it's easy to install the Cairo graphics device which does.

    At the R console:

    install.packages('Cairo',,'http://www.rforge.net/')
    

    To test:

    plot(rnorm(1000)) # non-antialiased (on Windows)
    library('Cairo')
    CairoWin()
    plot(rnorm(1000)) # antialiased!
    

    More

    0 讨论(0)
  • 2021-02-07 04:19

    On Windows, there is no built-in anti-aliasing. I don't know whether it is planned for future releases or not. You can get a Cairo-based graphics device from either the cairoDevice or Cairo packages; however, you will need to install GTK+ first:

    Download and install Gtk+ 2.12.9 Runtime Environment Revision 2 from http://gladewin32.sourceforge.net/

    Another option would be to use Java-based graphics through JGR (http://jgr.markushelbig.org/). Also a Qt-based device is under development, I think.

    0 讨论(0)
  • 2021-02-07 04:29

    Ok, I just checked. I was wrong in my earlier comment. From help(x11) where a lot of detail is available -- the new Cairo-based devices do have anti-aliasing available:

    x11 package:grDevices R Documentation

    X Window System Graphics

    Description:

     ‘X11’ starts a graphics device driver for the X Window System
     (version 11).  This can only be done on machines/accounts that
     have access to an X server.
    
     ‘x11’ is recognized as a synonym for ‘X11’.
    

    Usage:

     X11(display = "", width, height, pointsize, gamma, bg, canvas,
         fonts, xpos, ypos, title, type, antialias)
    
     X11.options(..., reset = FALSE)
    

    Arguments:

    [...]

     fonts: X11 font description strings into which weight, slant and
          size will be substituted.  There are two, the first for fonts
          1 to 4 and the second for font 5, the symbol font.  See
          section ‘Fonts’. 
    

    [...]

     antialias: for cairo types, the typeof anti-aliasing (if any) to be
          used.  One of ‘c("default", "none", "gray", "subpixel")’. 
    

    [...]

    Details:

     The defaults for all of the arguments of ‘X11’ are set by
     ‘X11.options’: the ‘Arguments’ section gives the ‘factory-fresh’
     defaults.
    
     The initial size and position are only hints, and may not be acted
     on by the window manager.  Also, some systems (especially laptops)
     are set up to appear to have a screen of a different size to the
     physical screen.
    
     Option ‘type’ selects between two separate devices: R can be built
     with support for neither, ‘type = "Xlib"’ or both.  Where both are
     available, types ‘"cairo"’ and ‘"nbcairo"’ offer
    
        * antialiasing of text and lines.
    
        * translucent colours.
    
        * scalable text, including to sizes like 4.5 pt.
    
        * full support for UTF-8, so on systems with suitable fonts you
          can plot in many languages on a single figure (and this will
          work even in non-UTF-8 locales).  The output should be
          locale-independent.
    
     ‘type = "nbcairo"’ is the same device as ‘type="cairo"’ without
     buffering: which is faster will depend on the X11 connection.
     Both will be slower than ‘type = "Xlib"’, especially on a slow X11
     connection as all the rendering is done on the machine running R
     rather than in the X server.
    
     All devices which use an X11 server (including the ‘type = "Xlib"’
     versions of bitmap devices such as ‘png’) share internal
     structures, which means that they must use the same ‘display’ and
     visual.  If you want to change display, first close all such
     devices. 
    

    [...and more...]

    0 讨论(0)
  • 2021-02-07 04:37

    If you have Cairo installed (see the other answers), to save it as an anti-aliased PNG, just change your code to:

    ggsave(file="filename.png", type="cairo-png")

    as specified here.

    But for what purpose do you want to "see a plot on the monitor as anti-aliased graph" or "anti-alias my plot windows"? If you mean like in the Plots window (tab) in RStudio, I am not sure that can be done, it serves basically just as a preview. I suggest you save the graph to a file and then use this file to display it or for any other purpose.

    0 讨论(0)
提交回复
热议问题