Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save a plot as a B&W (no gray pixel) image in R?

Tags:

r

ggplot2

I am generating some test images which are supposed to have foreground and background colors only. I created my plots using ggplot2, and I saved them using the methods jpeg() and png(). But I realized that the output is not B&W. Below you can see a sample image and the issue as well.

A sample image

[A sample image]

The image showing the issue

[Zoomed-in to show the none B&W output]

How can I save a plot like that as a B&W image? (I prefer to stick to JPEG format.)

like image 642
Azim Avatar asked Jan 28 '26 00:01

Azim


1 Answers

You can use the option antialias = "none". For example

library(ggplot2)
library(cowplot)
png(antialias = "none")
ggplot(data.frame(x=1:100, y=sin((1:100/10))), aes(x,y)) +
  geom_line() 
dev.off()

enter image description here

like image 107
dww Avatar answered Jan 30 '26 18:01

dww



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!