Home > other >  Assigning Unique Colors To Multiple Lines on a Graph
Assigning Unique Colors To Multiple Lines on a Graph

Time:04-08

I found this R tutorial that I would like to modify : enter image description here

  • Is it possible to assign each of these lines a unique color?
  • Is it possible to add a legend to this graph such that the legend does not interfere with the graph itself?

I tried to add a legend, but it ended up blocking the graph:

#I only showed for 8 chains...I don't think it's possible to show for all 100 chains without interfering with the graph, regardless of how the legend is placed

legend('topright', c('chain.1', 'chain.2', 'chain.3', 'chain.4', 'chain.5', 'chain.6','chain.7','chain.8'), lty=1, col=1:8)

enter image description here

  • Is it possible to assign each line a different color?
  • Is there some way to show a legend (even for 10-15 lines) in such a way that the legend does not interfere with the graph?

Thank you!

CodePudding user response:

You could use a palette, e.g. builtin rainbow. However 100 colors may not be very distinguishable.

clr <- rainbow(num.chains)  ## create `num.chains` colors
matplot(chain.states, type='l', lty=1, col=clr, ylim=c(0, 9), 
        ylab='state', xlab='time')
abline(h=1, lty=3)
abline(h=8, lty=3)

enter image description here

  • Related