是否有可能轻易地使ggplot去饱和?

时间:2015-08-21 18:40:16

标签: r colors ggplot2 markdown

是否可以轻松降低ggplot的饱和度?

原则上,可能有两种可能的策略 首先,将一些函数应用于ggplot对象(或者可能是Grob对象)以使所有颜色去饱和。第二,在渲染ggplot文件时打印.rmd去饱和的一些技巧。这两种策略对我来说都没问题,但第一种策略当然更有意义 从头开始创建灰色的ggplot不是一个好选择,因为想法是使用与灰色阴影打印相同的图。

对于如何在R中执行去饱和,有一些类似的问题和非常好的答案。Here是一种去饱和色调的便捷方法。而here是对光栅图像进行去饱和处理的方法。我正在寻找的是一种简单的方法来减少整个ggplot的饱和度。

3 个答案:

答案 0 :(得分:8)

刚刚遇到这个问题。实验包colorblindr(由Claire McWhite和我自己编写)包含一个可以通用方式执行此操作的函数。我正在使用@hrbrmstr中的示例图:

library(ggplot2)
library(viridis)

gg <- ggplot(mtcars) + 
  geom_point(aes(x=mpg, y=wt, fill=factor(cyl), size=factor(carb)), 
             color="black", shape=21) + 
  scale_fill_viridis(discrete = TRUE) +
  scale_size_manual(values = c(3, 6, 9, 12, 15, 18)) +
  facet_wrap(~am)
gg

enter image description here

现在让我们使用colorblindr的edit_colors()函数去饱和这个情节:

library(colorblindr) # devtools::install_github("clauswilke/colorblindr")
library(colorspace) # install.packages("colorspace", repos = "http://R-Forge.R-project.org") --- colorblindr requires the development version
# need also install cowplot; current version on CRAN is fine.

gg_des <- edit_colors(gg, desaturate)
cowplot::ggdraw(gg_des)

enter image description here

函数edit_colors()采用ggplot2对象或grob,并将颜色转换函数(此处为desaturate)应用于grob中的所有颜色。

我们可以为转换函数提供额外的参数,例如:做部分去饱和:

gg_des <- edit_colors(gg, desaturate, amount = 0.7)
cowplot::ggdraw(gg_des)

enter image description here

我们还可以进行其他转换,例如色盲模拟:

gg_des <- edit_colors(gg, deutan)
cowplot::ggdraw(gg_des)

enter image description here

最后,我们可以分别操纵线条颜色和填充颜色。例如,我们可以将所有填充区域变为蓝色。 (不确定这是否有用,但无论如何。)

gg_des <- edit_colors(gg, fillfun = function(x) "lightblue")
cowplot::ggdraw(gg_des)

enter image description here

答案 1 :(得分:3)

根据我上面的评论,这可能是实现ggplot2对象去饱和的最快/最脏的方式:

library(ggplot2)

set.seed(1)
p <- qplot(rnorm(50), rnorm(50), col="Class")
print(p)

enter image description here

pdf(file="p.pdf", colormodel="grey")
  print(p)
dev.off()

enter image description here

答案 2 :(得分:2)

我尝试使用新的绿色调色板,因为它很好地去饱和(即在彩色和非彩色图之间应该是明显的):

library(ggplot2)
library(grid)
library(colorspace)
library(viridis) # devtools::install_github("sjmgarnier/viridis") for scale_fill_viridis

gg <- ggplot(mtcars) + 
  geom_point(aes(x=mpg, y=wt, fill=factor(cyl), size=factor(carb)), 
             color="black", shape=21) + 
  scale_fill_viridis(discrete = TRUE) +
  scale_size_manual(values = c(3, 6, 9, 12, 15, 18)) +
  facet_wrap(~am)

gb <- ggplot_build(gg)
gb$data[[1]]$colour <- desaturate(gb$data[[1]]$colour)
gb$data[[1]]$fill <- desaturate(gb$data[[1]]$fill)

gt <- ggplot_gtable(gb)

grid.newpage()
grid.draw(gt)

你最终不得不操纵grob级别。

这里的情节是去饱和度:

enter image description here

以及这里描绘的情节:

enter image description here

我试图弄清楚为什么传说被跳过了,这可能会错过其他高度定制的ggplot美学和&amp;组件,所以即使它不是一个完整的答案,也许它可能是有用的(也许其他人可以坚持到它或在另一个答案中扩展它)。它应该只是替换gb对象或gt对象中的正确位。

更新我设法为图例找到了正确的grob元素:

gt$grobs[[12]][[1]][["99_9c27fc5147adbe9a3bdf887b25d29587"]]$grobs[[4]]$gp$fill <- 
    desaturate(gt$grobs[[12]][[1]][["99_9c27fc5147adbe9a3bdf887b25d29587"]]$grobs[[4]]$gp$fill)
gt$grobs[[12]][[1]][["99_9c27fc5147adbe9a3bdf887b25d29587"]]$grobs[[6]]$gp$fill <- 
    desaturate(gt$grobs[[12]][[1]][["99_9c27fc5147adbe9a3bdf887b25d29587"]]$grobs[[6]]$gp$fill)
gt$grobs[[12]][[1]][["99_9c27fc5147adbe9a3bdf887b25d29587"]]$grobs[[8]]$gp$fill <- 
    desaturate(gt$grobs[[12]][[1]][["99_9c27fc5147adbe9a3bdf887b25d29587"]]$grobs[[8]]$gp$fill)

grid.newpage()
grid.draw(gt)

enter image description here

找到需要去饱和的其他gp元素的阴谋也不是很糟糕。