Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
433 views
Welcome To Ask or Share your Answers For Others

1 Answer

Here is one simple example:

plot.new()
tmp <- capture.output(stem(iris$Petal.Length))
text( 0,1, paste(tmp, collapse='
'), 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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...