
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Fill Histogram Bars Using ggplot2 in R with Different Colors
When we create histogram using ggplot2 we need to pass the number of bins we want to have in the histogram and on the basis of these bin numbers the histogram will be created, these bin numbers are actually the number of bars we will have in the histogram. To fill those bars with different colors, we need to use fill argument and pass a range of values equal to the number of bins as shown in the below example.
Consider the below data frame −
x<-rnorm(1000) df<-data.frame(x)
Loading ggplot2 package and creating histogram of x −
Example
library(ggplot2) ggplot(df,aes(x))+geom_histogram(bins=10)
Output
Creating histogram of x with different color in all bars −
Example
ggplot(df,aes(x))+geom_histogram(bins=10,fill=1:10)
Output
Advertisements