1

I wanted to use par(new = TRUE) to plot my plots which were generated in a loop over another. But how do I make sure, that every new plot has a another color? It is all black and overlayed now, so that I cannot distinguish the plots anymore:

for(i in names(extensor_raw[2:9])){
  # Coerce a data.frame into an 'emg' object
  x <- as.emg(extensor_raw[i], samplingrate = 1000, units = "mV")  ##do this for every channel
  
  # Compute the rectified signal
  x_rect <- rectification(x)
  
  # Filter the rectified signal
  y <- lowpass(x_rect, cutoff = 100)
  
  # plot the original channel, the filtered channel and the LE-envelope
  plot(y, main = paste("LE-envelope"))
  par(new = TRUE) 
}

enter image description here

I tried to implement the following: I tried to include col = rainbow(9)[i], but this only gives me a blank plot:

plot(y, main = paste("LE-envelope") , col=rainbow(9)[i])
par(new = TRUE) 

enter image description here

As asked for in the comments I have used dput(y) (not sure whether the postion was right:

for(i in names(extensor_raw[2:9])){
  # Coerce a data.frame into an 'emg' object
  x <- as.emg(extensor_raw[i], samplingrate = 1000, units = "mV")  ##do this for every channel
  
  # Compute the rectified signal
  x_rect <- rectification(x)
  
  # Filter the rectified signal
  y <- lowpass(x_rect, cutoff = 50)

  # plot the original channel, the filtered channel and the 
  # LE-envelope
  plot(y, main = paste("LE-envelope extensor") , col=rainbow(9)[i])
  par(new=T) 
}
    
dput(y)

That is the result:

enter image description here

  • there is still the empty plot
slamballais
  • 3,161
  • 3
  • 18
  • 29
cel
  • 15
  • 3

2 Answers2

0
plot(y, main = paste("LE-envelope", i), col=rainbow(9)[i]) 
G5W
  • 36,531
  • 10
  • 47
  • 80
  • Hello, thanks for your fast answer. Sadly, this did not work because I get a blank image then with no plots at all instead of black or even rainbow ones. I think it might have to do with how the loop is iterated. But I could not figure the problem out. – cel Jun 05 '21 at 13:39
  • You put the parentheses in the wrong place. You wrote `plot(y, main = paste("LE-envelope", col=rainbow(9)[i]))` but it should be `plot(y, main = paste("LE-envelope"), col=rainbow(9)[i])` – G5W Jun 05 '21 at 13:41
  • Yes, thank you, I see that with my code and that mistake I get only black plots (because it's referring to the title) I have corrected it to your version but then I get a blank plot. I have updated it in my question above – cel Jun 05 '21 at 13:46
  • After you run your code, `y` should have the last value it had during the loop, the one for `names(extensor_raw[9]`. Please run `dput(y)` and paste the result in your question so that we can test this out. – G5W Jun 05 '21 at 14:05
  • Thanks for the fast answer, I am not quiet sure where the dput(y) should be placed? – cel Jun 05 '21 at 14:10
  • After you run your loop, just run that code. If you want, you could put it just after the `}` that ends the loop. – G5W Jun 05 '21 at 14:18
0

You defined the for-loop to iterate over names(extensor_raw). That means that i will be a character in every loop. However, to define any color, you need a numeric value. The solution is to iterate over an integer sequence instead:

seq_along(names(extensor_raw[2:9]))
# [1] 1 2 3 4 5 6 7 8

That way, col = rainbow(9)[i] will work, e.g.:

i <- 5
rainbow(9)[i]
# [1] "#00FFAA"

I also modified the code to work with a single plot call. From there, we just add lines with the lines function. I do this with an if-else statement.

Furthermore, we'd need to define the y range properly. So, we can do:

y_range <- c(0, max(extensor_raw[2:9]))

We can then use y_range in plot.

The full code:

for(i in seq_along(names(extensor_raw[2:9]))) {
  x <- as.emg(extensor_raw[i], samplingrate = 1000, units = "mV")  ##do this for every channel
  x_rect <- rectification(x)
  y <- lowpass(x_rect, cutoff = 100)

  if (i == 1) {
    plot(y, main = "LE-envelope", col = rainbow(9)[i], ylim = y_range)
  } else {
    lines(y, col = rainbow(9)[i])
  }
}
slamballais
  • 3,161
  • 3
  • 18
  • 29