Open In App

Change Fill and Border Color of ggplot2 Plot in R

Last Updated : 17 Jun, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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 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

  • 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 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:

  • 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 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:

  • 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 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:

  • 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 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 :

  • 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 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 :

  • 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 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:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads