Hi, Is there a way to plot non numerical data in R? Specifically, I have an array, say with 1000 entries, where each entry is a string of 4 characters (in any order, 24 possibilities in my case). I would like on the y-axis all the strings that are in the array as labels. The x-axis I would like labeled 0 to 1000. The line is to show how the strings change as we move through the array. (This is essentially a trace plot of a Markov Chain Monte Carlo algorithm. I think essentially I would like an equivalent to plot(myArray, type='l') but with discrete, finite, non-numerical values on the y-axis) Is this possible? Michal.
On Tue, 2007-11-20 at 00:37 +0000, Michal Charemza wrote:> Hi, > > Is there a way to plot non numerical data in R? > > Specifically, I have an array, say with 1000 entries, where each entry > is a string of 4 characters (in any order, 24 possibilities in my > case). I would like on the y-axis all the strings that are in the > array as labels. The x-axis I would like labeled 0 to 1000. The line > is to show how the strings change as we move through the array. > > (This is essentially a trace plot of a Markov Chain Monte Carlo > algorithm. I think essentially I would like an equivalent to > plot(myArray, type='l') but with discrete, finite, non-numerical > values on the y-axis) > > Is this possible? > > Michal.For some pre-existing possibilities, you might want to look at the following CRAN packages: MCMCPack mcmc coda scapeMCMC Alternatively, consider coercing your character values to a factor, predicated upon some relevant ordering. You can then plot the numeric values of the factors for your y axis. Simple example: Y <- sample(LETTERS, 1000, replace = TRUE) plot(1:1000, factor(Y), type = "l", yaxt = "n") axis(2, at = 1:26, labels = LETTERS, cex.axis = 0.75) HTH, Marc Schwartz
Here is one way to plot your data: # generate 24 cases of 4 letter character strings x <- expand.grid(c("A", "B"), c("C","D"), c("E","F"), c("G","H","I")) x <- apply(x, 1, paste, collapse='') # data to plot y <- factor(sample(x, 200, TRUE)) par(mar=c(5,6,1,1)) plot(seq_along(y), y, yaxt='n', type='l', ylab='') axis(2, at=seq_along(levels(y)), labels=levels(y), las=2) On Nov 19, 2007 7:37 PM, Michal Charemza <m.t.charemza at warwick.ac.uk> wrote:> Hi, > > Is there a way to plot non numerical data in R? > > Specifically, I have an array, say with 1000 entries, where each entry > is a string of 4 characters (in any order, 24 possibilities in my > case). I would like on the y-axis all the strings that are in the > array as labels. The x-axis I would like labeled 0 to 1000. The line > is to show how the strings change as we move through the array. > > (This is essentially a trace plot of a Markov Chain Monte Carlo > algorithm. I think essentially I would like an equivalent to > plot(myArray, type='l') but with discrete, finite, non-numerical > values on the y-axis) > > Is this possible? > > Michal. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?