Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
368 views
in Technique[技术] by (71.8m points)

r - ggplot2 for grayscale printouts

ggplot2 produces fancy graphs for screen/color prints, but the gray background and the colors interfere when printing them to grayscale. For better readablility, I'd prefer to disable the gray background and use color generators that produce either different shades of gray or different kinds of filling strokes to distinguish the groups.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

** EDIT ** Updated code: geom_bar requires a stat.

theme_bw could be what you're after. If you are plotting a geom that has a fill such as bars, the scale_fill_grey function will give you control over the shades of grey. If you are plotting a geom that has a colour (such as a line or points), the scale_colour_grey function will give you the control. As far as I know, ggplot does not plot patterned fills. Assuming you're plotting bars, the following will plot coloured bars on a grey background.

library(ggplot2)

data <- read.table(text = 
"type Year  Value 
 A    2000  3
 B    2000  10
 C    2000  11
 A    2001  4
 B    2001  5
 C    2001  12", sep = "", header = TRUE)

(p = ggplot(data = data, aes(x = factor(Year), y = Value)) +       
  geom_bar(aes(fill = type), stat="identity", position = "dodge"))

The following changes the coloured bars to shades of grey. Note that one of the bars gets lost in the background.

(p = p + scale_fill_grey(start = 0, end = .9))

The following removes the grey background.

(p = p + theme_bw())

enter image description here

A point has a colour, not a fill. So to use shades of grey on points, you would need something like this.

(p = ggplot(data = data, aes(x = factor(Year), y = Value)) +       
  geom_point(aes(colour = type), size = 5) +
  scale_colour_grey(start = 0, end = .9) +
  theme_bw())

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...