A jitter plot represents data points in the form of single dots, in a similar manner to a scatter plot. The difference is that the jitter plot helps visualize the relationship between a measurement variable and a categorical variable. It is quite useful to evaluate the spread of the data within groups, and has the advantage of showing a lot of data points, without risking massive overlap (overplotting) and thus lack of clarity.

We will see how to use ggplot() to code for a jitter plot representing 4 groups of 150 data points each. This example is based on the same data set used to illustrate how to draw boxplots and violin plots, among others. Here are the variables and dataframe:

# dataframe
df <- data.frame(group, response)
str(df)
## 'data.frame':    600 obs. of  2 variables:
##  $ group   : Factor w/ 4 levels "Gr1","Gr2","Gr3",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ response: num  21.3 22.8 18.9 22.8 15 ...



we first map the data with aes(group, response) and we use geom_jitter() to draw the plot. The code is as follows:

ggplot(df, aes(group, response)) +     
  geom_jitter()



As you may notice, the data is well organized in four delimited areas so that the dots from adjacent groups do not overlap. The spread of the data is well represented, but the default width of these groups (or horizontal jitter) makes it slightly difficult to estimate the density. We may try to reduce the jitter by using the argument width= in geom_jitter():

ggplot(df, aes(group, response)) +     
  geom_jitter(width= 0.1)                  



Do not try to overdo it as it will result in a plot which won’t be readable:

ggplot(df, aes(group, response)) + 
  geom_jitter(width=.01)       



The shape, size and color of the dots are of course tunable with shape=, size= and color=. Check this table and pick the number that matches the shape you want.

ggplot(df, aes(group, response)) + 
  geom_jitter(shape= 18, size=1.2, color="red")        



Adding plot title, axis titles, ticks, labels and other essential elements

In this section, you will learn how to set/modify all the necessary elements that make a plot complete and comprehensible. Such elements are: