Hello,

You can create as many colors as you like with

brewer.pal(n, name)

where name is the palette name.
Then, combine with the other color.

library(ggplot2)
library(RColorBrewer)

brew_colrs <- brewer.pal(4, "Paired")
black <- "#000000"

# set the names to the possible value of 'z'
colrs <- c(brew_cols, black) |> setNames(1:5)

set.seed(50)
x <- runif(15, 0, 1)
df <- data.frame(x = x,
                y = x^2 + runif(15, 0, 1),
                z = rep(1:5, 3))

ggplot(df, aes(x=x, y=y, colour=factor(z))) +
  geom_point(size=4) +
  scale_colour_manual(values = colrs)


Hope this helps,

Rui Barradas




On 8/15/2025 5:42 AM, Luigi Marongiu wrote:
Thank you, that worked fine.
May I also ask how to create a vector of colors with RColorBrewer?
Let's say I wanted to color z with 4 shade colors from the "Paired"
palette, but then assign the fifth with a color of my choice, say
"black", to distinguish clearly the fifth class.
If I run
```
scale_fill_brewer(palette="BrBG")
<ggproto object: Class ScaleDiscrete, Scale, gg>
     aesthetics: fill
     axis_order: function
     break_info: function
     break_positions: function
     breaks: waiver
     call: call
     clone: function
     dimension: function
     drop: TRUE
     expand: waiver
     get_breaks: function
     get_breaks_minor: function
     get_labels: function
     get_limits: function
     get_transformation: function
     guide: legend
     is_discrete: function
     is_empty: function
     labels: waiver
     limits: NULL
     make_sec_title: function
     make_title: function
     map: function
     map_df: function
     n.breaks.cache: NULL
     na.translate: TRUE
     na.value: NA
     name: waiver
     palette: function
     palette.cache: NULL
     position: left
     range: environment
     rescale: function
     reset: function
     train: function
     train_df: function
     transform: function
     transform_df: function
     super:  <ggproto object: Class ScaleDiscrete, Scale, gg>
```
while the help says "The brewer scales provide sequential, diverging
and qualitative colour schemes".
How can I generate 4 shades with RColorBrewer?
Thanks.

On Thu, Aug 14, 2025 at 10:00 AM Marttila Mikko
<mikko.martt...@orionpharma.com> wrote:

Hi Luigi,

As you map z to colour, you need scale_colour_brewer, not the fill version.
And to get discrete colours, you need to make z discrete. A separate group
mapping isn't needed in this case. Try this:

     ggplot(df, aes(x=x, y=y, colour=factor(z))) +
       geom_point(size=4) +
       scale_colour_brewer(palette = "Paired")

Best,

Mikko

-----Original Message-----
From: R-help <r-help-boun...@r-project.org> On Behalf Of Luigi Marongiu
Sent: Thursday, 14 August 2025 06:25
To: r-help <r-help@r-project.org>
Subject: [R] How to use RColorBrewer in ggplot2?

Hello,
I would like to define a color range to custom color some plot, specifically 
made in ggplot2 (but also for normal plots).
I have been trying to use RColorBrewer but I don't get any value out of this 
function. I expected it would create a vector of color values, but I must be 
missing something.
What is the correct you of this function?
Thank you.

EXAMPLE
```
set.seed(50)
df = data.frame(x = runif(15, 0, 1),
                 y = x^2 + runif(15, 0, 1),
                 z = rep(1:5, 3))
library(ggplot2)
library(RColorBrewer)
ggplot(df, aes(x=x, y=y, colour=z, group=z)) +
   geom_point(size=4) +
   scale_fill_brewer(palette = "Paired")
```


--
Best regards,
Luigi

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


This e-mail transmission may contain confidential or legally privileged 
information that is intended only for the individual or entity named in the 
e-mail address. If you are not the intended recipient, you are hereby notified 
that any disclosure, copying, distribution, or reliance upon the contents of 
this e-mail is strictly prohibited. If you have received this e-mail 
transmission in error, please reply to the sender, so that they can arrange for 
proper delivery, and then please delete the message from your computer systems. 
Thank you.




______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to