Coloring Barplots with ggplot2 in R
Last Updated :
24 Oct, 2021
In this article, we will discuss how to color the barplot using the ggplot2 package in the R programming language.
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
Syntax:
aes(x, y, ...)
Parameters:
x, y, ... : List of name-value pairs in the form aesthetic = variable describing which variables in the layer data should be mapped to which aesthetics used by the paired geom/stat.
Example:
We will be using 6 different data points for the bar plot and then with the help of the fill argument within the aes function, we will be applying the default colors to the barplot in the R programming language.
R
# load the library
library("ggplot2")
# create the 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 the bar
ggplot(gfg, aes(x, y, fill=x)) + geom_bar(stat="identity")
Output:-

Method 2: Using scale_fill_manual function
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:-
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,
3 min read
How To Make Barplots with Error bars in ggplot2 in R? In this article, we will discuss how to make a barplot with an error bar using ggplot2 in the R programming language. Error Bars helps us to visualize the distribution of the data. Error Bars can be applied to any type of plot, to provide an additional layer of detail on the presented data. Often t
4 min read
Themes and background colors in ggplot2 in R In this article, we will discuss how to change the look of a plot theme (background color, panel background color, and gridlines) using the R Programming Language and ggplot2 package. Themes in ggplot2 package The ggplot2 package in R Language has 8 built-in themes. To use these themes we just need
3 min read
Coloring Points Based on Variable with R ggpairs This article will explain how to color points based on a variable using ggpairs() By adding color to the points in a pairwise plot based on a categorical or continuous variable, we can easily see how different categories or ranges of values behave across multiple pairwise relationships using R Progr
3 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