Thursday, December 27, 2012

Mixing symbols and characters in plots

When you pass integers to the pch parameter of a plot you get the nice R symbols drawn on your plot.
Alternatively you can pass characters and have them used as the plotting symbol.
But what if you want to mix symbols and characters?

Here it is: the example uses the first two symbols (circle and triangle) and the letter "c" as plotting symbols.


# generate some data
foo <- data.frame(x = rnorm(100, 0, 1), y = rnorm(100, 0, 1), grp = letters[1:3][c(runif(100, 1, 4))])
 
# plot them with lattice
library(lattice)
xyplot(y ~ x,data = foo, groups = foo
  , pch = c(1, 2, as.integer(charToRaw("c"))) 
)
Created by Pretty R at inside-R.org

The trick is done by the charToRaw function.

1 comment: