Breaks and labels of different lengths scale_size_binned

Viewed 326

I'm likely missing something here, but can't find any good examples anywhere.. R Version 4.03, ggplot v 3.3.2. Trying to get scale_size_binned() labels to work, but keep getting an error

library(tidyverse)
set.seed(10)

t.dat <- data.frame(x=runif(100),y=runif(100),value=runif(100,1,10))
ggplot(t.dat,aes(x=x,y=y))+
  geom_point(aes(size=value))+
  scale_size_binned(breaks=c(1,2,5,10),
                    labels=c("1","2","5","10"),
                    limits = c(1,10))

Error: Breaks and labels are different lengths

Clearly the breaks and labels are not of different lengths - so wondering if there's something else I'm missing here.

1 Answers

Reading the online help, I think your code should work. But it doesn't.

One of the other ways of specifying labels is to supply a function, and this seems to work and give the result you want:

ggplot(t.dat,aes(x=x,y=y))+
  geom_point(aes(size=value))+
  scale_size_binned(
    breaks=c(1,2,5,10), 
    labels=function(x) as.character(round(x, 0))
  )

enter image description here

I think it's worth raising an issue.

Related