10

Is there a way to output a stem and leaf plot to a graphical device, such as window() / quartz()? There are at least two ways to get stem and leaf plots in R: ?stem, in the graphics package, and ?stem.leaf, in the aplpack package. Both output text to the console. For example:

> set.seed(1)
> stem(rbinom(10, size=10, prob=.5))

  The decimal point is at the |

  3 | 0
  4 | 000
  5 | 0
  6 | 00
  7 | 000

It would be nice if this could be conveniently output to a graphical device where it could be combined with other plots (say a histogram) in a multi-figure layout, and/or saved as a png file. I am aware that you can output LaTeX and compile it into a pdf (e.g., see: Stem and Leaf from R into LaTeX), but this isn't very convenient and isn't really what I'm after. Is there an R function that can do this? Is there a simple hand-coded solution?

Community
  • 1
  • 1
gung - Reinstate Monica
  • 11,583
  • 7
  • 60
  • 79
  • Take a look at `package:ascii` – IRTFM Oct 23 '14 at 16:30
  • The pdf documentation on CRAN is pretty sparse, @BondedDust. Are you thinking of `fig()` to capture output & save it as png? Could it, or another function, output to a standard graphics window? – gung - Reinstate Monica Oct 23 '14 at 16:37
  • Well, not really. I was thinking you would need to send the results of `capture.output()` to some mark(up/down) facility. I agree it's kind of painful to try to do any ascii-printing to the default plot device. I can do it, but only with pain. – IRTFM Oct 23 '14 at 16:42

2 Answers2

13

Here is one simple example:

plot.new()
tmp <- capture.output(stem(iris$Petal.Length))
text( 0,1, paste(tmp, collapse='\n'), adj=c(0,1), family='mono' )

enter image description here

If you want to overlay a histogram then you probably want to use the text function on each of the elements of tmp rather than pasteing. Functions like strheight and strwidth will be useful to find the coordinates.

There are also functions in the gplots and plotrix packages for plotting text and adding tables to plots (other functions in other packages probably exist along these lines as well).

Andrie
  • 176,377
  • 47
  • 447
  • 496
Greg Snow
  • 48,497
  • 6
  • 83
  • 110
  • 1
    This just gave me a flashback to my C programming course. – Rich Scriven Oct 23 '14 at 17:09
  • This was essentially what I was calling the "painful" approach. I think you also described (a few years ago) a facility to send to gnuplot. – IRTFM Oct 23 '14 at 17:39
  • @BondedDust, I don't think the above is too painful. Trying to line everything up with a histogram would start being uncomfortable, but I wouldn't consider it painful. – Greg Snow Oct 23 '14 at 17:53
  • My comment was too negative. I did upvote both your answer and the question as useful. – IRTFM Oct 23 '14 at 18:03
  • That's great, Greg. Thanks a lot. I also incorporated it into a function `stem.plot <- function(x){ ... }` and it works great. – gung - Reinstate Monica Oct 23 '14 at 18:07
4

Following is equivalent:

set.seed(1)
xx = rbinom(10, size=10, prob=.5)
barplot(t(table(xx)), horiz=T)

enter image description here

For even more similar:

set.seed(1)
xx = rnorm(10)

xxch = as.character(xx)
ff = sapply(strsplit(xxch, '\\.'), function(x) x[1])
ss = sapply(strsplit(xxch, '\\.'), function(x) x[2])
first = sapply(strsplit(ss, ''), function(x) x[1])
second = sapply(strsplit(ss, ''), function(x) x[2])
third = sapply(strsplit(ss, ''), function(x) x[3])
dd = data.frame(ff, first, second, third)
dd = cbind(dd[1], sapply(dd[-1], as.numeric))
ddt = data.table(dd)
gg = ddt[order(ff,first)][,paste(first, collapse=""),by=ff]
gg$rr = rownames(gg)
ggplot(gg)+geom_text(aes(x=rr, y=1, label=paste(ff,'|',V1))) +
theme(axis.text = element_blank(),axis.title = element_blank(), axis.ticks=element_blank()) +
coord_flip()+ labs(title="Decimal is at |")

enter image description here

The code may need to be tweaked for different sets.

Using capture.output (as suggested by @Greg) and plotting with ggplot:

tmp <- capture.output(stem(iris$Petal.Length))
stemdf = data.frame(tmp, rr=1:length(tmp))
ggplot(stemdf)+ geom_text(aes(x=rr, y=0, label=tmp), hjust=0) + 
    coord_flip()+ theme_classic() + 
    scale_x_discrete(breaks=NULL)+ 
    scale_y_discrete(breaks=NULL, limits=c(0,1))+ 
    theme(axis.text = element_blank(),
        axis.title = element_blank(), 
        axis.ticks=element_blank(), 
        panel.grid=element_blank(), 
        axis.line=element_blank())

enter image description here

rnso
  • 23,686
  • 25
  • 112
  • 234