ggplot2 Error: Insufficient values in manual scale

前端 未结 2 1909
难免孤独
难免孤独 2020-12-16 15:56

When you have less colours defined with scale_fill_manual than levels in a factor, ggplot2 complains with this error message :

# B         


        
2条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-16 16:17

    Just found a way to fill a palette to the needed size by picking from a given pool.

    # count the needed levels of a factor
    number <- nlevels(s4r$DIM)
    
    # repeat the given colors enough times
    palette <- rep(c("color1", "color2", ...), length.out = number)
    
    # or, if you want a random distribution:
    # if you want it random, but reproducable,
    # backup .Random.seed, or set your own
    set.seed(23)
    palette <- sample(c("color1", "color2", ...), number, replace = TRUE)
    
    scale_fill_manual(values=palette)
    

提交回复
热议问题