2

I have a source of data that has the following structure: timestamp, value, category. I visualize it using ggplot:

df %>% ggplot(aes(x=timestamp, y=value, col=category)) + geom_line()

While it works, there is a problem, namely, the mapping between category values and colors is not consistent from time to time.

I've looked at solution offered in How to assign colors to categorical variables in ggplot2 that have stable mapping?

but it doesn't seem to help me as I don't have a full list of all factor levels upfront. It has hundreds of possible values, and I have to wrap up most of them into an 'others' level to make the graph legible.

It may appear as an insignificant problem. However, some of the factor levels are very common and appear in every data snapshot. Therefore it's confusing when every time they appear in a different color.

Any suggestions would be greatly appreciated.

Best regards, Nikolai

Nikolai
  • 65
  • 5
  • But after all plots are completed you could save the factor levels, save them, and re-run your analysis? – r.user.05apr Jul 27 '20 at 13:55
  • Is this something you want to be able to go back to again and again to get the same colors, or is it more a one-time thing? If it's a long term thing, then how do you envision "saving" the levels you've "seen" before? – chemdork123 Jul 27 '20 at 14:07
  • @chemdork123 It's not a one-time thing, it's a part of a performance monitoring app. The problem with "saving" levels I've seen is that it requires manual intervention. All I want is that the color be generated based on the factor label. The best I could come up with was calculate a hash function for each factor label, take a substring of it and use it as the color hex. Seems to work, but far from elegant. – Nikolai Jul 27 '20 at 16:02
  • @r.user.05apr It's a part of a shiny app that analyzes an incoming stream of data where the factor variable has hundreds of possible values (and I don't have the complete list upfront). So it has to be automatic. – Nikolai Jul 27 '20 at 16:06
  • @fullera can you elaborate? what's colourlist, what's orderlist? – Nikolai Aug 04 '20 at 16:51

0 Answers0