#create some sample data
variable1<-rnorm(200,sd=3) #200 random points from normal dist
variable2<-variable1 * .4 + rnorm(200,sd=.5) #correlated variable
category<-sample(c(“A”,”B”,”C”),200,replace=T) #categorical variable
myDF<-data.frame(variable1,variable2,category) #put in data frame
rm(list=c(“variable1″,”variable2″,”category”)) #delete unneeded variables
So now we have a single object (a dataframe) which contains two correlated continuous variables and one categorical variable. Using the qplot() function from ggplot we can make a quick scatterplot of the continuous variables that is color coded by the categorical variable.
That creates this nice looking default plot.
Now….lets say that you don’t want a solid gray background, and you want a bigger default font size. These are really common tweaks, so ggplot2 makes it easy to change the default “theme” in order to get white backgrounds and custom text sizes. You can set this globally for an entire R session with the following command.
Then we can reuse the single line of code we already used to make a scatter plot, and the “black and white” theme will be applied, along with a larger base font size (the default is 12, so we have set it to be twice as large as the default). Note that the different font sizes of axes, tick marks, legend titles, etc are automatically calculated relative to the base size that we set to 24.
It is easy to change theme elements for a single plot, rather than changing them globally as we did before with theme_set(). If I decide I want to go back to the default gray theme for any given plot, I can simply add a theme element to the plot object produced by the qplot() function.
Note: I save the result of the qplot() function into a variable called myPlot. This doesn’t result in anything getting printed to the screen, it just saves the ggplot object in a variable, that I can print whenever I want and manipulate to my heart’s content. This is a useful way to avoid typing commands again. The plot gets printed to the screen in the second line of code.
myPlot + theme_gray(12)
This produces a boxplot with a gray background, because we have added the theme_gray(12) theme element to the plot. Note that this command also resulted in a smaller base font size. Also note that the next plot we do will have a white background and the larger base font size, because the theme_set() command we did earlier is still in effect globally.
Changing individual theme elements
Although all I did was add the function theme_gray(12) to the previous plot plot, ggplot2 did several things. It changed the background to gray AND the gridlines to white (rather than black gridlines and white background), AND changed the base font size. It did these several independent things behind the scenes, but you can of course manually change all the theme elements yourself. Lets say you like super-heinous-ugly plots….you might want to do the following.
- theme_blank() – used to prevent an element from being drawn
- theme_text() – used to tweak text and labels
- theme_line() & theme_segment() – for drawing lines and segments for things like axes
- theme_rect() – for things like the overall plot background and the plot panel background
There are various parameters you can tweak for these different theme functions look up the help file for the one you need. For example ?theme_text
Annotating a Plot
We can use the annotate() function as a quick way to add text to our plot. I position the text label by specifying the coordinates where I want the center of the text, in this case centered at 0 on the x-axis and at the maximum y value. Specifying x and y coordinates works on a boxplot also….just think of the categories as sequential integers.