How to obtain the default colors as RGB?

2,495 views
Skip to first unread message

Marius Hofert

unread,
Aug 19, 2011, 6:50:35 AM8/19/11
to ggplot2
Dear all,

Is it possible to get the default colors used by ggplot2 in
hexadecimal code?
For example, what are the hexadecimal RGB triplets for the four colors
in the
following example? So basically, I'm interested in a function that
returns the
hexadecimal codes for a given number of default colors. It can also
return a
different specification of the color, I just would like to know what
color the
default scheme produces (if, say, one has four colored groups in a
plot and thus
four different colors are evenly spaced around a HSL color circle)

library(ggplot2)
n <- 4 # number of (default) colors
dfl <- data.frame(xval=1:n, yval=1)
ggplot(dfl, aes(xmin=xval-0.4, xmax=xval+0.4, ymin=yval-0.4, ymax=yval
+0.4,
fill=factor(xval))) + geom_rect() + scale_fill_hue()


Cheers,

Marius

Allan

unread,
Aug 19, 2011, 11:33:05 AM8/19/11
to ggplot2
Somewhere I saw Hadley post "You basically use scale_fill_gradientn
with the palettes in colorspace::rainbow_hcl"
Here is a lazy way that works for me:

require(latticeExtra)
mycols <- dput(ggplot2like(n = 4, h.start = 0, l = 65)$superpose.line
$col)
mycols
# "#F8766D" "#7CAE00" "#00BFC4" "#C77CFF"

library(ggplot2)
n <- 4 # number of (default) colors
dfl <- data.frame(xval=1:n, yval=1)
ggplot(dfl, aes(xmin=xval-0.4, xmax=xval+0.4, ymin=yval-0.4,
ymax=yval+0.4, fill=factor(xval))) +
geom_rect() + scale_fill_manual(values = mycols)

On Aug 19, 6:50 am, Marius Hofert <marius.hof...@googlemail.com>
wrote:

Marius Hofert

unread,
Aug 19, 2011, 3:13:26 PM8/19/11
to ggplot2
Dear Allan,

thank you very much. I looked at ggplot2like and removed everything
unnecessary to obtain the default colors. This is the remaining code
(which does not require having latticeExtra installed anymore):

require(ggplot2)
n <- 4
hcl(h=seq(15, 375-360/n, length=n)%%360, c=100, l=65)

Cheers,

Marius

Hadley Wickham

unread,
Aug 21, 2011, 6:44:56 AM8/21/11
to Marius Hofert, ggplot2
This is the idea of the scales package (not yet on CRAN, but will be
for the next version of ggplot2), see e.g.
https://github.com/hadley/scales/blob/master/R/pal-hue.r

Hadley

> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> Please provide a reproducible example: http://gist.github.com/270442
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>

--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

Reply all
Reply to author
Forward
0 new messages