[R] How to use RColorBrewer in ggplot2?
Achim Zeileis
Ach|m@Ze||e|@ @end|ng |rom u|bk@@c@@t
Fri Aug 15 13:31:08 CEST 2025
Note that the RColorBrewer colors are also available in base R. The
qualitative palettes are exactly the same as in the palette.colors()
function:
R> RColorBrewer::brewer.pal(4, "Paired")
[1] "#A6CEE3" "#1F78B4" "#B2DF8A" "#33A02C"
R> palette.colors(4, "Paired")
[1] "#A6CEE3" "#1F78B4" "#B2DF8A" "#33A02C"
For the sequential and diverging palettes, very close approximations using
the HCL color model are available in hcl.colors():
R> RColorBrewer::brewer.pal(9, "YlGnBu")
[1] "#FFFFD9" "#EDF8B1" "#C7E9B4" "#7FCDBB" "#41B6C4" "#1D91C0" "#225EA8"
[8] "#253494" "#081D58"
R> hcl.colors(9, "YlGnBu", rev = TRUE)
[1] "#FCFFDD" "#E3F8D4" "#BCE9C5" "#85D5B7" "#18BDB0" "#00A0AE" "#007EB3"
[8] "#004C90" "#26185F"
Best,
Achim
On Fri, 15 Aug 2025, Rui Barradas wrote:
> 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.marttila using 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-bounces using r-project.org> On Behalf Of Luigi Marongiu
>>> Sent: Thursday, 14 August 2025 06:25
>>> To: r-help <r-help using 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 using 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 using 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.
>
More information about the R-help
mailing list