- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to change the color of a particular bar using geom_bar in R?
To change the color of a particular bar using geom_bar in R, we can provide the count corresponding to the value for which we want to change the color inside aes function.
For Example, if we have a data frame called df that contains two columns say V and F where V is categorical and F is for frequency and we want to change the color of frequency 10 in bar plot then we can use the below mentioned command −
ggplot(df,aes(V,F))+geom_bar(aes(fill=..F..==10),stat="identity")
Example
Following snippet creates a sample data frame −
x<-c("Low","Medium","High") Count<-c(20,28,40) df<-data.frame(x,Count) df
The following dataframe is created
x Count 1 Low 20 2 Medium 28 3 High 40
To load ggplot2 package and create bar plot for data in df on the above created data frame, add the following code to the above snippet −
x<-c("Low","Medium","High") Count<-c(20,28,40) df<-data.frame(x,Count) library(ggplot2) ggplot(df,aes(x,Count))+geom_bar(stat="identity")
Output
If you execute all the above given snippets as a single program, it generates the following Output −
To create bar plot for data in df with bar in different color having Count = 20 on the above created data frame, add the following code to the above snippet −
x<-c("Low","Medium","High") Count<-c(20,28,40) df<-data.frame(x,Count) library(ggplot2) ggplot(df,aes(x,Count))+geom_bar(aes(fill=..Count..==20),stat="identity")
Output
If you execute all the above given snippets as a single program, it generates the following Output −
- Related Articles
- How to change the color of bars of a bar plot using ggplot2 in R?
- How to manually set the color of each bar in base R bar plot?
- How to change the bars color to grey shade of a bar graph created by using ggplot2 in R?
- How to change status bar color to match app Android using Kotlin?
- How to create the bar chart with ggplot2 using color brewer in R?
- How to change status bar color to match app Android?
- How to change the Y-axis values in a bar plot using ggplot2 in R?
- How to change the order of bars in bar chart in R?
- How to change the automatic sorting of X-axis of a bar plot using ggplot2 in R?
- How to change the color of a single bar if a condition is true (Matplotlib)?
- How to change legend values in a bar plot created by using ggplot2 in R?
- How can I change the text color of the Navigation Bar on iOS?
- How to create a bar plot using ggplot2 with one bar having black border in R?
- How to create a bar chart using plotly in R?
- How to change the thickness of the borders of bars in bar plot created by using ggplot2 in R?
