0

Am getting the error when adding a label to my scale_colour_stepsn*

Error in `check_breaks_labels()`:
! `breaks` and `labels` must have the same length

*am using this to specific the colours of my bins rather than putting my data on a scale/gradient

Have looked around for a solution but they all seem to be for characters, whereas my data is numerical (continuous) and I'm not sure how to apply those solutions to my situation

Here's my code:

my_colours<- c("#00B050", "#92D050", "#FFFFB2", "#FED976", "#FEB24C")
aus_shp<- read_sf("~/map files/STE_2016_AUST.shp")
aus_shp$ent_gentob<- c(4.3,11.9,8.3,6.1,14.5,0.0,16.8,4.6,NA)
ggplot(data = aus_shp) +
  geom_sf(aes(fill = ent_gentob))+
  theme_void()+
  theme(legend.position = c(.93, .93),
        legend.justification = c("right", "top"),
        legend.box.just = "right",
        legend.margin = margin(6, 6, 6, 6))+
  scale_colour_stepsn(
    colours =  my_colours,
    breaks = c(1,5,10,15,20),
    labels = c(">1%", "1-<5%", "5-<10%", "10-<15%", "15-<20%"),
    aesthetics = "fill",
    name = "% R"
    )

I need to replicate this chart several times for different numerical variables using the same scale

Thanks in advance

1
  • Please provide enough code so others can better understand or reproduce the problem. Commented Mar 14, 2023 at 11:05

1 Answer 1

1

The issue is that there are only four breaks in your legend (the last one 20 gets dropped), whereas you provide five labels. One option to fix that would be to set the limits to include the 20 as the upper bound.

Also note that there is a scale_fill_stepsn.

Using some fake example data base on the nc shapefile from the sf package.

my_colours <- c("#00B050", "#92D050", "#FFFFB2", "#FED976", "#FEB24C")
aus_shp <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
aus_shp$ent_gentob <- rep_len(c(4.3, 11.9, 8.3, 6.1, 14.5, 0.0, 16.8, 4.6, NA), nrow(aus_shp))

library(ggplot2)

base <- ggplot(data = aus_shp) +
  geom_sf(aes(fill = ent_gentob)) +
  theme_void() +
  theme(
    # legend.position = c(.93, .93),
    legend.justification = c("right", "top"),
    legend.box.just = "right",
    legend.margin = margin(6, 6, 6, 6)
  )

base +
  scale_fill_stepsn(
    colours = my_colours,
    breaks = c(1, 5, 10, 15, 20),
    limits = c(0, 20),
    labels = c("<1%", "1-<5%", "5-<10%", "10-<15%", "15-<20%"),
    name = "% R"
  )

However, personally I would switch to a discrete fill scale for this type of legend by binning the data manually. Also note that the color assignment is different from the one you get by relying on scale_fill_stepsn. IMHO this is one more reason to do some manual work.

aus_shp$ent_gentob_bins <- cut(aus_shp$ent_gentob,
  breaks = c(-Inf, 1, 5, 10, 15, 20),
  right = FALSE
)

ggplot(data = aus_shp) +
  geom_sf(aes(fill = ent_gentob_bins)) +
  theme_void() +
  theme(
    # legend.position = c(.93, .93),
    legend.justification = c("right", "top"),
    legend.box.just = "right",
    legend.margin = margin(6, 6, 6, 6)
  ) +
  scale_fill_manual(
    values = my_colours,
    limits = levels(aus_shp$ent_gentob_bins),
    labels = c("<1%", "1-<5%", "5-<10%", "10-<15%", "15-<20%"),
    name = "% R",
    guide = guide_legend(override.aes = list(color = NA), reverse = TRUE)
  )

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @stefan that worked!* *eventually, first I had to increase the number of colours as it spewed out the error "Insufficient values in manual scale. 8 needed but only 5 provided". So I upped the number of colours in my scale (which works for future charts anyway where the % R will increase for other maps)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.