在ggplot中修改viridis调色板 [英] Modify viridis palette in ggplot

查看:215
本文介绍了在ggplot中修改viridis调色板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过主要是数字因素为ggplot着色,就像这样:

I'm trying to color a ggplot by a factor that's mostly numerical, like so:

iris %>%
  ggplot(aes(Sepal.Length, Sepal.Width, color = cut(Petal.Length, 0:7))) + 
  geom_point() + 
  scale_color_viridis_d()

我想做的是对(0,1]值进行特殊大小写,并将其颜色设置为红色,但保留其余值的弧度梯度.我知道我可以即时更改颜色,但是我想知道是否还可以构造一个新的离散调色板,该调色板以红色为第一种颜色,然后以其渐变为基础,从而使代码更具可重用性.

What I would like to do is to special-case the (0,1] value and color that in red but to retain the viridis gradient for the remaining values. I know I could change the color on the fly but I wonder if it's also possible to construct a new discrete color palette that has red as its first color and the gradient thereafter, which would make the code a bit more re-usable.

因此,更普遍的问题是:是否可以轻松地添加/删除/编辑现有离散调色板中的颜色?

The more general question is therefore: Can one easily add/remove/edit the colors in an existing discrete color palette?

推荐答案

这与@seasmith的答案基本相同,除了将调色板保存为变量以供以后使用.我经常这样做是为了明确设置NA颜色,然后在多个图中使用调色板.

This is basically the same as @seasmith's answer, except with the addition of saving the palette to a variable to use later. I do this often for explicitly setting a NA color and then using the palette across multiple plots.

其他软件包具有与viridis软件包相似的功能:例如,您可以从RColorBrewer::brewer.pal获得ColorBrewer调色板,从rcartocolor::carto_pal获得CartoColor调色板,或从与ggthemes相同名称的函数获得任何ggthemes软件包.调色板.

Other packages have similar functions to the viridis ones: for example, you can get ColorBrewer palettes from RColorBrewer::brewer.pal, CartoColor palettes from rcartocolor::carto_pal, or any of the ggthemes ones from functions with the same name as the palette.

library(tidyverse)

plasma_pal <- c("red", viridis::plasma(n = 6))

plasma_pal
#> [1] "red"       "#0D0887FF" "#6A00A8FF" "#B12A90FF" "#E16462FF" "#FCA636FF"
#> [7] "#F0F921FF"

iris %>%
    ggplot(aes(Sepal.Length, Sepal.Width, color = cut(Petal.Length, 0:7))) + 
    geom_point() +
    scale_color_manual(values = plasma_pal)

reprex软件包(v0.2.0)创建于2018-04-19.

Created on 2018-04-19 by the reprex package (v0.2.0).

这篇关于在ggplot中修改viridis调色板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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