柱形图阴影线的绘制方法

来源:互联网 发布:正规淘宝运营 编辑:程序博客网 时间:2024/06/05 03:17

R语言的图形颜色是非常丰富的,绘制出的彩色图表美观大方。但发表论文时如果适当选用黑白或灰度图形,不仅可以减少一些不必要的开支,而且也可以减少印刷油墨消耗,低碳环保。在灰度模式下,R语言绘制的很多图形都很容易设置,仅柱形图有些困难。

多系列柱形图的黑白图形可以选用两种方式绘制。第一种方式非常简单,直接使用灰度颜色区分不同样品,灰度颜色直接用gray函数产生:

set.seed(100)xx <- matrix(sample(100, 14, replace = FALSE), ncol = 2)n <- nrow(xx)cols <- rev(gray(0:(n + 1)/(n + 1)))[1:n]barplot(xx, col = cols, beside = TRUE, ylim = c(0, max(xx) * 1.1))legend("topleft", legend = LETTERS[1:n], fill = cols, box.col = NA)box()


第二种方式是使用阴影线。barplot函数中的阴影线可用angle和density参数设置线的角度和密度(当然也可以设置颜色)。简单改变角度和密度获得的图形区分度非常有限,组合两种不同角度的线可获得更好的效果。我们可以把这些组合编写成函数方便使用:

bnw_barplot <- function(height, shade = 1:7, ...) {    angle1 <- c(0, 45, 135, 88, 0, 45, 180)[shade]    angle2 <- c(0, 45, 135, 88, 0, 135, 90)[shade]    dens <- c(0, rep(10, 6))[shade]    barplot(height, angle = angle1, density = dens, col = "gray40", ...)    barplot(height, angle = angle2, density = dens, col = "gray40", add = TRUE,        ...)    box()}bnw_legend <- function(x, y = NULL, legend, shade = 1:7, ...) {    angle1 <- c(0, 45, 135, 88, 0, 45, 180)[shade]    angle2 <- c(0, 45, 135, 88, 0, 135, 90)[shade]    dens <- c(0, rep(20, 6))[shade]    legend(x, y, legend, angle = angle1, density = dens, ...)    legend(x, y, legend, angle = angle2, density = dens, ...)}


需要同时设置柱形图和图例。看看上面函数的调用效果:

shade <- 1:nbnw_barplot(xx, shade = shade, beside = TRUE, ylim = c(0, max(xx) * 1.1))bnw_legend("topleft", legend = LETTERS[1:n], shade = shade, box.col = "NA",    inset = 0.02)


上面的函数设置了7种阴影,如果不够用可以自行设置阴影线组合,只要区分度足够好就没有问题,如果还不够用就考虑阴影和灰度颜色的组合。barplot函数不能直接组合灰度填充颜色和阴影线,需要一点小技巧,可自行尝试。



作者: ZGUANG@LZU

Created: 2015-10-21 三 12:00

Emacs 24.4.1 (Org mode 8.2.10)

0 0
原创粉丝点击