Jayden
Jayden

Reputation: 2778

R plotCI mis-assigned colors plotted for series

In R v2.14.0 x64 on Windows 7, I am using the plotCI function in the gplots library, and trying to set the colour of each plot based on data within a data frame using:

plotCI(
        x = data[1:2,3], 
        ui = data[1:2,5], 
        li = data[1:2,4], 
        col=data[1:2,6], 
        lty = 1, pch=20, xaxt ="n", xlim = c(1,42), ylim = c(0,100), 
        gap = 0 )

The plot occurs correctly except for the colour of the plotted points, which are mis-assigned to the wrong series (the colours are consistent within series however).

I have a data frame of structure (first 7 rows only):

     size qim       X1   lower    upper color
1    1000   1 100.0000 99.6000 100.0000  blue
2    1000   2  99.8000 99.4000 100.0000  blue
3    1000   3  98.2000 96.6000  99.2000  blue
4    1000   4  62.7000 58.8000  65.7000  blue
5    1000   5  10.4000  9.0000  12.5000  blue
6    1000   6   3.9000  2.9000   4.9000  blue
7    5000   1  99.9000 99.4000 100.0000   red

I sort the data frame using:

data <- data.unsorted[with(data,order(qim,size)),]

The sort appears to have happened correctly, with the resultant data frame:

 size qim       X1   lower    upper color
1    1000   1 100.0000 99.6000 100.0000  blue
7    5000   1  99.9000 99.4000 100.0000   red
13  10000   1  99.7000 99.4000  99.9000 green
19  40909   1  98.5000 98.5000  98.5000 black
25 152228   1  98.1000 98.1000  98.1000 black
31 241707   1  98.9000 98.9000  98.9000 black
37 434844   1  97.4000 97.4000  97.4000 black

In the resultant plot, the first line is plotted as red, and the second line is plotted as blue (reversed).

Is there something I'm doing wrong, or is there some other explanation for this?

Upvotes: 4

Views: 947

Answers (1)

Ben Bolker
Ben Bolker

Reputation: 226192

There's a factor/character confusion going on here. The color variable is being read into R as a factor, so its underlying numeric values are assigned according to the alphabetic order of the values: "black"=1, "blue"=2, (probably) "green"=3, "red"=4. Then colors are being mapped according to R's default palette: 1=black, 2=red, 3=green, 4=blue. This leads to the (admittedly seemingly bizarre) correspondence: "black"=black, "blue"=red, "green"=green, "red"=blue (!!). The fix is actually pretty easy: just use as.character around your color variable.

data.unsorted <- read.table(textConnection(
"    size qim       X1   lower    upper color
1    1000   1 100.0000 99.6000 100.0000  blue
2    1000   2  99.8000 99.4000 100.0000  blue
3    1000   3  98.2000 96.6000  99.2000  blue
4    1000   4  62.7000 58.8000  65.7000  blue
5    1000   5  10.4000  9.0000  12.5000  blue
6    1000   6   3.9000  2.9000   4.9000  blue
7    5000   1  99.9000 99.4000 100.0000   red"),
header=TRUE)


library(gplots)
data <- data.unsorted[with(data,order(qim,size)),]

I will also point out that a with statement makes it much easier to read the code and make sure that you're getting the right columns:

with(data[1:2,],
     plotCI(x=X1,li=lower,ui=upper,
            col=as.character(color),
            lty=1,pch=20,
            xaxt="n",
            xlim=c(1,42),ylim=c(0,100),
            gap=0))

Upvotes: 6

Related Questions