How to Add Panel Border to ggplot2


Often you may want to add a panel border around a plot created in ggplot2.

The easiest way to do this is by using the theme() function with the following basic syntax:

library(ggplot2)

#create scatterplot of points vs assists
ggplot(df, aes(x=points, y=assists)) +
  geom_point() +
  theme(panel.border = element_rect(color = 'black', 
                                    fill = NA, 
                                    size = 3))

This particular example creates a scatterplot of the variables named points vs. assists in the data frame and then uses the theme() function with the panel.border argument with the following values:

  • color: The outline color to use for the panel
  • fill: The fill color to use for the panel
  • size: The width of the panel (larger values = thicker panel border)

The following examples shows how to add a panel border to a plot created in ggplot2 in practice.

Note: You may need to first install the ggplot2 package before being able to use the various functions from the package. You can use the following syntax to do so:

install.package('ggplot2')

Once ggplot2 is successfully installed, you can proceed to use the various plotting functions from the package.

Example: How to Add a Panel Border to ggplot2

First, we will create the following data frame in R that contains information about various basketball players:

#create data frame
df <- data.frame(team=c('A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'),
                 points=c(99, 68, 86, 88, 95, 74, 78, 93),
                 assists=c(22, 28, 45, 35, 34, 45, 28, 31),
                 rebounds=c(30, 28, 24, 24, 30, 36, 30, 29))

#view data frame
df

  team points assists rebounds
1    A     99      22       30
2    A     68      28       28
3    A     86      45       24
4    A     88      35       24
5    B     95      34       30
6    B     74      45       36
7    B     78      28       30
8    B     93      31       29

Note that this data frame contains the following variables:

  • team: The team name the player belongs on
  • points: Total points scored by the player
  • assists: Total assists by the player
  • rebounds: Total rebounds by the player

Suppose that we would like to create a scatterplot to visualize the relationship between the points and assists variables.

We can use the following syntax to do so:

library(ggplot2)

#create scatterplot of points vs assists
ggplot(df, aes(x=points, y=assists)) +
  geom_point()

This produces the following scatterplot:

The x-axis displays the values from the points column, the y-axis displays the values from the assists column and the points in the plot represent each pairwise combination of points and assists for each player.

Notice that the plot has no panel border by default.

In order to add a panel border around the plot, we can use the following syntax:

library(ggplot2)

#create scatterplot of points vs assists
ggplot(df, aes(x=points, y=assists)) +
  geom_point() +
  theme(panel.border = element_rect(color = 'black', 
                                    fill = NA, 
                                    size = 3))

This produces the following scatterplot:

Notice that a black panel border has been added around the plot.

For this particular example we specified a size value of 3 to make the border width slightly larger and more visible.

Feel free to modify the values for the color, fill and size arguments within the panel.border function to customize the appearance of the border.

Note: You can find the complete documentation for the theme() function in ggplot2 here

Additional Resources

The following tutorials explain how to perform other common operations in ggplot2:

How to Change Font Size in ggplot2
How to Rotate Axis Labels in ggplot2
How to Remove a Legend in ggplot2
How to Remove Axis Labels in ggplot2

Leave a Reply

Your email address will not be published. Required fields are marked *