Site icon R-bloggers

ASCII Scatterplots in R

[This article was first published on BioStatMatt » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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 ):

> 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 their blog: BioStatMatt » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.