如何将茎叶图作为图输出 [英] How to output a stem and leaf plot as a plot

查看:157
本文介绍了如何将茎叶图作为图输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将茎叶图输出到图形设备(例如window()/quartz())?至少有两种方法可以在R中获得茎和叶图:?stem

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

如果可以方便地将其输出到图形设备,在这里可以将其与其他图形(例如直方图)以多图形布局组合和/或另存为png文件,那就太好了.我知道您可以输出LaTeX并将其编译为pdf(例如,请参见: Stem and Leaf from R into LaTeX ),但这不是很方便,也不是我真正想要的.是否有R函数可以做到这一点?有一个简单的手工编码解决方案吗?

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?

推荐答案

下面是一个简单的示例:

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

如果要覆盖直方图,则可能需要在tmp的每个元素上使用text函数,而不是paste ing. strheightstrwidth之类的功能对于查找坐标很有用.

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.

gplots和plotrix软件包中还具有用于绘制文本并将表添加到绘图的功能(其他软件包中的其他功能也可能沿着这些路线存在).

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

这篇关于如何将茎叶图作为图输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆