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

标签 r colors ggplot2 markdown

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

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

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

最佳答案

刚刚遇到这个问题。实验包 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

现在让我们使用 edit_colors() 去饱和该图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)

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

关于r - 是否可以轻松地降低 ggplot 的饱和度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32147307/

相关文章:

sql - 计算客户之间共享了多少订单

python - 在计算 Pandas 创建的数据框中列的平均值时指定 "skip NA"

r - 如何 : Automatically set fixed coordinate ratio (coord_fixed) when x- and y-axis are on different scales?

r - ggplot2 中的文本替换和格式化

google-chrome - Windows : Some colors are incorrecly displayed 上的 Chrome 65

r - 来自 ggpol 的facet_share 创建了太大的边距

r - 当 latex 包丢失时,tinytex 无法写入目录

r - R:使用子元素对嵌套列表中的值进行计数

c# - 为 HSB 拾色器创建 "Dynamic"渐变

text - 为什么 CGContextSetRGBStrokeColor 不适用于 ios7?