Why does rerunning function that creates a graph modify the existing graph?

I am writing a function that inputs two waves and plots them on the same graph. Then I need to truncate the data and change the color and replot the new data on the same graph. When I try to run the function it throws an error when I try to change the color of the truncated data and says "trace is not on graph". When I run each line of the code individually with the actual wave names it works fine. Also, when I rerun the function after fixing the current graph is plots data on the previously created graph before creating a new graph for the new waves. Basically I just want to be able to throw two waves of data into the function and for it to pop out a pretty graph with the two colors on it. The code I am using along with the data file and some graphs are below.

Function CountvsPress (w0,w1, waveout)
    wave w0, w1, waveout
    string outwave
    Display w0 vs w1
    ModifyGraph mode=3,marker=8,msize=2,opaque=1
    ModifyGraph msize=1
    ModifyGraph marker=0
    ModifyGraph grid(bottom)=1
    ModifyGraph minor(bottom)=1
    duplicate/o w1 w1_test
    w1_test = w1_test*(x>4000)
    AppendToGraph w0 vs w1_test
    ModifyGraph rgb(w1#1)=(24576,24576,65280)
    duplicate/o w1 w1_test
    w1_test = w1_test*(x>7000)
    duplicate/o w1, $outwave
    wave waveout = $outwave
    display waveout
End


In the attached picture the graph on the left is the correct graph this is how I want it to look. The graph on the right is after the function is rerun. The blue data is different. The attached file is the data file I am using. I am very new to coding any help getting this to do what I want is appreciated.
BRI1999.txt (869.14 KB)
I believe I originally had the w0#1 in there but it gave me the same error (trace is not on graph) so I tried w1#1.

The final display command was supposed to display the newly created graph which wouldn't be overwritten by a subsequent use of the function.
Boy, was I stupid. This is a really common confusion and I've answered that question hundreds of times...

The trace isn't called w0#1, it is given a name that depends on the *actual* wave name. You can synthesize the trace name using something like
    String tname = NameOfWave(w0)+"#1"
    ModifyGraph rgb($tname)=...

And in recent versions of Igor you can give a trace a name of your choosing using the /TN flag:
AppendToGraph w0/TN=trace2 vs w1
At least that's how I recall it working :)

Sorry for my brain fart.

John Weeks
WaveMetrics, Inc.
support@wavemetrics.com
Thank you that fixes the problem with the color of the second trace but it still seems to change the previous graph when I run the function a second time. It changes only the second trace added. It seems to change the blue data on the old graph and also add it to the new graph. So the function works perfectly for a newly created graph but it changes the previous graph. I don't understand why it would change a graph that was already created.
This line:
        duplicate/o w1 w1_test
makes a duplicate of whatever wave w1 points to, and calls the duplicate "w1_test". Every time you run the function, it makes the wave with that name and overwrites the previous one. You need to make a wave with a unique name, possibly like this:
    String newname = UniqueName(NameOfWave(w1)+"test", 1, 0)
    Duplicate w1, $newname/WAVE=w1_test

If w1 refers to a wave called, for instance, "wave0", the call to UniqueName will generate a name like "wave1" or whatever unused name is next in that series. If w1 is called something without a number suffix, then the first time UniqueName will just add "0" to the name to make it unique.

John Weeks
WaveMetrics, Inc.
support@wavemetrics.com