2
$\begingroup$

I have an issue with printing/creating black and white and greyscale images with ggplot2.

I have been trying to make some black and white, and greyscale graphics for publication, they look perfect on mine and everyone's monitors, but came out in colour when printed. (An awful photo of the result is here: https://i.stack.imgur.com/jh2rL.jpg)

For example:

p <- qplot(displ, hwy, data = mpg)
p <- p + facet_wrap(~ cyl) + theme_bw()

 pdf("testplot.pdf",width=7.6,height=3.6)
     p
 dev.off()

The generated image and output pdf looks perfect onscreen, but the facets borders and titles are in a shade of red when printed. Try it!

I get the same results when outputting to a .png, and also when I print directly from the R graphics window. According to my friend with some experience with image manipulation, the images I've sent him have bits of colour in them according to the colour histogram.

Is there any way to generate true B&W and greyscale images using ggplot? I tried forcing greyscale (perhaps very naively!) by using 'colormodel="gray"' in the pdf() command, but it does not make a difference.

Any help would be greatly appreciated.

$\endgroup$
5
  • $\begingroup$ I've removed my answer as it was obviously wrong. Your code works for me (no red anything). What versions of R/ggplot2 are you using? $\endgroup$ Oct 18, 2011 at 10:45
  • $\begingroup$ Have you tried printing it though? Or looking at a colour histogram of the image? I'm using ggplot2 version 0.8.9 and R 2.12.1. $\endgroup$
    – dcl
    Oct 18, 2011 at 10:50
  • $\begingroup$ You are right, I "printed" it using MS XPS document writer and it does have red tint to it. $\endgroup$ Oct 18, 2011 at 17:48
  • $\begingroup$ ggplot2 has a function ggsave to save plots to disk. Try this instead of pdf(); print(p); dev.off(); $\endgroup$
    – Andrie
    Oct 18, 2011 at 17:54
  • $\begingroup$ @Andrie. I have tried those but they do not work. $\endgroup$
    – dcl
    Oct 19, 2011 at 1:18

1 Answer 1

2
$\begingroup$

Printing ggplot2 plots to the png or jpeg devices does result in RGB color space files, like you describe. If you need a true grayscale color model for those, they can easily be coverted with photoshop droplets or similar.

For the pdf device, however, the grays in ggplot2 figures are being correctly described as grayscale black percentages (i.e. 20% black for the facet labels background, 80% black for the histogram bars, 10.2% black for the main background) even without the colormodel argument.

require(ggplot2)

n = 1000

group = factor(rep(1:3, len=n))
value = as.numeric(group) + rnorm(n)

data = data.frame(group, value)

dev.new(width=5, height=2)
p = qplot(value, data=data) + facet_wrap(~group, nrow=1)
p

dev.print(device=png, file='test.png', width=dev.size()[1], units='in', res=100)
dev.print(device=pdf, file='test.pdf')

enter image description here

$\endgroup$
2
  • $\begingroup$ Hi John, I'm afraid the images produced by your code still have the same issue for me... I just printed them out and they are not greyscale. i.imgur.com/v13dD.png Colour Histogram of the original pdf. $\endgroup$
    – dcl
    Oct 19, 2011 at 1:28
  • 2
    $\begingroup$ If that is a screenshot of the pdf, it will get saved as png or jpeg, and be in RGB like the others. But the actual pdf is gray. Perhaps take a look at your computer's color settings? On mac this is the "Color" tab under the "Displays" system pref pane. Also check the color settings and color profile that you're using to print. If either of those are off, there could be color casts like you're getting. $\endgroup$
    – John Colby
    Oct 19, 2011 at 15:57

Your Answer

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

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