Choosing between qplot() and ggplot() in ggplot2
RGgplot2R Problem Overview
I'm starting to use the great ggplot2
package for plotting in R, and one of the first things I ask myself before each plot is "well, will I use qplot
or ggplot
?"
I understand that qplot
provides a simpler syntax while ggplot
allows maximum features and flexibility, but what is the function you use the most, and do you have some precise use cases for each one ? Do you use mostly qplot
and ggplot
only for complex plots, or do you use ggplot
everytime ?
Thanks for your feedback !
R Solutions
Solution 1 - R
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.
Solution 2 - R
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.
Solution 3 - R
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
.
Solution 4 - R
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.
Solution 5 - R
- 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()
Solution 6 - R
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!)