Change Fill and Border Color of ggplot2 Plot in R
Last Updated :
17 Jun, 2021
In this article, we are going to see various methods to add color into a ggplot2 in the R programming language. We will select a bar plot as an example to see how we can change the color of the fill of the bar as well as the borders.
First, you need to install the ggplot2 package if it is not previously installed in R Studio. For creating a simple bar plot we will use the function geom_bar( ).
Syntax:
geom_bar(stat, fill, color, width)
Parameters :
- stat : Set the stat parameter to identify the mode.
- fill : Represents color inside the bars.
- color : Represents color of outlines of the bars.
- width : Represents width of the bars.
Data in use:

Let us first draw a regular plot so that the difference can be apparent.
Example:
R
library (ggplot2)
ODI <- data.frame (match= c ( "M-1" , "M-2" , "M-3" , "M-4" ),
runs= c (67,37,74,10))
perf <- ggplot (data=ODI, aes (x=match, y=runs))+
geom_bar (stat= "identity" )
perf
|
Output:

Now let us discuss colors that can be added to this plot. Adding colors not only make the charts appealing but also gives makes them descriptive for better understanding.
Method 1: Using default colors
Use the command fill to add color inside the bars. Since the bars are in the different x-axis, values we need to assign the x-axis variable to the fill.
Syntax
fill=attribute
You need to write this command inside the aes() also known as the quoting function or aesthetic function under the ggplot2 library.
Example:
R
library (ggplot2)
ODI <- data.frame (match= c ( "M-1" , "M-2" , "M-3" , "M-4" ),
runs= c (67,37,74,10))
perf <- ggplot (data=ODI, aes (x=match, y=runs,fill=match))+
geom_bar (stat= "identity" )
perf
|
Output:

Use the same fill. But this time since we have to assign a single color we will use it inside the geom_bar( ). Assign the color of your choice to the fill as shown below. The theme is changed because some color plots might not be visible to the default theme in R.
Syntax:
theme_[theme_name]( )
Example:
R
library (ggplot2)
ODI <- data.frame (match= c ( "M-1" , "M-2" , "M-3" , "M-4" ),
runs= c (67,37,74,10))
perf <- ggplot (data=ODI, aes (x=match, y=runs))+
geom_bar (stat= "identity" ,fill= "lightblue" )+
theme_dark ()
perf
|
Output:

Here we need a command color for assigning colors to the outline. Outline colors look good when the fill color is white. It is the property of color because some colors overshadow other colors or if we assign the same color to fill and outline both. Since we need the same fill and the same outline we will write these commands inside the geom_bar( ).
Example:
R
library (ggplot2)
ODI <- data.frame (match= c ( "M-1" , "M-2" , "M-3" , "M-4" ),
runs= c (67,37,74,10))
perf <- ggplot (data=ODI, aes (x=match, y=runs))+
geom_bar (stat= "identity" ,color= "red" ,fill= "white" )+
theme_classic ()
perf
|
Output:

Here we need to provide different outlines. The fill will be the same which is white. The fill will be inside the geom_bar( ). The color will be inside aes( ) under ggplot( ) as it is variable in this case.
Example:
R
library (ggplot2)
ODI <- data.frame (match= c ( "M-1" , "M-2" , "M-3" , "M-4" ),
runs= c (67,37,74,10))
Assigning default and different outline colors to bar plot
perf <- ggplot (data=ODI, aes (x=match, y=runs,color=match))+
geom_bar (stat= "identity" ,fill= "white" )+
theme_classic ()
perf
|
Output:

Method 2: Setting colors manually
1) For Fill color
To manually change the color of a bar plot we will use the following function :
- scale_fill_manual( ) : It is used to provide custom colors. We can either write the color code as “#XXXXXX” or we can directly write the color name as “color_name”.
Syntax:
scale_fill_manual( values )
- scale_fill_brewer( ) : It uses a range of colors from set of palettes in RColorBrewer package.
Syntax:
scale_fill_brewer( palette)
- scale_fill_grey( ) : To fill the bars using gray scale colors.
Example:
R
library (ggplot2)
ODI <- data.frame (match= c ( "M-1" , "M-2" , "M-3" , "M-4" ),
runs= c (67,37,74,10))
perf <- ggplot (data=ODI, aes (x=match, y=runs,fill=match)) +
geom_bar (stat= "identity" )
perf+ scale_fill_manual (values= c ( "#9933FF" ,
"#33FFFF" ,
"red" ,
"darkblue" ))
perf+ scale_fill_brewer (palette= "" PrGN "" )
perf+ scale_fill_grey ()
|
Output:

2) For Borders or outlines
To manually change the color of borders of a bar plot we will use the following function :
- scale_color_manual( ): It is used to provide custom colors. We can either write the color code as “#XXXXXX” or we can directly write the color name as “color_name”.
Syntax:
scale_color_manual( values )
- scale_color_brewer( ): It uses a range of colors from set of palettes in RColorBrewer package.
Syntax:
scale_color_brewer( palette)
- scale_color_grey( ): Gives outlines from the gray scale colors.
Since, we need to fill the borders, better to make all the bars fill color as white for better blending of colors and also add a suitable theme of your choice.
Example:
R
library (ggplot2)
ODI <- data.frame (match= c ( "M-1" , "M-2" , "M-3" , "M-4" ),
runs= c (67,37,74,10))
perf <- ggplot (data=ODI, aes (x=match, y=runs,color=match))+
geom_bar (stat= "identity" ,fill= "white" )
perf+ scale_color_manual (values= c ( "#9933FF" ,
"#33FFFF" ,
"red" ,
"darkblue" ))+ theme_classic ()
perf+ scale_color_brewer (palette= "Dark2" )+ theme_classic ()
perf+ scale_color_grey ()+ theme_classic ()
|
Output:

Similar Reads
Change Color of ggplot2 Boxplot in R
In this article, we are going to see how to change the color of boxplots using ggplot2 in R Programming Language. We have considered the built-in data frame "ChickWeight". It contains information about the feed type and growth rate of chickens for six different types of foods like casein, soybean, e
3 min read
Change the Fill Color of One of the Dodged Bars in ggplot2
ggplot2 is an exceptionally versatile R package used for creating a wide range of data visualizations. One common visualization type is the grouped bar plot, which displays bars side-by-side to compare different groups. Sometimes, you might want to highlight or distinguish one of these bars by chang
4 min read
Change Theme Color in ggplot2 Plot in R
A theme in ggplot2 is a collection of settings that control the non-data elements of the plot. These settings include things like background colors, grid lines, axis labels, and text sizes. we can use various theme-related functions to customize the appearance of your plots, including changing theme
4 min read
How To Change facet_wrap() Box Color in ggplot2 in R?
In this article, we will discuss how to change facet_wrap() box color in ggplot2 in R Programming language. Facet plots, where one subsets the data based on a categorical variable and makes a series of similar plots with the same scale. Facetting helps us to show the relationship between more than t
3 min read
Change Color of Bars in Barchart using ggplot2 in R
In this article, we are going to see various methods to change the color of a bar chart using ggplot2 in the R programming language. For creating a simple bar plot we will use the function geom_bar( ). Syntax: geom_bar(stat, fill, color, width) Parameters : stat : Set the stat parameter to identify
4 min read
Combine bar and line chart in ggplot2 in R
Sometimes while dealing with hierarchical data we need to combine two or more various chart types into a single chart for better visualization and analysis. These are known as âCombination chartsâ. In this article, we are going to see how to combine a bar chart and a line chart in R Programming Lang
4 min read
Add Panel Border to ggplot2 Plot in R
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. Approach:Specify the data object, and it has to be a dataframe. Here it has two variable n
3 min read
How to change Colors in ggplot2 Line Plot in R ?
A line graph is a chart that is used to display information in the form of series of data points. It utilizes points and lines to represent change over time. Line graphs are drawn by plotting different points on their X coordinates and Y coordinates, then by joining them together through a line from
2 min read
Add Confidence Band to ggplot2 Plot in R
In this article, we will discuss how to add Add Confidence Band to ggplot2 Plot in the R programming Language. A confidence band is the lines on a scatter plot or fitted line plot that depict the upper and lower confidence bounds for all points on the range of data. This helps us visualize the error
3 min read
Change Labels of GGPLOT2 Facet Plot in R
In this article, we will see How To Change Labels of ggplot2 Facet Plot in R Programming language. To create a ggplot2 plot, we have to load ggplot2 package. library() function is used for that. Then either create or load dataframe. Create a regular plot with facets. The labels are added by default.
3 min read