Tell me more ×
Cross Validated is a question and answer site for statisticians, data analysts, data miners and data visualization experts. It's 100% free, no registration required.

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: http://i.imgur.com/UuB6A.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.

share|improve this question
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? – Roman Luštrik Oct 18 '11 at 10:45
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. – dcl Oct 18 '11 at 10:50
You are right, I "printed" it using MS XPS document writer and it does have red tint to it. – Roman Luštrik Oct 18 '11 at 17:48
ggplot2 has a function ggsave to save plots to disk. Try this instead of pdf(); print(p); dev.off(); – Andrie Oct 18 '11 at 17:54
@Andrie. I have tried those but they do not work. – dcl Oct 19 '11 at 1:18

1 Answer

up vote 2 down vote accepted

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

share|improve this answer
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. – dcl Oct 19 '11 at 1:28
2  
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. – John Colby Oct 19 '11 at 15:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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