How to use scale_fill_manual function In R Language

scale_fill_manual() allows you to specify your own set of mappings from levels in the data to aesthetic values. 

Syntax:

scale_fill_manual(..., values)

Parameters:

…: Common discrete scale parameters: name, breaks, labels, na.value, limits, and guide.

Example:

We will be using 6 different data points for the bar plot and then with the help of using the scale_fill_manual function, we will be applying the given colors to the barplot in the R programming language.

Input:

R




# load the package
library("ggplot2")
  
# create a dataframe
# with letters and numbers
gfg < -data.frame(
    x=c('A', 'B', 'C', 'D', 'E', 'F'),
    y=c(4, 6, 2, 9, 7, 3))
  
# display bar
ggplot(gfg, aes(x, y, fill=x)) +
geom_bar(stat="identity") +
scale_fill_manual(values=c("A"="purple",
                           "B"="yellow",
                           "C"="red",
                           "D"="blue",
                           "E"="green",
                           "F"="black"))


Output:-



Coloring Barplots with ggplot2 in R

In this article, we will discuss how to color the barplot using the ggplot2 package in the R programming language.

Similar Reads

Method 1: Using fill argument within the aes function

Using the fill argument within the aes function to be equal to the grouping variable of the given data. Aesthetic mappings describe how variables in the data are mapped to visual properties (aesthetics) of geoms. Aesthetic mappings can be set in ggplot() and in individual layers...

Method 2: Using scale_fill_manual function

...