Modify viridis palette in ggplot

前端 未结 2 1536
温柔的废话
温柔的废话 2021-01-06 09:23

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:         


        
2条回答
  •  一整个雨季
    2021-01-06 10:07

    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.

    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)
    

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

提交回复
热议问题