How to choose pch symbols for a factor in R?

12.1k Views Asked by At

I have tried to find out how to choose the symbols used for the factorial parameter in a case like this:

plot(data.3$age,data.3$tl,pch=c(data.3$mu),col=c(data.3$mu),
cex=1.5,cex.lab=1.3,cex.axis=1.5,las=1,bty="n",xlab="Age (years)",
ylab="Male Total Length (mm)",ylim=c(0,780),xlim=c(0,20))

I have used pch=as.numeric(factor) but I want to be able to choose the symbols myself because I find the default ones quite difficult to distinguish from each other.

I guess it's fairly simple but I have really tried to find out how to do this.

3

There are 3 best solutions below

1
On

Two answers, the choice of which depends on how your factor levels are set up:

First, if your factor levels start at 1, and are sequential after that (so factors levels are 1,2,3,4 in the example below):

plot(data.3$age,data.3$tl, pch = c(21,10,22,7)[as.numeric(pch=c(data.3$mu))] )

Second, if your factor levels are not sequential (say you have subsetted a larger data set, with 7 factors, but you are only interested in plotting 4 of them):

plot(data.3$age,data.3$tl, pch = c(21,10,22,7)[as.numeric(as.factor(as.character(pch=c(data.3$mu))))] )
0
On

A graphical representation of the pch symbols and the associated values can be found on many pages, e.g., here.

enter image description here

3
On

You can see the values available for pch in this plot. (Make the figure window wide for best viewing.)

i <- -128:25
plot(i, pch = i, bg = "blue")

Negative numbers are ASCII values.


I think that this is what you are asking for, but I don't know why you are drawing a plot like this. A set of histograms or boxplots would make more sense.

dummy <- data.frame(
  x = 1:20, 
  y = factor(sample(letters[1:4], 20, replace = TRUE))
)
pch_lookup <- c(a = 3, b = 10, c = 15, d = 20)
with(dummy, plot(x, y, pch = pch_lookup[as.character(y)]))