user1923975
user1923975

Reputation: 1389

Manual colour scale not working

I am using RBrewer to to manually colour my ggplot bar chart but i'm having no luck.

I create my colour palette of blues and then assign it to a function for it to ramp.

blues <- brewer.pal(9, "Blues")
blue_range <- colorRamp(blues)

I then plot my stacked bar chart, where I know i have 20 groups.

ggplot(Month.Summary, aes(x=Calendar.Month, y = Measure, fill = Groups)) + geom_bar(stat="Identity", position = "fill") +scale_fill_manual(values = blue_range(20))

I unfortunately get the following error:

Error: Insufficient values in manual scale. 20 needed but only 3 provided.

I'm using Groups as my fill, where I know there are 2 instances. I'm passing 20 to the blue_range function so i'm not sure why it's saying i'm only passing 3 colours.

Upvotes: 1

Views: 810

Answers (1)

krlmlr
krlmlr

Reputation: 25444

The blue_range() function expects values between 0 and 1. To get the discrete palette, pass a sequence to this function:

> blue_range(seq(0, 1, length.out = 20))
           [,1]      [,2]     [,3]
 [1,] 247.00000 251.00000 255.0000
 [2,] 236.47368 244.26316 251.6316
 [3,] 225.94737 237.52632 248.2632
 [4,] 215.68421 230.78947 244.8947
 [5,] 205.57895 224.05263 241.5263
 [6,] 193.78947 217.21053 237.5263
 [7,] 176.94737 210.05263 231.6316
 [8,] 160.10526 202.89474 225.7368
 [9,] 139.21053 191.68421 220.9474
[10,] 117.73684 179.89474 216.3158
[11,]  98.36842 168.10526 210.6316
[12,]  81.10526 156.31579 203.8947
[13,]  64.26316 144.26316 197.1053
[14,]  50.36842 130.36842 189.9474
[15,]  36.47368 116.47368 182.7895
[16,]  25.10526 102.89474 173.1053
[17,]  14.57895  89.42105 162.5789
[18,]   8.00000  75.78947 148.2632
[19,]   8.00000  61.89474 127.6316
[20,]   8.00000  48.00000 107.0000

This should work in the ggplot() call -- not tested because you didn't provide a reproducible example.

Note that recent ggplot2 has scale_fill_distiller() which provides a similar functionality with a more convenient interface.

Upvotes: 1

Related Questions