ggplot2用于灰度打印输出 [英] ggplot2 for grayscale printouts

查看:567
本文介绍了ggplot2用于灰度打印输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ggplot2为屏幕/彩色打印产生花哨的图形,但灰色背景和颜色在将它们打印为灰度时会产生干扰。为了获得更好的可读性,我希望禁用灰色背景,并使用产生不同灰度阴影或不同填充笔划的颜色生成器来区分组。

解决方案

**编辑**更新的代码: geom_bar 需要一个 stat



theme_bw 可能就是您要做的。如果你正在绘制一个填充像条的geom,那么 scale_fill_grey 函数可以让你控制灰色阴影。如果您正在绘制具有颜色的几何图形(如线条或点), scale_colour_grey 函数将为您提供控件。据我所知,ggplot不会绘制图案填充。假设你正在绘制条形图,以下将在灰色背景上绘制彩色条。

  library(ggplot2)

data< - read.table(text =
)类型年值
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))

以下内容将彩条变为灰色阴影。请注意,其中一个栏会在后台丢失。

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

以下消除灰色背景。

 (p = p + theme_bw())



点有一个颜色,而不是填充。所以要在积分上使用灰色阴影,你需要这样的东西。

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


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.

解决方案

** 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())

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())

这篇关于ggplot2用于灰度打印输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆