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:
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).