# Color and Legend with Gadfly - how can I control them?

**URL:** <https://discourse.julialang.org/t/color-and-legend-with-gadfly-how-can-i-control-them/3168>\
**Category:** Visualization\
**Tags:** question, gadfly\
**Created:** [April 11, 2017, 1:03pm UTC](https://discourse.julialang.org/t/color-and-legend-with-gadfly-how-can-i-control-them/3168 "2017-04-11T13:03:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![PeterJacko](https://avatars.discourse-cdn.com/v4/letter/p/b77776/32.png) [@PeterJacko](https://discourse.julialang.org/u/PeterJacko)\
**Post date:** [April 11, 2017, 1:03pm UTC](https://discourse.julialang.org/t/color-and-legend-with-gadfly-how-can-i-control-them/3168/1 "2017-04-11T13:03:53Z")

</div>

I would like to ask you if you have any advice… as I can’t sort out one issue when creating plots with Gadfly in Julia v0.5.

I need to plot matrices, which have discrete values (from 0 to 127), so that each value is plotted with one colour. See the attached examples.

I create them with the following line:

```julia
plot( x = jjj - 1 , y = iii - 1 , color = iiijjjvalues ,
     Scale.color_discrete , 
     Guide.xticks( ticks = collect( 0 : 5 : number_of_observations_arm_2 ) ) ,
     Guide.yticks( ticks = collect( 0 : 5 : number_of_observations_arm_1 ) ) ,
     Guide.xlabel( "# successes on arm 1 (out of $number_of_observations_arm_2)" ) , 
     Guide.ylabel( "# successes on arm 0 (out of $number_of_observations_arm_1)" ) ,
     Guide.colorkey( "Consecutive \n failures" ) ,
     Coord.cartesian( yflip = true , fixed = true ) ,
     Geom.point, 
     Theme( panel_fill = nothing , highlight_width = 0pt ,
            default_point_size = my_point_size , plot_padding = 5pt ,
            major_label_font_size = 11pt , minor_label_font_size = 9pt ) 
      ) 

```

You can see in the legend that the same sequence of colours appears in the two plots: blue, gold, pink, green, violet, red, grey, etc. These colours appear in the plot in this order if you follow the first column from top to bottom, then then second column, etc.

However the legend then looks weird, because it would be natural to have the colours ordered by the values they correspond to (0, 1, 2, 3, 4, etc.). Moreover, as the colour ordering prevails, the same values are plotted in different colours in these two plots, which is not very helpful - I need to include many figures like this and I would prefer if I only needed one legend, the same for all of them.

I wonder whether you would have a suggestion for a quick fix or where I could get some help with it. Basically, I need to get control of how the colours are assigned to values and how they appear in the legend.

Thanks a lot!

Peter.

Plot1:

 ![](https://global.discourse-cdn.com/julialang/original/3X/c/3/c359b2329cea0514c83fccfff5cdca2e3dc74c88.png)

Plot2:

 ![](https://global.discourse-cdn.com/julialang/original/3X/1/b/1b0aa20b0dd9c901daec634169a0b35b2ef15712.png)

---

<div class="post-metadata">

**Author:** ![bjarthur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bjarthur/32/9638_2.png) [@bjarthur](https://discourse.julialang.org/u/bjarthur)\
**Post date:** [April 11, 2017, 3:19pm UTC](https://discourse.julialang.org/t/color-and-legend-with-gadfly-how-can-i-control-them/3168/2 "2017-04-11T15:19:00Z")

</div>

have you thought about making an image instead of a plot?

---

<div class="post-metadata">

**Author:** ![PeterJacko](https://avatars.discourse-cdn.com/v4/letter/p/b77776/32.png) [@PeterJacko](https://discourse.julialang.org/u/PeterJacko)\
**Post date:** [April 11, 2017, 3:36pm UTC](https://discourse.julialang.org/t/color-and-legend-with-gadfly-how-can-i-control-them/3168/3 "2017-04-11T15:36:46Z")

</div>

not sure what you mean.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [April 11, 2017, 4:17pm UTC](https://discourse.julialang.org/t/color-and-legend-with-gadfly-how-can-i-control-them/3168/4 "2017-04-11T16:17:58Z")

</div>

Rather than going through the plotting machinery, you could just map each value to a different color yourself, and then generate an image from those mapped colors. This is extremely easy to do with the Images.jl package:

 ![](https://global.discourse-cdn.com/julialang/original/3X/0/3/033169800520cf314ee3306db9e85be0ee0f61e3.png)

You can save the resulting image as a PNG, and then embed that in your document. I realize that this doesn’t solve your legend problem, but it might be a simpler way to visualize your data.

---

<div class="post-metadata">

**Author:** ![vchuravy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vchuravy/32/8_2.png) [@vchuravy](https://discourse.julialang.org/u/vchuravy)\
**Post date:** [April 12, 2017, 2:21am UTC](https://discourse.julialang.org/t/color-and-legend-with-gadfly-how-can-i-control-them/3168/5 "2017-04-12T02:21:50Z")

</div>

Take a look at `Scale.color_discrete_hue` [http://gadflyjl.org/stable/lib/scales/scale\_color\_discrete\_hue.html](http://gadflyjl.org/stable/lib/scales/scale_color_discrete_hue.html) by fixing the color scale between your two plots you should be able to have a consistent choice of colors in your plots. You can have even more control with `Scale.color_discrete_manual` and create an appropriate `Theme` to reuse your settings across many plots.

---

<div class="post-metadata">

**Author:** ![PeterJacko](https://avatars.discourse-cdn.com/v4/letter/p/b77776/32.png) [@PeterJacko](https://discourse.julialang.org/u/PeterJacko)\
**Post date:** [July 17, 2017, 9:23am UTC](https://discourse.julialang.org/t/color-and-legend-with-gadfly-how-can-i-control-them/3168/6 "2017-07-17T09:23:13Z")

</div>

Thanks to eveyone for replying. Anyone interested, please see my solution here: [https://github.com/GiovineItalia/Gadfly.jl/issues/974](https://github.com/GiovineItalia/Gadfly.jl/issues/974)
