How to use Rcolorbrewer palette for plotting graphs using ggplot2 in R

This recipe helps you use Rcolorbrewer palette for plotting graphs using ggplot2 in R

Recipe Objective

How to use Rcolorbrewer palette for plotting graphs using ggplot2?

The r color palette is used for changing the default color of graphs plotted using different libraries. ggplot uses the following syntax for changing colors of various plots: -scale_fill_brewer (palette="") where, palette = different color schemes. This recipe demonstrates an example of how to use Rcolorbrewer palette.

Step 1 - Install the necessary package and library

install.packages("ggplot2") install.packages("RColorBrewer") library(ggplot2) library(RColorBrewer) display.brewer.all() # all the color palettes

Step 2 - Define a dataframe

Syntax - ggplot(data) + geom_bar(aes(x,y)) + geom_errorbar(aes(x,y,ymin,ymax) data - input data geom_bar - bar plot aes (x,y) - the aes function - creates mapping from data to geom

data <- data.frame(values = c(5,10,15,20,25,30), type = c("A","B","C","D","E","F"), sd_dev = c(1.5,2.5,3.5,4.5,5.5,6.5)) data

Step 3 - Plot a bar chart

ggplot(data)+ geom_bar(aes(x=type,y=values),stat="identity") # defualt color for the bar chart

Step 4 - Use scale_fill_brewer() for changing the colors of bars

ggplot(data)+ geom_bar(aes(x = type,y = values,fill = type),stat="identity") + scale_fill_brewer(palette = "Blues") # color type : Sequential – Gives Light colours for "low data" and dark for "high data" ggplot(data)+ geom_bar(aes(x = type,y = values,fill = type),stat="identity") + scale_fill_brewer(palette = "Set3") # color type : Diverging – Gives Light colours for "mid-range data" and low and high for "contrasting dark colours" ggplot(data)+ geom_bar(aes(x = type,y = values,fill = type),stat="identity") + scale_fill_brewer(palette = "Spectral") # color type : Qualitative – Gives Colours designed to give maximum visual difference between classes {"mode":"full","isActive":false}

What Users are saying..

profile image

Anand Kumpatla

Sr Data Scientist @ Doubleslash Software Solutions Pvt Ltd
linkedin profile url

ProjectPro is a unique platform and helps many people in the industry to solve real-life problems with a step-by-step walkthrough of projects. A platform with some fantastic resources to gain... Read More

Relevant Projects

Ensemble Machine Learning Project - All State Insurance Claims Severity Prediction
In this ensemble machine learning project, we will predict what kind of claims an insurance company will get. This is implemented in python using ensemble machine learning algorithms.

Build a Graph Based Recommendation System in Python -Part 1
Python Recommender Systems Project - Learn to build a graph based recommendation system in eCommerce to recommend products.

Build an Image Segmentation Model using Amazon SageMaker
In this Machine Learning Project, you will learn to implement the UNet Architecture and build an Image Segmentation Model using Amazon SageMaker

Insurance Pricing Forecast Using XGBoost Regressor
In this project, we are going to talk about insurance forecast by using linear and xgboost regression techniques.

NLP Project for Beginners on Text Processing and Classification
This Project Explains the Basic Text Preprocessing and How to Build a Classification Model in Python

Build a Collaborative Filtering Recommender System in Python
Use the Amazon Reviews/Ratings dataset of 2 Million records to build a recommender system using memory-based collaborative filtering in Python.

Loan Eligibility Prediction Project using Machine learning on GCP
Loan Eligibility Prediction Project - Use SQL and Python to build a predictive model on GCP to determine whether an application requesting loan is eligible or not.

Natural language processing Chatbot application using NLTK for text classification
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.

Hands-On Approach to Master PyTorch Tensors with Examples
In this deep learning project, you will learn how to perform various operations on the building block of PyTorch : Tensors.

Many-to-One LSTM for Sentiment Analysis and Text Generation
In this LSTM Project , you will build develop a sentiment detection model using many-to-one LSTMs for accurate prediction of sentiment labels in airline text reviews. Additionally, we will also train many-to-one LSTMs on 'Alice's Adventures in Wonderland' to generate contextually relevant text.