[R] How to use RColorBrewer in ggplot2?

Marttila Mikko m|kko@m@rtt||@ @end|ng |rom or|onph@rm@@com
Thu Aug 14 10:00:40 CEST 2025


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.



More information about the R-help mailing list