[R] Color & pch functionalities in dotchart()

Duncan Murdoch murdoch.duncan at gmail.com
Tue Nov 3 16:46:52 CET 2015

On 03/11/2015 8:58 AM, GINGINS Simon wrote:
> Hi,
> I am currently building graphs using dochart(). I plotted the points in two different colors according to specific criteria from my dataset. Later, I decided to also give them different symbols, for easier reading if printed. I got quite a surprise when I realized that, even though I gave the same variable to both color= and pch= , I got a mix of the colors and symbols. I wanted one type of symbol to be only one color, and the second only the other color, and it is clearly not what happened. From my data, I would say that the pch= argument is not dealing properly with the variable I gave it. I tried using a dataset from R to see if this was only in my dataset, and it did the same. here’s a reproducible example, using beaver1:
> dotchart(beaver1$temp, groups=factor(beaver1$day), color=as.factor(beaver1$activ), pch=beaver1$activ)
> Does anyone know why, with the same variable, the arguments color & pch give different results?
> That would be of great help, since now I am no longer sure which one isthe correct representation of my data.
> Best regards & many thanks for the help

Looks like a bug in dotchart.  When you specify groups, it sorts the 
data to put it into groups.  It also sorts the colors, but it doesn't 
sort the pch values, so they end up associated with the wrong points.

A workaround would be for you to do the sorting in advance.  For example,

groups <- factor(beaver1$day)

o <- sort.list(as.numeric(groups), decreasing = TRUE)
tmp <- beaver1[o,]

dotchart(tmp$temp, groups=factor(tmp$day), color=as.factor(tmp$activ), pch=tmp$activ)

Duncan Murdoch

More information about the R-help mailing list