EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials R Programming Tutorial Plot Function in R
Secondary Sidebar
R programming Tutorial
  • Chart/graphs
    • Graphs in R
    • Bar Charts in R
    • Pie Chart in R
    • Histogram in R
    • Line Graph in R
    • Plot Function in R
    • Scatterplot in R
    • R Boxplot labels
  • Basic
    • What is R Programming Language
    • Careers in R Programming
    • Install R
    • List of R Packages
    • Introduction of R Tools Technology
    • R Programming Language
    • DataSet in R
    • What is RStudio?
    • R-studio-Functions
    • R Packages
    • Time series?in R
    • R Data Types
    • R for data science
    • R Operators
    • R Data Frame
    • R Analytics Tool
    • R Tree Package
    • Vectors in R
  • Control statement
    • If Statement in R
    • If Else Statement in R
    • Else if in R
    • Switch Statement in R
  • Loops
    • Loops in R
    • For Loop in R
    • Nested For Loop in R
    • While Loop in R
    • Next in R
  • Regression in R
    • Simple Linear Regression in R
    • Linear Regression in R
    • Multiple Linear Regression in R
    • Logistic Regression in R
    • Poisson Regression in R
    • OLS Regression in R
    • P-Value in Regression
  • Anova in R
    • ANOVA in R
    • One Way ANOVA in R
    • Two Way ANOVA in R
  • Data Structure
    • R list
    • Arrays in R
    • Data Frames in R
    • Factors in R
    • R Vectors
  • Advanced
    • Statistical Analysis with R
    • R String Functions
    • Data Exploration in R
    • R CSV Files
    • KNN Algorithm in R
    • Sorting in R
    • lm Function in R
    • Hierarchical Clustering in R
    • R Normal Distribution
    • Binomial Distribution in R
    • Decision Tree in R
    • GLM in R
    • Arima Model in R
    • Linear Model in R
    • Predict Function in R
    • Survival Analysis in R
    • Standard Deviation in R
    • Statistical Analysis in R
    • Predictive Analysis?in R
    • T-test in R
    • Database in R
  • Programs
    • Functions in R
    • Boxplot in R
    • R Program Functions
    • Factorial in R
    • Random Number Generator in R
  • Interview question
    • R Interview Questions

Related Courses

R Programming Certification Course

Statistical Analysis Course Training

All in One Data Science Courses

Plot Function in R

By Priya PedamkarPriya Pedamkar

Plot Function in R

Overview of Plot Function in R

Plot function in R language is a basic function that is useful for creating graphs and charts for visualizations. It is implemented as plot() in R programing language. The plot function supports a wide variety of function parameters for different scenarios and types of objects to be passed to it. Plot function in the R graphics package mostly used to develop the two-dimensional graphs to analyze the data set distribution or to visualize correlation among data variables. Several graphs like scatter plot and line graphs are some of the commonly used charts for exploratory data analysis which are created using plot function in R.

Syntax of the Plot Function in R

The generic syntax for a plot in Rstudio is:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,060 ratings)

Plot(x,y,…)

And its complete syntax is:

plot(x, y, type, main, sub,  xlab, ylab)

“x” provides us the data points and we will plot that data by using the above syntax. Sometimes data in X is self-sufficient for the plot that it doesn’t require any other variable.

“y” also provides us data and we plot it with X variable data.

Plots are of different kinds.

Type command is used to pass on the code like which type of plot we want.

Different Types of Plot Functions

Types of the plot are:

  • “p”: is used for points plot
  • “l”: is used for lines plot
  • “b”: is used for both point plot and lines plot in a single place
  • “c”: is used to join empty point by the lines
  • “o”: is used for both lines and over-plotted point
  • “h”: is used for ‘histogram plot’
  • “s”: is used for stair steps
  • “n”: is used for no plotting

For the title of the plot, we have to pass the “main” syntax. Similarly, for the subtitle of the plot, we have to pass “sub” syntax.

The plot is of no use if the x-axis and y-axis are not labeled. For labeling, we will use syntax “xlab” for x-axis legends and “ylab” for y-axis legends.

Examples of a Plot Functions with datasets

The basic examples of the plots have been given below:

Case 1

We have marks of 20 students of two different sections of Class 10th. X is class 10 section A and Y is class 10 section B.

X = 40, 15, 50, 12, 22, 29, 21, 35, 14, 15,49, 25, 41, 43, 30, 20, 48, 25, 18, 23

Y = 41, 42, 32, 14, 42, 27, 13, 50, 33, 22, 31, 30, 49, 25, 40, 39, 14, 37, 15, 50

  • Let’s see the line plot of class 10 section A.

X = c(40, 15, 50, 12, 22, 29, 21, 35, 14, 15, 49, 25, 41, 43, 30, 20, 48, 25, 18, 23)
plot(X ,type = "l")

Plot Function

  • Let’s see the line plot of class 10 section B.

Y = c(41, 42, 32, 14, 42, 27, 13, 50, 33, 22, 31, 30, 49, 25, 40, 39, 14, 37, 15, 50)
plot(Y, type = "l")

Plot Function

  • Let’s see the point plot of Class 10 section A.

X = c(40, 15, 50, 12, 22, 29, 21, 35, 14, 15, 49, 25, 41, 43, 30, 20, 48, 25, 18, 23)> plot(X ,type = "p")
Plot Function

  • Let’s see the point plot of Class 10 section B.

Y = c(41, 42, 32, 14, 42, 27, 13, 50, 33, 22, 31, 30, 49, 25, 40, 39, 14, 37, 15, 50)> plot(Y, type = "p")
Plot Function

Lastly, we can see a mixture of both points and lines for both the section.

Class 10 section A
X = c(40, 15, 50, 12, 22, 29, 21, 35, 14, 15, 49, 25, 41, 43, 30, 20, 48, 25, 18, 23)> plot(X ,type = "b")
Class 10 Section A

Class 10 section B

Y = c(41, 42, 32, 14, 42, 27, 13, 50, 33, 22, 31, 30, 49, 25, 40, 39, 14, 37, 15, 50)> plot(Y, type = "b")
Class 10 Section

Case 2

Let’s consider a situation where we have to plot data that provides the marks of a class. In class, there are 50 students. In this case, we will see how to add the name of the axis, title and all.

Roll number Marks
1 43
2 55
3 62
4 86
5 99
6 43
7 76
8 29
9 84
10 58
11 68
12 56
13 89
14 31
15 20
16 77
17 75
18 94
19 61
20 35
21 54
22 41
23 63
24 32
25 73
26 58
27 26
28 45
29 60
30 81
31 52
32 29
33 48
34 36
35 51
36 53
37 99
38 20
39 57
40 28
41 64
42 39
43 59
44 31
45 84
46 76
47 28
48 74
49 68
50 64

Now we have to present this data in the plot.

Note: Code, in this case, is based on the situation where the data is in excel, by doing this I like to showcase how we upload the data into R and process it if we have to make a plot out of it.

plot_data = read.csv("Plots in R.csv",header = TRUE) > plot(plot_data$Roll.number, plot_data$Marks, type = "p", xlab = 'Marks', ylab = 'Roll Number')

Plots Data

In this plot, we can see the name of the titles. On the x-axis, we have marks, on the y-axis we have roll number. Like on the same lines we can add the title of the plot also which we will see in the below code.

plot(plot_data$Roll.number, plot_data$Marks, type = "p", xlab = 'Marks', ylab = 'Roll Number', main = 'Result')
real number

Advantages of Plot in R Function

Advantages of the plot:

  • Pass on the findings in constructive ways to the stakeholders.
  • Easy to understand
  • The human brain can process visual information more easily than written information.
  • Quick access to relevant information
  • Easy Identification of latest trends
  • Simple comprehension of data

Conclusion

Data is available in an enormous amount. It is not easy to convert the data into that structure which provides some meaningful insights. One of the best structure which converts data into precise and meaningful format is the plot (if we say in large “visualization”). Researchers, data scientists, economists always prefer plots if they want to showcase any data. Plots are easy to understand, the learnings derived from plots can last long in the mind.

If you think that there is too much data and you want to pass on the learnings of that data to your audience, the best way is to use the plot. The only precaution you have to take is to find which type of plot is the best fit for your data points. Once you find the right type, writing code or syntax is not tough.

Recommended Articles

This is a guide to Plot Function in R. Here we discuss the introduction, Syntax of the Plot Function in R, Examples of a plot and their Types along with the Advantages. You can also go through our other suggested articles to learn more–

  1. R Boxplot labels
  2. Piecewise Function in Matlab
  3. Filter Function in Matlab
  4. Scatterplot in R
  5. Complete Guide to MATLAB Plot Function
Popular Course in this category
R Programming Training (13 Courses, 20+ Projects)
  13 Online Courses |  20 Hands-on Projects |  120+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Statistical Analysis Training (15 Courses, 10+ Projects)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
1 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more