Add Panel Border to ggplot2 Plot in R - GeeksforGeeks

28 Apr, 2021
In this article, we will use theme() function to add panel border to the plot in R Programming Language. Here we will create a scatter plot, but you can apply it to any plot and add a Panel Border to that one.
Specify the data object, and it has to be a dataframe. Here it has two variable named year and point.
Call ggplot() function. Put first parameter ‘data’ and then set the aesthetic function ‘aes()’.
Inside aes() function, set the year variable for the X Axis, use the point variable for the Y Axis.
Call ggtitle() to set the title of the plot.
Call geom_point() function. Here inside geom_point() function, we use aes() with size parameter for setting the size of points and also use col parameter to set the color of points.
Add Panel Border to plot using theme() function and panel.border as its parameter. To add Panel Border to R plot we simply add theme() function to ggplot() function. Inside theme() function, we have to specify panel.border parameter to add panel border around plot and use element_rect() function as value of panel.border parameter.
Syntax: theme(panel.border) 
Parameter: theme() has many parameters to specify the theme of plot. we can use them as per our requirements but for add panel border to plot, we will use only panel.border parameter and specify element_rect() function as it’s value.
Return: Theme of the plot. 
element_rect() is used for specifying borders and backgrounds.
Syntax:
element_rect(color = “color_name”, fill = NULL, size = NULL, linetype = NULL) 
Parameter: 
fill : Specifies the color by which we fill whole rectangle.
color : For specifying border color.
size : For specifying border size.
linetype : For specifying border line type
Return: Border around plot.
Dataset in use:
Let just first create a regular scatter plot to understand the difference better.
Example:
R
# load ggplot2 packagelibrary(ggplot2)  # Create a dataframe for Plot datadata <- data.frame(year = c(2011, 2012, 2013, 2014, 2015),                   point = c(10, 20, 30, 40, 50))  # Plot the scatter plotggplot(data, aes(year, point)) +      geom_point()+  ggtitle("Scatter Plot")
Output:
Simple Scatter plot using ggplot2
Now let’s add a border to it and display the result.
Example:
R
# load ggplot2 packagelibrary(ggplot2)  # Create a dataframe for Plot datadata <- data.frame(year = c(2011, 2012, 2013, 2014, 2015),                   point = c(10, 20, 30, 40, 50))  # Plot the scatter plot with panel border# of size 10 and green colorggplot(data, aes(year, point)) +      geom_point()+  ggtitle("Scatter Plot with Panel Border")+  theme(panel.border = element_rect(color = "green",                                    fill = NA,                                    size = 10))
Output: 
Scatter Plot with Panel Border
Picked
R-ggplot
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Change Color of Bars in Barchart using ggplot2 in R
How to Change Axis Scales in R Plots?
Group by function in R using Dplyr
How to Split Column Into Multiple Columns in R DataFrame?
How to filter R DataFrame by values in a column?
How to import an Excel File into R ?
How to filter R dataframe by multiple conditions?
Replace Specific Characters in String in R
Time Series Analysis in R
R - if statement