Change Fill and Border Color of ggplot2 Plot in R - GeeksforGeeks

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)  # Inserting dataODI <- data.frame(match=c("M-1","M-2","M-3","M-4"),                  runs=c(67,37,74,10))  # Basic vertical barplotperf <-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.
Different fill color
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)  # Inserting dataODI <- data.frame(match=c("M-1","M-2","M-3","M-4"),                  runs=c(67,37,74,10))  # Assigning default and different colors to bar plotperf <-ggplot(data=ODI, aes(x=match, y=runs,fill=match))+ geom_bar(stat="identity")perf
Output:
Same fill color
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)  # Inserting dataODI <- data.frame(match=c("M-1","M-2","M-3","M-4"),                  runs=c(67,37,74,10))  # Assigning default and same colors to bar plotperf <-ggplot(data=ODI, aes(x=match, y=runs))+ geom_bar(stat="identity",fill="lightblue")+ theme_dark()perf
Output:
Same outline
 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)  # Inserting dataODI <- data.frame(match=c("M-1","M-2","M-3","M-4"),                  runs=c(67,37,74,10))  # Assigning default and same outline # colors to bar plotperf <-ggplot(data=ODI, aes(x=match, y=runs))+ geom_bar(stat="identity",color="red",fill="white")+ theme_classic()perf
Output:
Different Outline
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)  # Inserting dataODI <- 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 plotperf <-ggplot(data=ODI, aes(x=match, y=runs,color=match))+ geom_bar(stat="identity",fill="white")+ theme_classic()perf
Output:
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)  # Inserting dataODI <- 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")  # Assigning fill colors manually # Assigning custom colorperf+scale_fill_manual(values=c("#9933FF",                             "#33FFFF",                             "red",                             "darkblue"))  # Assigning brewer palette perf+scale_fill_brewer(palette=""PrGN"")  # Assigning gray scaleperf+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)  # Inserting dataODI <- 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")  # Assigning border colors manually # Assigning custom colorsperf+scale_color_manual(values=c("#9933FF",                             "#33FFFF",                             "red",                             "darkblue"))+theme_classic()  # Assigning brewer palette perf+scale_color_brewer(palette="Dark2")+theme_classic()  # Assigning gray scaleperf+scale_color_grey()+theme_classic()
Output:
Picked
R-ggplot
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Filter data by multiple conditions in R using Dplyr
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?
K-Means Clustering in R Programming
How to filter R DataFrame by values in a column?
Replace Specific Characters in String in R
How to filter R dataframe by multiple conditions?
R - if statement