Extracting the colors assigned by ggplot

1,989 views
Skip to first unread message

Ashim Kapoor

unread,
Nov 8, 2017, 10:12:50 PM11/8/17
to ggplot2
Dear All,

Here is a reprex :

x <- as.factor(rep(1:10,10))
y <- rnorm(100)
z <- as.factor(sample(x,100))

ggplot(data = data.frame(x,y,z), aes(x=x,y=y,color = z,shape = z,fill = z)) + geom_point()

I want to list out which color is assigned to which level of the factor z.

How can I do this ?

Best Regards,
Ashim

Ron Crump

unread,
Nov 9, 2017, 7:07:20 AM11/9/17
to Ashim Kapoor, ggplot2


Dear Ashim,

>Here is a reprex :

Thanks.

>x <- as.factor(rep(1:10,10))
>y <- rnorm(100)
>z <- as.factor(sample(x,100))
>
>ggplot(data = data.frame(x,y,z), aes(x=x,y=y,color = z,shape = z,fill =
>z))
>+ geom_point()
>
>I want to list out which color is assigned to which level of the factor z

I don't know. I would take the approach of setting them manually, which has the added advantage of letting you chose your colour scheme. Add a call to scale_fill_manual and scale_colour_manual with a vector of colours.

I think the fill in your aesthetic (and consequently a call to scale_fill_manual) might be irrelevant as most shapes don't have a fill attribute, only a colour one. I'd check this but I don't have my PC to hand.

Alternatively, the default list of colours is available online at
http://www.cookbook-r.com/Graphs/Colors_(ggplot2)/
Although if you plan to do something like refer to a feature of your graph via something like 'the blue point in figure 1' you might find the default colours a challenge to describe/name. I do.

Regards,
Ron.

Ron Crump

unread,
Nov 9, 2017, 10:34:40 AM11/9/17
to Ashim Kapoor, ggplot2
Dear Ashim,

>Here is a reprex :

Thanks.

>x <- as.factor(rep(1:10,10))
>y <- rnorm(100)
>z <- as.factor(sample(x,100))
>
>ggplot(data = data.frame(x,y,z), aes(x=x,y=y,color = z,shape = z,fill =
>z))
>+ geom_point()
>
>I want to list out which color is assigned to which level of the factor z

I don't know. I would take the approach of setting them manually, which has the added advantage of letting you chose your colour scheme.

Alternatively, the default list of colours is probably available online


Ron Crump

unread,
Nov 9, 2017, 10:37:58 AM11/9/17
to Ashim Kapoor, ggplot2
My apologies for my most recent email. It was sent in error - I came across it in my drafts folder and thought I hadn't previously sent it.

Ron.

Ashim Kapoor

unread,
Nov 10, 2017, 12:19:59 AM11/10/17
to Ron Crump, ggplot2
Dear Jianshuang and Ron,

Many thanks for your reply.

> library(scales)
> hue_pal()(10)
 [1] "#F8766D" "#D89000" "#A3A500" "#39B600" "#00BF7D" "#00BFC4" "#00B0F6"
 [8] "#9590FF" "#E76BF3" "#FF62BC"

> x <- as.factor(rep(1:10,10))
> y <- rnorm(100)
> z <- as.factor(sample(x,100))
> levels(z)
 [1] "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"  "10"

Can I assume that the 10 colours generated by hue_pal will be mapped to the levels of z in the same order as they occur in levels(z)  ?

For instance if I do :
z <- factor(z, levels = seq(10,1))

> levels(z)
 [1] "10" "9"  "8"  "7"  "6"  "5"  "4"  "3"  "2"  "1"
>

In this case will the 10 colours reported by hue_pal()(10) be assigned to the 10 levels of z in the reverse of the original order?

Another query :

x <- as.factor(rep(1:10,10))
y <- rnorm(100)
z <- as.factor(sample(x,100))

gg1 <- ggplot(data = data.frame(x,y,z), aes(x=x,y=y,color = z,shape =z,fill = z)) + geom_point() +
       scale_shape_manual(values=c(11:20))

Note: we need the scale shape manual because we have more than 6 levels for shape. I forgot to do this in my orginal example (my apologies for this).

Can I not manipulate the following matrices and recover the mapping between colour and the levels of z?

> ggplot_build(gg1)$data[1]
[[1]]

    shape  colour    fill  x           y PANEL group size alpha stroke
1      16 #00BFC4 #00BFC4  1  0.29579631     1     4  1.5    NA    0.5
2      12 #D89000 #D89000  2 -0.43358638     1     8  1.5    NA    0.5
3      20 #FF62BC #FF62BC  3 -0.09725683     1    20  1.5    NA    0.5
4      15 #00BF7D #00BF7D  4  0.90603884     1    23  1.5    NA    0.5
5      17 #00B0F6 #00B0F6  5 -0.62904424     1    29  1.5    NA    0.5
6      18 #9590FF #9590FF  6 -1.07468368     1    37  1.5    NA    0.5
7      12 #D89000 #D89000  7 -1.23907191     1    40  1.5    NA    0.5
8      14 #39B600 #39B600  8 -0.40183759     1    49  1.5    NA    0.5
9      13 #A3A500 #A3A500  9  2.17610186     1    55  1.5    NA    0.5
10     16 #00BFC4 #00BFC4 10 -0.16761590     1    63  1.5    NA    0.5
11     20 #FF62BC #FF62BC  1 -0.55441511     1     7  1.5    NA    0.5
12     16 #00BFC4 #00BFC4  2 -0.91594616     1    12  1.5    NA    0.5
13     15 #00BF7D #00BF7D  3  0.30314967     1    17  1.5    NA    0.5
14     19 #E76BF3 #E76BF3  4  0.38348840     1    26  1.5    NA    0.5
15     14 #39B600 #39B600  5 -0.01784608     1    27  1.5    NA    0.5
16     15 #00BF7D #00BF7D  6 -0.82852709     1    35  1.5    NA    0.5
17     17 #00B0F6 #00B0F6  7  1.00782650     1    43  1.5    NA    0.5
18     12 #D89000 #D89000  8  0.45755324     1    47  1.5    NA    0.5
19     13 #A3A500 #A3A500  9 -0.02577137     1    55  1.5    NA    0.5
20     20 #FF62BC #FF62BC 10  0.85958394     1    65  1.5    NA    0.5
21     12 #D89000 #D89000  1 -0.06696429     1     2  1.5    NA    0.5

or the following matrix ?

> ggplot_build(gg1)$plot[[4]]
* x      -> x
* y      -> y
* colour -> z
* shape  -> z
* fill   -> z

Which object contains the mapping between the levels of z and the colour assigned to it ?

Best Regards,
Ashim.

Reply all
Reply to author
Forward
0 new messages