- 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
How to create bar plot with gradient colors using ggplot2 in R?n
To create bar plot with gradient colors using ggplot2, we can use scale_fill_gradient function where we can set the lower and higher color values.
For Example, if we have a data frame called df that contains two columns say Cat and Count then we can create the bar plot with gradient colors by using the below command −
ggplot(df,aes(Cat,Count,fill=Cat))+geom_bar(stat="identity")+scale_fill_gradient(low="blue",high="red")
Example
Following snippet creates a sample data frame −
x<-LETTERS[1:5] y<-c(27,25,24,21,20) df<-data.frame(x,y) df
The following dataframe is created
x y 1 A 27 2 B 25 3 C 24 4 D 21 5 E 20
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<-LETTERS[1:5] y<-c(27,25,24,21,20) df<-data.frame(x,y) library(ggplot2) ggplot(df,aes(x,y))+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 having bars filled with gradient color on the above created data frame, add the following code to the above snippet −
x<-LETTERS[1:5] y<-c(27,25,24,21,20) df<-data.frame(x,y) library(ggplot2) ggplot(df,aes(x,y,fill=y))+geom_bar(stat="identity")+scale_fill_gradient(low="blue",high="red")
Output
If you execute all the above given snippets as a single program, it generates the following Output −
- Related Articles
- How to create a bar plot with ggplot2 using stat_summary in R?
- How to create bar plot with log values using ggplot2 in R?
- How to create transparent bar plot using ggplot2 in R?
- How to create a bar plot using ggplot2 with one bar having black border in R?
- How to create the stacked bar plot using ggplot2 in R with labels on the plot?
- How to create a stacked bar plot with vertical bars in R using ggplot2?
- How to create a bar plot using ggplot2 with percentage on Y-axis in R?
- How to create bar plot using ggplot2 with structure data frame?
- How to fill bars of a bar plot created using ggplot2 with colors based on frequency?
- How to create bar plot of means with error bars of standard deviations using ggplot2 in R?
- How to create stacked plot with density using ggplot2 in R?
- How to create a plot using rgb colors in R?
- How to create bar chart using ggplot2 with chart sub-title in R?
- How to create the bar chart with ggplot2 using color brewer in R?
- Create bar plot of one column in an R data frame using ggplot2.
