qplot和R中的反锯齿技术

20

我正在使用ggplot2库,使用qplot命令。我知道可以通过在qplot之后使用以下命令将输出保存为反锯齿图像文件:

ggsave(file="filename.png")

但是我的液晶显示器怎么办?有没有办法在监视器上看到抗锯齿图形的绘制?


我认为这是你和你的窗口管理器之间的事情。 - Dirk Eddelbuettel
8
有没有一种方法可以让R在绘制窗口时进行抗锯齿处理? - Mark
4个回答

17

正如其他人所提到的,R 内置的 Windows 图形设备不支持反锯齿。但现在可以很容易地安装 Cairo 图形设备来实现反锯齿。

在 R 控制台中:

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

进行测试:

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

更多


14

在Windows上,没有内置的抗锯齿功能。我不知道未来版本是否有计划加入此功能。您可以从cairoDeviceCairo软件包中获取基于Cairo的图形设备; 但是,您需要先安装GTK+

http://gladewin32.sourceforge.net/下载并安装Gtk+ 2.12.9 Runtime Environment Revision 2

另一个选择是通过JGRhttp://jgr.markushelbig.org/)使用基于Java的图形。我认为还有一种基于Qt的设备正在开发中。


2
我在Windows 7上使用R 3.1.1 64位安装了Cairo,而无需安装GTK +。抗锯齿效果很好。 - jnm2

5
如果您已安装了Cairo(参见其他答案),要将其保存为抗锯齿PNG,请将您的代码更改为: 如此指定在这里
但是,您想要“在监视器上看到抗锯齿图形的情况”或者“使我的绘图窗口抗锯齿”有什么目的呢? 如果您的意思是像RStudio中的Plots窗口(选项卡)一样,我不确定是否可以实现,它基本上只作为预览。 我建议您将图表保存到文件中,然后使用此文件来显示它或用于任何其他目的。

我建议使用 type = "cairo" - Liang Zhang

4

好的,我刚刚检查了一下。我之前的评论是错误的。从help(x11)中可以看到有很多详细信息 -- 新的基于Cairo的设备确实支持抗锯齿功能:

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...]


好的,现在我有一个问题:当我输入X11(type="cairo")时,会出现错误,显示"Error in X11(type = "cairo") : unused argument(s) (type = "cairo")"。我已经加载了cairo,但不知道如何更改我的X11。我正在使用Win XP上的R 2.9.2。我需要放弃整个系统并购买Mac才能在我的显示器上获得抗锯齿效果吗? - Mark
我有点担心那部分。我知道Cairo来自Gnome/Gtk,并且在Linux上非常适合,但我认为这是跨平台的。当您在XP上调用capabilities()时会得到什么?我在最后看到了一个'cairo' TRUE。 - Dirk Eddelbuettel
我得到了FALSE,但无论如何还是谢谢你,这是一段非常有趣的旅程。我稍后会在Ubuntu上尝试它。再次感谢你的关注。 - Mark
很高兴为您效劳。Cairo是一个带有反锯齿、Alpha混合等优点的好东西,但其渲染速度较慢也是一个缺点。 - Dirk Eddelbuettel
啊,我看到Felix已经有了必需品。因此,在Windows上,您需要其中一个Cairo软件包,这需要您获取Gtk+进行底层技术支持。很合理。 - Dirk Eddelbuettel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接