qplot()
qplot() is one of the functions used to create plots by using the ggplot2 package. There are a variety of options for this function, so we will be introducing these piecemeal. A basic starting point is the following syntax
qplot(x=, y=, data=, geoms=, xlab=, ylab=, main=…)
Let’s break down each option of the qplot().
- x= The associated data for the x axis of your figure goes here
- y= The associated data for the y axis of your figure goes here
- data= The name of the object where the data is contained goes here
- geoms= The type of geometric object that you would like to produce goes here. They must be in parentheses. Some of the shapes are the following:
- histogram
- point
- density
- path
- boxplot
- jitter
- xlab= The title for the x axis goes here
- ylab= The title for the y axis goes here
- main= The title for the entire graph goes here
Let’s see an example of this in action. To do this example, you will need knowledge on how to import an R file into R. If you are not sure on how to do that, please click here.
Example I
Pokemon is a lucrative and diverse franchise. One of foundation of their success is the video games that they produce. In these games, trainers capture these creatures and go on adventures while growing along the way.
One of the specific features of the new video games is the ability to Wonder Trade. Wonder Trade randomly matches two willing individuals to exchange Pokemon. Tyler Stark (aka The Iron Developer) performed an experiment trading 300 of one Pokemon. He then recorded the data. An edited version of the data is provided here.
Create a histogram of the amount of waiting time for the wonder trade to occur using the qplot().
Answer I
We created a histogram of the data, which is provided in Figure 1. The code to produce this figure is provided below Figure 1. But first we looked at the top few rows of the data after loading it into R. We did this to check what the data looked like and what rows and columns were entitled. The output in R is provided below.
Notice in the code that we only had to put the name of the column of data for the x axis. We do not need to use both the y and x options of the function. Additionally, the standard option for geom when nothing is specified is the “histogram”.
load('WTE.RData') # load the data into R ls() #check what objects we just loaded into R head(WTE) #check the top of the data to see what it looks like qplot(x=waitTime..seconds., data=WTE, geoms='histogram', xlab="Wait Time (sec)",ylab='Count', main='Histogram of \n Waiting Time for a Wonder Trade') #notice that the x data is the name of the column #notice that in the main header we added \n. This tells R to go to the next line.
Adding Themes
We can add themes, or slight changes to the function’s output by adding a bit of code to the function. There are a variety of things that one can change to the final graphic produced such as the background color, font size, graphic colors and a variety of other changes. You can go to http://docs.ggplot2.org/current/theme.html to find the complete listing of what you can change. The function you use is
theme()
In the beginning, the best way of understanding how to change certain aspects of graphic produced is to simply do examples.
Example II
Produce a graphic with the boxplots of waiting time by country. Show the graphic produced with minimal changes made. Then change the titles and the orientation of the country markers to be legible. Change the orientation by using the theme().
Answer II
Here we provide the standard produced graphic in Figure 3. The improved graphic is shown in Figure 3. The code to produce these images is provided below Figure 4. They have comments, explaining each part of the code. Figure 5 shows the output in R.
qplot(x=trainerLocation, y=waitTime..seconds., data=WTE, geom="boxplot") #notice how we cannot read which countries are where. They look like a garbeled mess. We need to fix that. qplot(x=trainerLocation, y=waitTime..seconds., data=WTE, geom="boxplot", xlab="Trainer Location", ylab="Waiting Time (Seconds)", main= "Boxplots of Waiting Time by Country")+theme(axis.text.x = element_text(angle = 90, hjust = 1)) #notice that we changed the titles the same way we did in Example I # we used the theme() to change the orientation of the names of the countries. We did this by specifying the axis text for the x axis using axis.text.x # next we used the function element_text() to change the angle by 90 degrees. Lastly, we adjusted the height by 1.
We will try to provide more examples of how to change specific parts of the figures. However, a really good way to learn how to change specific things in a figure is to play around with the options. Again, go to http://docs.ggplot2.org/current/theme.html to learn more about the different things that you can change for a graphic that you make.