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.

qplot(x=variable1,y=variable2,data=myDF,color=category,geom=”point”)

That creates this nice looking default plot.

Changing ThemesNow….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.

theme_set(theme_bw(24))

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.

qplot(x=variable1,y=variable2,data=myDF,color=category,geom=”point”)

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

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.

This is where the rubber hits the road for tweaking theme elements. We can change theme elements by using the opts() function to pass new values for various theme elements. There are about 30 theme elements you might want to change (listed here). In our case we changed two: the plot.background element and the panel.background element. Note the use of theme_rect(fill=”yellow”) to specify that we want a yellow plot background. Any time you tweak a theme element with the opts() command, you do so with one of five functions.

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.