- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Why scale_fill_manual does not fill the bar with colors created by using ggplot2 in R?
We can manually fill the color of bars in a bar plot which is created by using ggplot2 but for this we would be needing color aesthetics already used for the bars. That means we need to use filling of bars inside aes of ggplot2 and then the colors can be mentioned inside the scale_fill_manual function.
Example
Consider the below data frame:
> x<-c("A","B","C","D") > count<-c(321,324,320,328) > df<-data.frame(x,count) > df
Output
x count 1 A 321 2 B 324 3 C 320 4 D 328
Loading ggplot2 package and creating bar plot:
Example
> library(ggplot2) > ggplot(df,aes(x,count))+geom_bar(stat="identity")
Output:
Creating the bar plot by using scale_fill_manual:
Example
> ggplot(df,aes(x,count))+geom_bar(stat="identity")+scale_fill_manual(values=c("blue","green","grey","yellow"))
Output
This does not change the color of bars.
Creating the bar plot by using scale_fill_manual but passing the fill of bars in aes:
Example
> ggplot(df,aes(x,count,fill=x))+geom_bar(stat="identity")+scale_fill_manual(values=c("blue","green","grey","yellow"))
Output:
- Related Articles
- How to fill bars of a bar plot created using ggplot2 with colors based on frequency?
- How to fill histogram bars using ggplot2 in R with different colors?
- How to create bar plot with gradient colors using ggplot2 in R?\n
- How to align the text horizontally in a bar plot created by using ggplot2 in R?
- How to change legend values in a bar plot created by using ggplot2 in R?
- How to change the thickness of the borders of bars in bar plot created by using ggplot2 in R?
- How to change the bars color to grey shade of a bar graph created by using ggplot2 in R?
- How to change the size of dots in dotplot created by using ggplot2 in R?
- How to add a citation in a plot created by using ggplot2 in R?
- How to reverse the X-axis labels of scatterplot created by using ggplot2 in R?
- How to change the Y axis limit for boxplot created by using ggplot2 in R?
- How to create the bar chart with ggplot2 using color brewer in R?
- How to change the abline colour created using ggplot2 in R?
- How to change the angle of annotated text in plot created by using ggplot2 in R?
- How to create bar plot with log values using ggplot2 in R?

Advertisements