ASCII Scatterplots in R

June 25, 2010
By

(This article was first published on BioStatMatt » R, and kindly contributed to R-bloggers)

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

To leave a comment for the author, please follow the link and comment on his blog: BioStatMatt » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Tags: , ,

Comments are closed.