R中绘图包之间的关系
我刚刚开始使用 R,并开始制作图表。我知道至少有三种不同的绘图包,标准的、ggplot2 和lattice。
这些软件包是互补的,还是涵盖了大部分相同的领域?如果它们是互补的,我什么时候使用它们?如果它们涉及相同的领域,那么作为 R 新用户,我应该将精力投入到母带处理中?
I am just starting out with R, and beginning to start producing charts. I am aware that there are at least three different plotting packages, the standard one, ggplot2 and lattice.
Are these packages complementary, or do they cover much the same ground? If they are complementary, when do I use each? If they cover the same ground, which one should I, as a new R user, put my energy into mastering?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有 4 个绘图系统。有标准、网格、格子和ggplot2。后两者是建立在前两者基础上的更高层次的系统。每个都有优点和缺点。
标准图形使您可以对绘图进行绝对控制,并且非常适合按照您喜欢的方式制作一个绘图。 Lattice 的开发是为了解决您需要绘图数组的情况。它非常灵活,可以在数据和任何变量上绘制大多数函数。如果您想要将任意函数应用于每个受试者的数据并以网格图的形式呈现,那么点阵就是您的最佳选择。它建立在网格之上,并且几乎是任何人使用网格包的唯一方式。
最新的 ggplot2 既是一个绘图包,也是绘图中的一种新哲学。它基于 Wilkinson 的“图形语法”,并尝试准确地做到这一点,生成图形语法。人们只需学习诸如 geom(绘制的内容)、stat(数据统计)、facet(单独的面板)等术语的高级语法,就可以构建非常复杂的图表。它们通常非常可爱,特别是对于电子发行而言。不幸的是,无法对每个细节进行精细控制。有些事情你根本无法调整。也就是说,许多人得出了一个合理的结论:用简单的方法来描述高质量的图只是付出很小的代价。
查看 ggplot2 的一些默认图表和示例图表。如果它们对您有吸引力,那么我建议您从那里开始。如果可以的话,尝试学习通过基本语法方法来做所有事情。我个人认为Hadley将便利功能作为网站的主要帮助是一个错误。它似乎破坏了 ggplot2 的整个目的。缩写语法作为帮助的主要界面提供,但本书的全部内容都是关于哲学和基本语法的。
(我这么说,但我大部分绘图都是在基本图形中进行的,因为我发现构建图形的每个组件都很有趣。)
There are 4 plotting systems. There is standard, grid, lattice, and ggplot2. The latter two are higher level systems built on the former two. Each has advantages and disadvantages.
Standard graphics gives you absolute control over plots and is great to make one plot just the way you like it. Lattice was developed to address situations where you want arrays of plots. It is very flexible and can plot most any function over your data and over any variable. If you want an arbitrary function applied to each subject's data and presented as a grid of plots, lattice is your baby. It's built on grid and almost the only way anyone uses the grid package.
The latest one, ggplot2, is both a graphing package and a new philosophy in graphing. It is based on "The Grammar of Graphics" by Wilkinson and attempts to do exactly that, generate a grammar for graphics. One merely has to learn the higher level syntax of terms like geom (what you plot), stat (statistics on the data), facet (individual panels), and you can construct very complex graphs. They generally come out quite lovely, especially for electronic distribution. Unfortunately, fine control of each individual detail is not available. There are certain things you simply cannot adjust. That said, many have come to the sane conclusion that it's a small price to pay for the easy way to describe high quality plots.
Have a look at some of the default and example graphs for ggplot2. If they appeal to you then I'd suggest you start there. If you can, try to learn to do everything through the basic grammar method. I personally think it's a mistake that Hadley has the convenience functions as the main help on the website. It seems to undermine the whole purpose of ggplot2. An abbreviated syntax is presented as the primary interface for the help but the book is all about the philosophy and fundamental grammar.
(I say that but I do most of my plotting in base graphics because I find it fun building every single component of the graphs.)
这三个包实际上是三个不同的绘图概念。如果您知道自己在做什么,则标准绘图设备运行得很快,并且在我看来相当直观,因为所有命令都处理图形的特定元素(绘图、标题、轴、标签、图例等)。 .)。这些图表的布局非常灵活,我发现它们对于大多数标准统计绘图和相当简单的二维图表很有用。
Lattice 是一种基于网格的绘图设备,具有更大的灵活性。您基本上可以绘制任何您想要的内容,如 R Graph Gallery 所示。还可以对图表进行注释。在我看来,Lattice 对于可定制的高端图表非常有用。我主要在制作彩色地图、3D 可视化和不同数据子集的组合图时使用它(例如,在模型构建中,用于不同术语的效果)。另请参阅在 R 中使用点阵图形。
ggplot2 实际上是结合这两个系统优点的尝试:您保留了点阵的大部分灵活性,但您可以像使用标准绘图一样处理轻松定义的图形对象。图形输出通常比标准包中的标准设置更好看,并且比点阵更少麻烦。就我个人而言,我对 ggplot2 的经验很少,但它看起来绝对有前途。我仍然更喜欢基本图形包,但这可能更多地与我习惯了这个事实有关。老猴子和新把戏等等......
基本上,我会说选择你觉得最舒服的方式。我肯定会至少学习标准包的基础知识,但从那里我想 ggplot2 可以为您提供所需的一切,如果您不打算使用高调的图形。如果你真的想制作定制的复杂图,了解lattice只会对你有所帮助。但除此之外,其他两个提供了您需要的一切,并且使用起来相当直接。
The three packages are actually three different plotting concepts. The standard plotting device goes fast if you know what you're doing, and is -in my eyes- rather intuitive in the sense that all commands deal with particular elements of a graph (plot, title, axis, labels, legend, ...). These graphs are pretty flexible in layout, and I find them useful for most of the standard statistical plotting and fairly straight-forward 2D graphs.
Lattice is a grid-based plotting device, and allows a lot more flexibility. You can basically plot anything you want, as shown on the R Graph Gallery. Graphs can also be annotated. Lattice is -again, in my eyes- very useful for customizable high-end graphs. I use it mainly when I'm making color maps, 3D-visualizations and combined graphs of different subsets of my data (e.g. in model building, for the effects of the different terms.). See also Using lattice graphics in R.
ggplot2 is actually an attempt to combine the good of both systems : you keep most of the flexibility of lattice, but you work on easily defined graph objects as you do with the standard plotting. The graphical output is in general better looking than the standard settings in the standard package, and with less hassle than with lattice. Personally, I have only little experience with ggplot2, but it looks definitely promising. I still prefer the base graph package, but that has probably more to do with the fact that I'm used to that one. Old monkeys and new tricks and all that...
Basically, I'd say to go with what you feel most comfortable with. I'd definitely learn at least the basics of the standard package, but from there I guess ggplot2 gives you everything you need if you're not going into high-profile graphics. If you really want to make customized complex graphs, getting to know lattice can only help you. But otherwise, the other two provide everything you need, and are fairly straight in use.