在 ggplot2中选择 qplot()和 ggplot()

我开始使用了不起的 ggplot2软件包在 R 中进行绘图,在每个绘图之前我首先问自己的问题之一就是 “那么,我应该使用 ABC1还是 ggplot?”

我理解 qplot提供了一个更简单的语法,而 ggplot允许最大的特性和灵活性,但是什么是你使用最多的功能,你有一些精确的用例为每一个?你主要使用 qplotggplot仅仅是为了复杂的情节,还是你每次都使用 ggplot

谢谢你的反馈!

42112 次浏览

As for me, if both qplot and ggplot are available, the criterion depends on whether data is stored in data.frame or separate variables.

x<-1:10
y<-rnorm(10)


qplot(x,y, geom="line") # I will use this
ggplot(data.frame(x,y), aes(x,y)) + geom_line() # verbose


d <- data.frame(x, y)


qplot(x, y, data=d, geom="line")
ggplot(d, aes(x,y)) + geom_line() # I will use this

Of course, more complex plots require ggplot(), and I usually store data in data.frame, so in my experience, I rarely use qplot.

And it sounds good to always use ggplot(). While qplot saves typing, you lose a lot of functionalities.

I think it depends on how often and for what purpose you intend to use ggplot2.

I mainly use ggplot2 for graphics in publications. This means that I tend to need the more advanced features and so I have never bothered to learn about qplot. Also, since I have around four publications a year, I'm not using ggplot2 enough to be really comfortable with the syntax and so concentrating on a single aspect seems optimal.

However, if you get new data sets each week, then you are probably interested in quickly exploring the data sets and producing good quality plot. In this case, learn both. You will get enough practice with the syntax and will (eventually) save time with qplot.

Juba, I have found that one can use qplot for most basic plotting needs. It's sufficiently simple, and the defaults quite reasonable, that I have my undergraduate students use it exclusively and they can produce excellent plots with limited experience. And the plot created by qplot [p <- qplot(etc)] can be modified by any of the full commands ggplot2 provides, which is handy (they are all stored the same way, no matter how they were created). So personally I use qplot for most everything, and save ggplot for inside of functions.

One more variant from me: I use qplot when I'm typing directly into the console, and ggplot when I'm writing scripts. But after finding over and over again that I want to recreate a plot I typed into the console 15 minutes earlier, I write almost all of them into a script now - so I use ggplot almost all of the time.

(Interesting to see the diversity of answers!)

I am new to R but just thought of sharing this.

 a <- c(1,2,3)


b <- c(2,3,4)


x <- qplot(a,b)


y <- ggplot(data.frame(a,b), aes(a,b)) +geom_line()

If i change the value of the variables a and b and then plot x, it will take into account the changed values where as y would not. So while scripting it would be good to use ggplot as if you use qplot all the graphs will be equal to the latest provided references to qplot.

  • qplot is the simplest choice if you are dealing with input vectors
  • ggplot requires a data.frame as input data structure.

When you want to produce a histogram, qplot needs only the vector of occurrences

#rnorm
x <- rnorm(10)


#ggplot2 package: qplot
qplot(x, geom="histogram")


#ggplot2: using straight ggplot (requires conversion to data.frame)
ggplot(data.frame(x), aes(x)) + geom_histogram()