Skip to contents

Manual ggplot2 scales mapping a 1-5 rating (as a factor or its character codes "1".."5") onto the pal_rating red-amber-green palette.

Usage

scale_fill_rating(distinct = FALSE, ...)

scale_colour_rating(distinct = FALSE, ...)

scale_color_rating(distinct = FALSE, ...)

Arguments

distinct

If TRUE, use a fully distinct 5-colour ramp where each point has its own shade; if FALSE (default) use pal_rating, where 1 and 2 share red and 4 and 5 share green. The distinct ramp suits stacked bars, where neighbouring segments need to be told apart.

...

Passed to ggplot2::scale_fill_manual() / ggplot2::scale_colour_manual().

Value

A ggplot2 scale.

Details

Maps the rating codes "1".."5" onto pal_rating, which uses the bands_rating_3() thresholds (1-2 bad, 3 ok, 4-5 good). Set distinct = TRUE for a fully distinct 5-colour ramp, which reads better on stacked bars where adjacent segments must be separable. scale_color_rating() is an alias of scale_colour_rating().

Examples

df <- data.frame(feature = c("a", "b"), pct = c(60, 40), rating = c(4, 2))
ggplot(df, aes(feature, pct, fill = factor(rating))) +
  geom_col() +
  scale_fill_rating()