Open In App

Change Fill and Border Color of ggplot2 Plot in R

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:




library(ggplot2)
  
# Inserting data
ODI <- data.frame(match=c("M-1","M-2","M-3","M-4"),
                  runs=c(67,37,74,10))
  
# Basic vertical barplot
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:




library(ggplot2)
  
# Inserting data
ODI <- 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 plot
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:




library(ggplot2)
  
# Inserting data
ODI <- 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 plot
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:




library(ggplot2)
  
# Inserting data
ODI <- 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 plot
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:




library(ggplot2)
  
# Inserting data
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 :

Syntax:

scale_fill_manual( values ) 

Syntax:

scale_fill_brewer( palette)

Example:




library(ggplot2)
  
# Inserting data
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")
  
# Assigning fill colors manually 
# Assigning custom color
perf+scale_fill_manual(values=c("#9933FF",
                             "#33FFFF",
                             "red",
                             "darkblue"))
  
# Assigning brewer palette 
perf+scale_fill_brewer(palette=""PrGN"")
  
# Assigning gray scale
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 :

Syntax:

scale_color_manual( values ) 

Syntax:

scale_color_brewer( palette)

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:




library(ggplot2)
  
# Inserting data
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")
  
# Assigning border colors manually 
# Assigning custom colors
perf+scale_color_manual(values=c("#9933FF",
                             "#33FFFF",
                             "red",
                             "darkblue"))+theme_classic()
  
# Assigning brewer palette 
perf+scale_color_brewer(palette="Dark2")+theme_classic()
  
# Assigning gray scale
perf+scale_color_grey()+theme_classic()

Output:


Article Tags :