Category Archives: Uncategorized

ASCII Scatterplots in R

I really like R's stem function, it creates a stem-and-leaf plot right in the R console, no fancy graphics devices required! In a recent R-help post, Ralf Bierig presented a very nice ASCII scatterplot representing two densities. Unfortunately, I don't know of any R function that will generate this type of plot, but I will argue that they are very useful to quickly visualize data, and to present data in ASCII-only media, such as an R mailing list message.

I wrote a little prototype R function I am calling scat ( to compliment stem, and because the output may be somewhat 'crappy' ) to generate a simple ASCII scatterplot. I put the code on a wiki. Unfortunately, the spambots have forced me to disallow editing of the wiki :-(.

You can load the latest version of scat by downloading the file from the wiki, or the original from here scat.R, or within R with

> source("http://biostatmatt.com/R/scat.R")

Here are some examples (note: this doesn't work well unless you view the ASCII in a monospace font):

> data(co2) #Mauna Loa Atmospheric CO2 Concentration
> scat(c(co2[1:75]), rows=10, cols=80)
 ____________________________________________________________________________________
|                                                                                    |
|                                                         *                          |
|                                                        * *          ***            |
|                               *           ***                      *   *      * *  |
|                   **         * *         *   *        *   *      **          *     |
|      *           *          *    *      *           **         *        *   *      |
|     * *        *    *      *           *      *    *       *               *       |
|   **   *     **           *       *   *           *           *          **        |
|  *          *        *   *          **         * *          **                     |
|         *  *            *          *                                               |
|          **           **                                                           |
|____________________________________________________________________________________|

> x <- sin(seq(0,2*pi,length.out=30))
> scat(x, cols=15, rows=8)
 ___________________
|                   |
|     *             |
|   ****            |
|   *   *           |
|  *     *          |
|  *     **      *  |
|         *     *   |
|          *   *    |
|           ****    |
|___________________|

> library(MASS)
> data(galaxies) #Velocities for 82 Galaxies
> d <- density(galaxies)
> scat(d$y, d$x)
 ______________________________________________________
|                                                      |
|                       *                              |
|                       **                             |
|                      ****                            |
|                      *  *                            |
|                      *  **                           |
|                      *   ***                         |
|                      *     **                        |
|                     *       *                        |
|                     *       *                        |
|                     *        *                       |
|                     *        *                       |
|                     *        *                       |
|                    *          *                      |
|                    *          *                      |
|                    *          **                     |
|                    *           *                     |
|      ***          **           **                    |
|     ** **         *             **                   |
|    **   **     ***               **      ****        |
|  ***     *******                  ********  *******  |
|______________________________________________________|