Layout Questions

Within a ggplot, almost all displayed components can be modified and adjusted. Some of these modifications will be discussed as follows.

To start, let’s create a ggplot object with our scatter plot from the introduction to ggplot:

scatter <- ggplot(
  pss, 
  aes(
    stfeco, 
    stfdem
  )
) +  
  geom_jitter(
    alpha = .2, 
    col = "blue"
  ) +
  scale_x_continuous(
    breaks = seq(
      0, 
      10, 
      1
    )
  ) +
  scale_y_continuous(
    breaks = seq(
      0, 
      10, 
      1
    )
  )

scatter
## Warning: Removed 96 rows containing missing values or values outside the scale range
## (`geom_point()`).

First, we will add titles, axis labels, and sources again.

scatterLeg <- scatter +
  labs(
       x = "Satisfaction with Economy",
       y = "Satisfaction with Democracy",
       title = "Correlation Plot",
       caption = "Data: Panem Social Survey.\n Data jittered."
       )

scatterLeg
## Warning: Removed 96 rows containing missing values or values outside the scale range
## (`geom_point()`).

Within the theme() function, we can address and modify specific components of the plot. This includes the following properties of the plot:

  • plot.title
  • axis.title.x / axis.title.y
  • axis.text.x / axis.text.y
  • panel.grid / panel.grid.minor / panel.grid.major
  • plot.background / panel.background

A complete overview of all settings that can be used in theme() can be found in the User Documentation.

We will now make changes gradually. First, we will change the font size, position, and appearance of the title. We do this using plot.title in theme(). We use the element_text() function for this:

scatterLeg +
  theme(
    plot.title = element_text(
      size = 25,
      face = "italic",
      hjust = 0.5
    )
  )
## Warning: Removed 96 rows containing missing values or values outside the scale range
## (`geom_point()`).

For this, we have used the three arguments size (font size), face (appearance), and hjust (position).

Next, we want to edit the axis titles.

scatterAxes <- scatterLeg +
  theme(
    plot.title = element_text(
      size = 25,
      face = "italic",
      hjust = 0.5
    ),
    axis.title.x = element_text(
      size = 16, 
      color = "seagreen", 
      hjust = 0
    ),
    axis.title.y = element_text(
      size = 8, 
      color = rgb(
        0, 
        105, 
        179, 
        maxColorValue = 255
      ), 
      hjust = 1, 
      face = "bold"
    )
  )

scatterAxes
## Warning: Removed 96 rows containing missing values or values outside the scale range
## (`geom_point()`).

Instead of specifying a color, you can determine the hue using the rgb() function. Alternatively, you can also use the HTML color code within the color argument.

scatterLeg +
  theme(
    plot.title = element_text(
      size = 25,
      face = "italic",
      hjust = 0.5
    ),
    axis.title.x = element_text(
      size = 16, 
      color = "seagreen", 
      hjust = 0
    ),
    axis.title.y = element_text(
      size = 8, 
      color = "#0069B3", 
      hjust = 1, 
      face = "bold"
    )
  )
## Warning: Removed 96 rows containing missing values or values outside the scale range
## (`geom_point()`).

Now, let’s continue experimenting and edit the axis ticks. For this, we use axis.ticks.x or axis.ticks.y.

scatterTicks <- scatterAxes +
  theme(
    axis.text.x = element_text(
      size = 12, 
      angle = 45,
      color = "darkgrey"
    ),
    axis.text.y = element_text(
      size = 11, 
      hjust = 0,
      vjust = 1
    )
  )

scatterTicks
## Warning: Removed 96 rows containing missing values or values outside the scale range
## (`geom_point()`).

With the angle argument, we can rotate the axis labels. With hjust and vjust, we can change the starting position of the text.

Next, we want to change the plot grid, specifically the lines. To do this, we first use the panel.grid argument and the element_line() function within the argument.

scatterGrid <- scatterTicks +
  theme(
    panel.grid = element_line(
      color = "green",
      size = 1,
      linetype = "solid" # blank, solid, dashed, dotted, dotdash, longdash, twodash 
    )
  )
## Warning: The `size` argument of `element_line()` is deprecated as of ggplot2 3.4.0.
## ℹ Please use the `linewidth` argument instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
scatterGrid
## Warning: Removed 96 rows containing missing values or values outside the scale range
## (`geom_point()`).

The panel.grid.major and panel.grid.minor arguments can be used to edit the major and minor gridlines separately. For example, if we only want the major gridlines, we do the following:

scatterGrid <- scatterTicks +
  theme(
    panel.grid.major = element_line(
      color = "green",
      size = 1,
      linetype = "solid" # blank, solid, dashed, dotted, dotdash, longdash, twodash 
    ),
    panel.grid.minor = element_blank()
  )

scatterGrid
## Warning: Removed 96 rows containing missing values or values outside the scale range
## (`geom_point()`).

You can also edit the minor gridlines separately by adding .x or .y as needed.

Finally, you can also change the background of the plot or panel. This is done using the plot.background or panel.background arguments. You can use the element_rect() function within the argument.

scatterGrid +
  theme(
    plot.background = element_rect(
      color ="darkgray",
      size = 2,
      fill = "lightpink"
    ),
    panel.background = element_rect(
      fill = "moccasin"
    )
  )
## Warning: The `size` argument of `element_rect()` is deprecated as of ggplot2 3.4.0.
## ℹ Please use the `linewidth` argument instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Warning: Removed 96 rows containing missing values or values outside the scale range
## (`geom_point()`).

There are also a variety of pre-made themes that can be further customized.

An overview of available themes can be found here.