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)
 ______________________________________________________
|                                                      |
|                       *                              |
|                       **                             |
|                      ****                            |
|                      *  *                            |
|                      *  **                           |
|                      *   ***                         |
|                      *     **                        |
|                     *       *                        |
|                     *       *                        |
|                     *        *                       |
|                     *        *                       |
|                     *        *                       |
|                    *          *                      |
|                    *          *                      |
|                    *          **                     |
|                    *           *                     |
|      ***          **           **                    |
|     ** **         *             **                   |
|    **   **     ***               **      ****        |
|  ***     *******                  ********  *******  |
|______________________________________________________|

7 thoughts on “ASCII Scatterplots in R

  1. Awesome! 😀

    Might be also useful for people working on text terminals when displaying usual R graphics is impossible and printing to PDF/Postscript is not an option.

  2. That's helpful; thanks. It's reminiscent of the "dumb" terminal in Gnuplot, which I've used to craft text-based emails that contained graphs.

  3. Thanks to all!

    Bill, I didn't know that Gnuplot had a "dumb" terminal. That's a thought, maybe R should have an Gnuplot graphics driver. I'm sure it's much more mature than 'scat'. I believe octave uses Gnuplot by default.

    Tal, that's great, I hope it's helpful. I did see your post. See also what I mentioned to Bill above.

Comments are closed.