0

I am working with a generic plot fxn that does not know how many lines it will plot until it's completed plotting. The data is plotting just fine, though when I try to create the legend, I'm not getting the colors.

Since the number of lines is unknown I am generating colors for the lines at random; BUT, I am storing the color in a array/vector.

From my code: I understand this is not easily reproduced, but it is not easily dumbed down, as I've gotten to the point of dumbed down and not able to find the problem...

# plot blank plot
plot(1, type="n", xlab="time elapsed [sec]", ylab="Memory Used [MB]", ylim=c(ymin,ymax),   
  xlim=c(xmin,xmax))

 # LOOOOP over column 2 (node)
for(i in 1: length(col2))
{
  if(col2[i] == compareNode)
  {
    #print(paste0("count: ", count))

    # x = time value, y = memory used value for node: compareNode
    x[count] = col1[i]
    y[count] = (col87[i] - col85[i])/1024
    count = count + 1
  }
  else
  {
    # Plot 
    colTest <- sample(colors(), 1)
    mycols <- c(mycols,colTest)
    lines(x, y, col=colTest )

    NODES[nodeCount] = col2[i]
    nodeCount = nodeCount + 1
    # Resets
    compareNode = col2[i]
    x = c()
    y = c()
    count = 1

  }
}

# Plot final line
colTest <- sample(colors(),1)
mycols <- c(mycols,colTest)
lines(x, y, col=colTest)
#print(colors)
legend("topleft", title="LDMS: Memory Used", legend = NODES, col=mycols )
garbage <- dev.off()

Trying for a better example here:

loop x times:
  color <- sample(colors(),1)
  mycols <- c(mycosl,color)
  lines(x,y,col=color)
done
lines(x,y,col=color)   # plots last set of data
legend("topright", title="title", legend=NODES, col=mycols)
BluePanda
  • 13
  • 1
  • 6
  • There are a bunch of things wrong with your code. If you don't know beforehand how long `colors` will be, how do you take care of its initialization? Second, `legend()` is perfectly able to just use a vector of color names, no need to recode it using `shQuote()`. – meuleman Aug 19 '14 at 18:19
  • You should actually provide a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) that we can run and test. Each of these random lines of code do not make much sense on their own. – MrFlick Aug 19 '14 at 18:20
  • @meuleman colors can be initiated like any other empty set colors = c() or colors <- c(). I will try to post something more reproduceable. I understand it is difficult to reproduce, but it is also hard to give you all the code as well... – BluePanda Aug 19 '14 at 18:39
  • Sure, it can, but it's at the very least bad practice to then fill it up with values by indexing non-existing elements. – meuleman Aug 19 '14 at 18:41
  • Well it is the fastest way without pre-parsing the data file to determine how many lines there will be. – BluePanda Aug 19 '14 at 18:53
  • Define `mycols <- character(0)` before your for loop. Since you only create it inside an if/else block, it won't exist outside of that block. – MrFlick Aug 19 '14 at 19:10
  • It is actually defined above the loop, sorry. should have mycols <- c(). – BluePanda Aug 19 '14 at 19:11

1 Answers1

0

Staying as close as possible to your code, and assuming we indeed don't know how many lines we're going to plot:

mycols <- c();
while (sample(1:100, 1) != 14) {
  col <- sample(colors(), 1);
  mycols <- c(mycols, col);
  ##lines(..., col=col) # plot line
}
legend("topleft", title="LDMS: Memory Used", legend = NODES, col=mycols, lty=1)

Better yet, don't just resample 1 element from colors(), but randomize the output of colors() once and then just iterate over it. This will make sure you don't inadvertently reuse any colors.

meuleman
  • 378
  • 1
  • 7
  • I like the way you are adding each color to the list much better, but I am still getting a blank legend. :( I will update the OP with more details. – BluePanda Aug 19 '14 at 18:40
  • add `lty=1` or whatever line type you're using. I've updated my answer. – meuleman Aug 19 '14 at 18:42
  • I get the warning: Warning in segments(x1, y1, x2, y2, ...) : supplied color is neither numeric nor character; still no color – BluePanda Aug 19 '14 at 18:52
  • Me neither. Please add a minimal (and working) example to your question. – meuleman Aug 19 '14 at 19:01
  • I tried to add a bit -- but it is difficult to add anything that can easily be run without my data. Dumbed down I wasn't able to find the problem... – BluePanda Aug 19 '14 at 19:08
  • Don't forget to add `lty=1` to the `legend()` call. And your new code contains a number of mistakes, most notable a confusion between `col` and `colTest`. Without a better reproducible example there's not much else we can do. – meuleman Aug 19 '14 at 19:12
  • lty=1 gives me warnings...Warning in segments(x1, y1, x2, y2, ...) : supplied color is neither numeric nor character – BluePanda Aug 19 '14 at 19:13
  • Right, because you never fill it with anything. Again, check your code for places with `colTest` and `col`. – meuleman Aug 19 '14 at 19:14
  • I don't mean for it to be confusing, I'm trying to give you as much as I can. I don't know how to give you a reproducible version. I only have what I have – BluePanda Aug 19 '14 at 19:14
  • I didn't even notice I'd typed col. My apologizes. @meuleman thanks for being patient – BluePanda Aug 19 '14 at 19:19