R中的ggplot和basic plot有什么区别? [英] What's the difference between ggplot and basic plot in R?
问题描述
对于在R中绘制图形,我通常使用软件包graphics
中的基本曲线plot()
,barplot()
,boxplot()
...函数.但是ggplot
似乎更经常使用.
For plotting graphs in R, I usually use basic plot plot()
, barplot()
, boxplot()
... functions from the package graphics
. But ggplot
seemed to be used more often.
在选择使用哪种图形时,要考虑的两种图形之间的主要区别是什么?
What are the main differences between the two types of graphics to take into account when choosing which one to use ?
推荐答案
基本绘图范例是纸上写的",而lattice
和ggplot
范例基本上是在编写使用网格程序包完成程序的程序.低级输出到目标图形设备. ggplot
-范例具有图形语法"设计,该设计试图将各种不同的绘图功能集成到一个连贯的程序包中.它确实需要加载ggplot2
软件包,而R会以已经加载的graphics
和grDevices
软件包启动.当在函数内部使用ggplot2
和lattice
函数时,都需要使用显式的print
调用.
The base plotting paradigm is "ink on paper" whereas the lattice
and ggplot
paradigms are basically writing a program that uses the grid-package to accomplish the low-level output to the target graphics devices. The ggplot
-paradigm has the "Grammar of Graphics" design which tries to integrate a variety of different plotting functions into one coherent package. It does require loading the ggplot2
package, whereas R starts up with the graphics
and grDevices
packages already loaded. Both ggplot2
and lattice
functions require the use of an explicit print
call when they are used inside a function.
使用ggplot2
,您可以将该函数的结果分配给对象名称,然后进一步对其进行修改.准备好进行发布"时,您将处理输出并将其发送到带有打印的设备.通常,通过使用+.gg
函数,通过在使用qplot
或ggplot
创建的基本图中添加图层",可以逐步修改"ggplot"图形.
With ggplot2
you assign the result of that function to an object name and then further modify it. When it's ready for "publication" you get the output processed and sent to a device with print. "ggplot" graphics often get progressively modified by adding "layers" to a base plot created with qplot
or ggplot
through the use of the +.gg
-function.
在base
图形的情况下,没有保存结果的R对象.这些命令将立即得到处理,并刻在当前设备的纸"上.然后,您发出其他命令来增加该设备上的输出. plotrix
软件包为使用base
-graphics范例开发高级绘图设备提供了一个很好的例子.
In the case of base
-graphics there is no R object that holds results. The commands get processed immediately and inscribed on the "paper" of the current device. You then issue further commands to augment the output on that device. The plotrix
package gives a good example of the development of advanced plotting facilities using the base
-graphics paradigm.
ggplot2
功能与base
和lattice
图形功能的一个主要限制是ggplot2没有任何3D绘图功能.但是,lattice
-package并未得到积极维护,但在停止积极开发的时刻,它似乎已经相当成熟,如果您发现错误,则很可能已将其修复. gridExtra
和latticeExtra
软件包都扩展了grid和ggplot2功能.现在还有一个gridBase
程序包,它支持将基础绘图结果保存为网格"grob",然后合并基础和网格(即lattice
或ggplot
)输出.的确,"ggplot"范式似乎是近年来更持续活动的目标.
One major limitation of ggplot2
-functions versus base
and lattice
graphics functions is that ggplot2 does not have any 3D plotting functions. The lattice
-package, however, is not being actively maintained, but it seemed fairly mature at the point that active development was stopped and if you find a bug it will probably be fixed. There are both the gridExtra
and latticeExtra
packages that extend lattice and ggplot2 capabilities. There is now also a gridBase
package that supports saving base plotting results as a grid "grob" and then merging base and grid, i.e. lattice
or ggplot
, output. It is certainly true that "ggplot"-paradigm seems to be the target of more sustained activity in recent years.
这篇关于R中的ggplot和basic plot有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!