是否可以轻松去除ggplot? [英] Is it possible to desaturate a ggplot easily?

查看:153
本文介绍了是否可以轻松去除ggplot?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以轻松地对 ggplot 进行去饱和处理?




原则上,可能有两种可能的策略。

首先,将一些函数应用于 ggplot 对象(或者可能是 Grob 对象)去饱和所有颜色。其次,一些技巧在呈现 .rmd 文件时打印 ggplot 不饱和。这两种策略对我来说都可以,但是第一个策略当然会更有前途。

从头开始创建灰色的 ggplot 并不是一件好事选项,因为它的想法就像是以灰色阴影打印一样。




关于如何在R中执行去饱和,有一些类似的问题和非常好的答案。



现在让我们使用colorblindr中的 edit_colors()函数对此图进行去色处理:

  library(colorblindr)#devtools :: install_github(clauswilke / colorblindr)
library(colorspace)#install.packages(colorspace,repos =http://R-Forge.R-project.org)--- colorblindr需要开发版本
#还需要安装cowplot; CRAN上的当前版本很好。

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



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



我们可以为转换函数提供额外的参数,例如

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



我们也可以做其他转换,例如色盲模拟:

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



最后,我们可以分别处理线条颜色和填充颜色。例如,我们可以使所有填充的区域变成蓝色。 (不知道这是否有用,但无论如何。)

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


Is it possible to desaturate a ggplot easily?


In principle, there could be two possible strategies.
First, apply some function to a ggplot object (or, possibly, Grob object) to desaturate all colors. Second, some trick to print ggplot desaturated while rendering a .rmd file. Both strategies would be ok for me, but first one is, of course, more promissing.
Creating ggplot in greys from the beginning is not a good option as the idea is to have the same plot as if it was printed in shades of grey.


There were some similar questions and remarkably good answers on how to perform desaturation in R. Here is a convenient way to desaturate color palette. And here is the way of desaturating a raster image. What I'm looking for, is a simple way of desaturating the whole ggplot.

解决方案

Just came across this question. The experimental package colorblindr (written by Claire McWhite and myself) contains a function that can do this in a generic way. I'm using the example figure from @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

Now let's desaturate this plot, using the edit_colors() function from colorblindr:

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)

The function edit_colors() takes a ggplot2 object or grob and applies a color transformation function (here desaturate) to all colors in the grob.

We can provide additional arguments to the transformation function, e.g. to do partial desaturation:

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

We can also do other transformations, e.g. color-blind simulations:

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

Finally, we can manipulate line colors and fill colors separately. E.g., we could make all filled areas blue. (Not sure this is useful, but whatever.)

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

这篇关于是否可以轻松去除ggplot?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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