Categories: Data Science

Learn R – How to Create Histogram using GGPlot

This article represents techniques (commands samples) which could be used to create histogram using ggplot2 package in R programming. Please feel free to comment/suggest if I missed to mention one or more important points. Also, sorry for the typos.

Following is the summary of commands used to create histogram using ggplot:

  • Using simplistic ggplot and geom_histogram method
  • Using ggplot and geon_histogram command with attributes such as col, fill, alpha
  • Using ggplot, geom_histogram and  scale_fill_gradient method
Common Techniques to Create Histogram using ggplot2

In the code examples below, diamonds dataset from ggplot2 package is used. To work with examples below, load the ggplot2 library prior to executing the commands given below.

Following are some of the common attributes that could be used with geom_histogram method while creating a histogram using ggplot2 library:

  • col: Assigns border color for the bin
  • fill: Color used to fill the bins
  • alpha: Controls the fill transparency

Following is the most simplistic command which could be used to create a Histogram.

# Pay attention to usage of ggplot method with additional layer of geom_histogram() method
ggplot(diamonds, aes(x=price)) + geom_histogram() 

Following could be used to create Histogram with labels and additional attributes passed to geom_histogram method such as col, fill, alpha etc.

# Pay attention to usage of attributes such as col, fill, alpha. Also, note labs method 
# for assigning labels and title
ggplot(diamonds, aes(x=price)) + geom_histogram(col="red", fill="green", alpha=0.4) +
  labs (x="Price", y="Count", title="Histogram for Price")

Following commands could be used to assign varying color shades to bins (blue by default).

# Paint different bins with different shares of blue (default color) : Parameter aes(fill=..count..)
ggplot(diamonds, aes(x=price)) + geom_histogram(col="red", aes(fill=..count..)) +
  labs (x="Price", y="Count", title="Histogram for Price")

Following commands could be used assign different color shades varying from low to highest bin heights

# Paint different bins with different shades of custom colors using scale_fill_gradient method
ggplot(diamonds, aes(x=price)) + geom_histogram(col="red", aes(fill=..count..) ) +
  labs (x="Price", y="Count", title="Histogram for Price") +
  scale_fill_gradient("Count", low="yellow", high="red")     

 

Ajitesh Kumar

I have been recently working in the area of Data analytics including Data Science and Machine Learning / Deep Learning. I am also passionate about different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia, etc, and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data, etc. I would love to connect with you on Linkedin. Check out my latest book titled as First Principles Thinking: Building winning products using first principles thinking.

Recent Posts

Retrieval Augmented Generation (RAG) & LLM: Examples

Last updated: 25th Jan, 2025 Have you ever wondered how to seamlessly integrate the vast…

3 weeks ago

How to Setup MEAN App with LangChain.js

Hey there! As I venture into building agentic MEAN apps with LangChain.js, I wanted to…

4 weeks ago

Build AI Chatbots for SAAS Using LLMs, RAG, Multi-Agent Frameworks

Software-as-a-Service (SaaS) providers have long relied on traditional chatbot solutions like AWS Lex and Google…

4 weeks ago

Creating a RAG Application Using LangGraph: Example Code

Retrieval-Augmented Generation (RAG) is an innovative generative AI method that combines retrieval-based search with large…

1 month ago

Building a RAG Application with LangChain: Example Code

The combination of Retrieval-Augmented Generation (RAG) and powerful language models enables the development of sophisticated…

1 month ago

Building an OpenAI Chatbot with LangChain

Have you ever wondered how to use OpenAI APIs to create custom chatbots? With advancements…

1 month ago