ggplot2 intro

Covers the basic knowledge about

1. Understanding the Ggplot Syntax:

The main difference is that, unlike base graphics, ggplot works with dataframes and not individual vectors.

# Setup
options(scipen=999)  # turn off scientific notation like 1e+06
library(ggplot2)
data("midwest", package = "ggplot2")  # load the data

# Init Ggplot
ggplot(midwest, aes(x=area, y=poptotal))  # area and poptotal are columns in 'midwest'

aes() function is used to specify the X and Y axes. Even though the x and y are specified, there are no points or lines in it. This is because, ggplot doesn’t assume that you meant a scatterplot or a line chart to be drawn. More about aes.

2. How to Make a Simple Scatterplot:

Adding points using a geom layer called geom_point. More about geom_xxx : List of geom_xxx

ggplot(midwest, aes(x=area, y=poptotal)) + 
    geom_point() 

Like geom_point(), there are many such geom layers which we will see in list.

Example: a smoothing layer using geom_smooth(method='lm').

The majority of points lie in the bottom of the chart which doesn’t really look nice. So, let’s change the Y-axis limits to focus on the lower half.

3. Adjusting the X and Y axis limits:

Method 1: By deleting the points outside the range

Method 2: Zooming In

4. How to Change the Title and Axis Labels:

Here is the full function:

5. How to Change the Color and Size of Points:

#How to Change the Color and Size To Static?

#How to Change the Color To Reflect Categories in Another Column?

As an added benefit, the legend is added automatically. If needed, it can be removed by setting the legend.position to None from within a theme() function.

Also, You can change the color palette entirely.

6. How to Change the X Axis Texts and Ticks Location:

#How to Change the X and Y Axis Text and its Location?

This involves two aspects: breaks and labels.

Step 1: Set the breaks

scale_x_continuous because, the X axis variable is a continuous variable. Had it been a date variable, scale_x_date could be used. Like scale_x_continuous() an equivalent scale_y_continuous() is available for Y axis.

Step 2: Change the labels

If you need to reverse the scale, use scale_x_reverse().

#How to Write Customized Texts for Axis Labels, by Formatting the Original Values?

* Method 1: Using sprintf()

* Method 2: Using a custom user defined function. (Formatted 1000’s to 1K scale)

#How to Customize the Entire Theme in One Shot using Pre-Built Themes?

Finally, instead of changing the theme components individually. We can change the entire theme itself using pre-built themes. The help page ?theme_bw shows all the available built-in themes. We have 2 methode: * Use the theme_set() and * Draw the ggplot and then add the overall theme setting (eg. theme_bw())

Last updated