35

is there a way to plot anti-aliased graphics from the Windows version of R? As you can see from the two versions below the Mac version of R prints graphics anti aliased.... Mac Version

....whereas while the Windows version anti-aliases text, it does not anti-alias the actual graphic, as can be seen from the riser points, and the grid: Windows Version

Here is the code by the way:

library(scatterplot3d) 
attach(mtcars) 
s3d <-scatterplot3d(wt,disp,mpg, pch=16, highlight.3d=TRUE,
  type="h", main="3D Scatterplot")
fit <- lm(mpg ~ wt+disp) 
s3d$plane3d(fit)

I need the highest quality possible, for web page publication. I am running Windows 7 and pulling data from RBloomberg, which only works under Windows.

3 Answers 3

19

This is likely to depend on details of the rendering engine on each platform, which could be hard to modify. My suggestions (untested, for lack of time and access to Windows):

  • install the cairoDevice package and use Cairo_png(). According to the documentation:
 This functions the same as any other R graphics device. You may
 use the conventional plot commands and expect essentially the same
 output, except that everything is anti-aliased (similar to other
 vector-based devices like Quartz). Alpha-blending is supported, as
 is enhanced interactivity via ‘getGraphicsEvent’. The device
 should work the same across all supported platforms (Mac, Windows,
 and Linux).
  • Render the PNG at a much higher resolution (or output data from R as PDF) and use ImageMagick (convert) or some other tool to get the anti-aliased version you need.
3
  • You can also output as SVG with Cairo which may provide superior output.
    – James
    May 17, 2011 at 9:58
  • I have (finally) got this working. But is there any way to save files or copy and paste, from the screen image, like one can with the standard device? Thanks. Aug 11, 2011 at 20:34
  • Once cairoDevice is installed, you can simply call Cairo() for a plot window that is anti-aliased under MS Windows. Oct 4, 2015 at 8:12
15

Installing cairoDevice is no longer necessary for using Cairo with png devices. You can now specify type='cairo' when opening the device. Compare the following:

png('test1.png', 500, 500)
s3d <- scatterplot3d(wt,disp,mpg, pch=16, highlight.3d=TRUE,
                     type="h", main="3D Scatterplot")
fit <- lm(mpg ~ wt+disp) 
s3d$plane3d(fit)
dev.off()

enter image description here

png('test2.png', 500, 500, type='cairo')
s3d <- scatterplot3d(wt,disp,mpg, pch=16, highlight.3d=TRUE,
                     type="h", main="3D Scatterplot")
fit <- lm(mpg ~ wt+disp) 
s3d$plane3d(fit)
dev.off()

enter image description here

I'm running Win 8.1, and 64-bit R 3.2.2.

9

Use a vector device such as pdf. First make sure you have that capability and so not surprisingly the capabilities function is what to check. If you do have pdf then just do this:

pdf(file="out_graph.pdf")
s3d <-scatterplot3d(wt,disp,mpg, pch=16, highlight.3d=TRUE,
  type="h", main="3D Scatterplot")
fit <- lm(mpg ~ wt+disp) 
s3d$plane3d(fit)
dev.off()

An alternative for web output might be the png() graphics device. It gets high marks for compactness and web browser compatibility although it is a raster format.

5
  • @Joris ... I thought the SO interface might have chosen a format on its own. I don't think it takes my pdf's and keeps them in that format when I upload them. (But I could be in error here.)
    – IRTFM
    May 16, 2011 at 22:08
  • The OP said he needs them for web display, so he probably needs a raster format.
    – Ben Bolker
    May 16, 2011 at 22:34
  • I would be lazy and make a pdf then use ImageMagick to convert that to a high-res png. May 17, 2011 at 5:06
  • @DWin : I would have thought so too, but the difference between the upper plot and the lower is apparent, and both are png.
    – Joris Meys
    May 17, 2011 at 8:10
  • @ Joris. Right. So the problem is with the default output of the screen graphics device in Win7. He needs to coerce the format to something else or to apply settings that improve the resolution.
    – IRTFM
    May 17, 2011 at 10:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.