Possible to keep colors static when toggling out observations in Plot?

Hi everyone!

A question from one of our Climate DataViz Challenge groups:

Plot assigns colors automatically according to the number of Observations shown. Can we override this and assign specific colors? This would be interesting for trying to keep consistent colors across graphics… especially where some graphs aren’t intended to toggle off Observations:

Here’s an example of changing color (the opposite of the desired effect)…

5 Likes

Your colors are based on the country’s position in the list of countries that you pass to Plot.lineY, which changes each time you fiddle with the checkboxes. Alternatively, you could use a function to specify the color in terms of the country’s position in the overall data:

3 Likes

Hi Mark! Thank you for helping us out! In your embedded example, it seems that the colors are still changing … i.e., if you click off Brunei Darussalam, then Cambodia becomes blue.

Do you have an example of how use a function to specify a fixed color?

Hit the reload button

1 Like

Thank you!

1 Like

The color Legend is missing.

2 Likes

Unfortunately, the Swatch that’s automatically generated from the line color:{legend:true} appears to have vanished. I suppose that’s based on the filtered colors and there’s a clash with the full color list. Might need to generate that separately.

2 Likes

I’ve added the Swatch, based on the selected countries using the same color scheme, in a separate cell. Note that I had also removed the addAnimation which doesn’t do anything as far as I can tell.

1 Like

Thank you for this, Mark. It’s really amazing how much support and help that you share. It’s also amazing b/c Brett and I were on the phone with this team both when I posted the question and when you replied. You really emphasized how asking questions is so commonly met with expert advice in this community! It’s such a gift to be here with you!

2 Likes

Another option is to specify a color domain: Fork of Static Colors / Observable / Observable

color:{legend:true,
domain: countries},

where ‘countries’ is the unique set of 10 countries in the data.

This auto-assigns a color to each element in the domain and it doesn’t change when you filter the data.

4 Likes

Cool! Thank you Ananya!

Thank you Aaron! Looks like @Ananya has the “officially recommended approach”. :slight_smile:

1 Like

Haha. I just merged both of your solutions into my question notebook!

1 Like