Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Data Visualization
  • Statistics in R
  • Machine Learning in R
  • Data Science in R
  • Packages in R
  • Data Types
  • String
  • Array
  • Vector
  • Lists
  • Matrices
  • Oops in R
Open In App
Next Article:
How To Make Scree Plot in R with ggplot2
Next article icon

How to Make ECDF Plot with ggplot2 in R?

Last Updated : 18 Apr, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Empirical Cumulative Distribution Function Plot (ECDF) helps us to visualize one or more distributions. ECDF plot is a great alternative for histograms and it has the ability to show the full range of data without the need for various parameters.

In this article, we will discuss how to draw an ECDF plot using the ggplot2 package of R Programming language. To draw an ECDF plot, we use the stat_ecdf() function of the ggplot2 package of R Language.

Syntax: ggplot( df, aes(x)) + stat_ecdf( geom, col )

Parameters: 

  • df : determines dataframe used to plot ECDF plot
  • geom: determines the shape of plot, i.e., point, step,etc.
  • col: determines the color of plot

Create a basic ECDF plot

In this example, we will create a basic ECDF plot. We will use the ggplot() function and the stat_ecdf() function to plot the ECDF plot.

R
# set seed set.seed(1234)  # create a random data frame  sample_data <- data.frame(x = round(rnorm(700,                                            mean=800,                                            sd=450)))  # load library ggplot2 library(ggplot2)  # Basic ECDF plot using ggplot package ggplot(sample_data, aes(x)) +   # stat_ecdf() function is used to plot ECDF plot stat_ecdf() 

Output:

Color Customization

To change the color of the ECDF plot we use the col parameter of the stat_ecdf() function. We can add any color as the value of parameter col. We can even use hex codes of color. In this example, we have a green-colored ECDF plot made using stat_ecdf() function with the col parameter being green.

R
# set seed set.seed(1234)  # create a random data frame  sample_data <- data.frame(x = round(rnorm(700, mean=800,                                            sd=450)))  # load library ggplot2 library(ggplot2)  # Basic ECDF plot using ggplot package ggplot(sample_data, aes(x)) +   # stat_ecdf() function is used to plot ECDF plot # col parameter is used to color plot as green stat_ecdf(col="green") 

Output:

Shape Customization

To change the shape of the ECDF plot we use the geom parameter of the stat_ecdf() function. We can add any shape as the value of parameter geom. In this example, we have a stair-shaped ECDF plot made using stat_ecdf() function with the geom parameter being "step".

R
# set seed set.seed(1234)  # create a random data frame  sample_data <- data.frame(x = round(rnorm(20, mean=800, sd=450)))  # load library ggplot2 library(ggplot2)  # Basic ECDF plot using ggplot package ggplot(sample_data, aes(x)) +   # stat_ecdf() function is used to plot ECDF plot # geom parameter is used to shape plot as step stat_ecdf(geom="step") 

Output:

Multiple ECDF colored by group:

ECDF plot can be used for plotting multiple distributions. To plot multiple ECDF plots, we firstly create a multi-dimension dataset and then use the col parameter of the aes() function to color them according to the group. 

Here, is a multiple distribution ECDF plots plotted with color by group using col parameter of the aes() function of the ggplot2 package. To create a multi-dimension dataset we will use rnorm() functions with gl() function to group them in 5 columns of size 1000.

R
# set seed set.seed(1234)  # create a random data frame  sample_data <- data.frame(x = c(rnorm(1000, 10, 5),                          rnorm(1000, 20, 10),                         rnorm(1000, 30, 20),                         rnorm(1000, 40, 30),                         rnorm(1000, 50, 30)),                           group = gl(5, 1000))  # load library ggplot2 library(ggplot2)  # Basic ECDF plot using ggplot package # col parameter is used to color plot  # according to group ggplot(sample_data, aes(x=x, col=group)) +   # stat_ecdf() function is used to plot ECDF plot stat_ecdf() 

Output:


Next Article
How To Make Scree Plot in R with ggplot2

M

mishrapriyank17
Improve
Article Tags :
  • R Language
  • R-ggplot

Similar Reads

  • How To Make Density Plots with ggplot2 in R?
    Density plots are a data visualization method used to estimate the probability density function (PDF) of a continuous variable. They provide smooth, continuous data distribution which makes them more informative than histograms in certain situations. The plot is produced by applying a kernel functio
    2 min read
  • How To Make Dumbbell Plot in R with ggplot2?
    The Dumbbell Plot shows the change between two points in our dataset. It is named so because of its Dumbbell shape. It helps us to understand the span of data categorically. To make Dumbbell Plot in R using ggplot2,  we use the geom_dumbbell() function. Syntax: geom_dumbbell(data, aes(y, x, xend), s
    2 min read
  • How To Make Scree Plot in R with ggplot2
    In this article, we are going to see how can we plot a Scree plot in R Programming Language with ggplot2.  Loading dataset: Here we will load the dataset, (Remember to drop the non-numerical column). Since the iris flower dataset contains a species column that is of character type so we need to drop
    2 min read
  • How To Make Violin Plots with ggplot2 in R?
    Violin plots help us to visualize numerical variables from one or more categories. They are similar to box plots in the way they show a numerical distribution using five summary-level statistics. But violin plots also have the density information of the numerical variables. It allows visualizing the
    5 min read
  • How To Make Lollipop Plot in R with ggplot2?
    A lollipop plot is the combination of a line and a dot. It shows the relationship between a numeric and a categorical variable just like a barplot. A lollipop plot can be used at places where a barplot and scatter plot both are required. This single plot helps us visualize the problem better and tak
    3 min read
  • How To Make World Map with ggplot2 in R?
    In this article, we will discuss how to create a world map and plot data on it using the R Programming Language. To create a world map using it we will use the geom_map() function of the ggplot2 package of the R Language. This function returns a ggplot object so all the functions that work on other
    4 min read
  • How to Make ECDF Plot with Seaborn in Python?
    Prerequisites:  Seaborn In this article, we are going to make the ECDF plot with Seaborn Library. ECDF PlotECDF stands for Empirical Commutative Distribution. It is more likely to use instead of the histogram for visualizing the data because the ECDF plot visualizes each and every data point of the
    5 min read
  • How to fix aspect ratio in ggplot2 Plot in R ?
    In this article, we will be looking at the approach to fix the aspect ratio in the ggplot2 plot using functions in the R programming language. The aspect ratio of a data graph is defined as the height-to-width ratio of the graph's size. It can be fixed automatically using the coord_fixed() function
    2 min read
  • How To Join Multiple ggplot2 Plots with cowplot?
    In this article, we are going to see how to join multiple ggplot2 plots with cowplot. To join multiple ggplot2 plots, we use the plot_grid() function of the cowplot package of R Language. Syntax: plot_grid(plot1,plot2,label=<label-vector>, ncol, nrow) Parameters: plot1 and plot2 are plots that
    3 min read
  • How to add image to ggplot2 in R ?
    In this article, we will discuss how to insert or add an image into a plot using ggplot2 in R Programming Language. The ggplot() method of this package is used to initialize a ggplot object. It can be used to declare the input data frame for a graphic and can also be used to specify the set of plot
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences