Basic Plot in R

 

Basic Plot in R

Introduction

Visualization is one of the most powerful features of R. The plot() function is the most fundamental graphics function in R and is used to create a variety of graphs such as:

  • Scatter plots
  • Line graphs
  • Points plots
  • Combined line and point plots

The exact graph produced by plot() depends on the type of data supplied.


Syntax

plot(x, y,
main,
xlab,
ylab,
col,
pch,
type,
lwd)

where

  • x : x-axis values
  • y : y-axis values
  • main : title of the graph
  • xlab : x-axis label
  • ylab : y-axis label
  • col : color
  • pch : plotting symbol
  • type : type of graph
  • lwd : line width

1. Simple Scatter Plot

Suppose we have marks of five students.

marks <- c(75,85,90,60,95)

plot(marks)

Output:




Adding Title and Labels

marks <- c(75,85,90,60,95)

plot(marks,
main="Student Marks",
xlab="Student Number",
ylab="Marks")




2. Scatter Plot Between Two Variables

age <- c(18,19,20,21,22)

marks <- c(70,75,85,90,95)

plot(age,
marks,
main="Age vs Marks",
xlab="Age",
ylab="Marks")

This produces a scatter plot.




3. Plot Types

The argument type determines how data is displayed.

Points (type="p")

(Default)

plot(age, marks, type="p")

Line Plot (type="l")

plot(age, marks,
type="l",
col="blue")




Both Points and Lines (type="b")

plot(age, marks,
type="b",
col="red")




Histogram-like Vertical Lines (type="h")

plot(age, marks,
type="h")




Step Plot (type="s")

plot(age, marks,
type="s")




4. Colors

Use the col parameter.

plot(age,
marks,
col="red")

Other colors:

"blue"
"green"
"yellow"
"orange"
"black"
"purple"

5. Plot Symbols (pch)

plot(age,
marks,
pch=16)

Common symbols:



Example:

plot(age,
marks,
pch=17,
col="blue")





6. Line Width

plot(age,
marks,
type="l",
lwd=3)

Larger lwd means thicker lines.




7. Changing Axis Limits

plot(age,
marks,
xlim=c(15,25),
ylim=c(50,100))

8. Adding Grid

plot(age,
marks)

grid()

9. Adding More Points

plot(age,
marks)

points(20,80,
col="red",
pch=19)

Adds a point at (20,80).




10. Adding Lines

plot(age,
marks)

lines(age,
marks,
col="blue")




11. Adding Text

plot(age,
marks)

text(age,
marks,
labels=marks,
pos=3)

Output:

Each point is labeled with its mark.




12. Multiple Curves on Same Graph

year <- c(2020,2021,2022,2023,2024)

sales1 <- c(10,15,20,25,30)
sales2 <- c(12,18,22,28,35)

plot(year,
sales1,
type="l",
col="blue",
ylim=c(0,40))

lines(year,
sales2,
col="red")

legend("topleft",
legend=c("Product A","Product B"),
col=c("blue","red"),
lty=1)




13. Plotting Mathematical Functions

𝑦=𝑥2
x <- seq(-5,5,0.1)

y <- x^2

plot(x,
y,
type="l",
col="blue",
main="y=x^2")



14. Sine Function

x <- seq(0,2*pi,0.1)

y <- sin(x)

plot(x,
y,
type="l",
col="red")




15. Using Sequences

x <- 1:10

y <- x^3

plot(x,
y,
type="b")



Important Parameters

ParameterPurpose
main    Title
xlab    X-axis label
ylab    Y-axis label
col    Color
pch    Point symbol
type    Plot type
lwd    Line width
xlim    X-axis range
ylim    Y-axis range
cex    Point size
grid()    Add grid
text()    Add labels
points()    Add points
lines()    Add curves
legend()    Add legend

Common Values of type

TypeMeaning
"p"Points
"l"Lines
"b"Both
"o"Overplotted
"h"Vertical lines
"s"Stair steps
"n"Empty plot

Applications of plot()

  • Scatter plots
  • Mathematical function graphs
  • Growth trends
  • Sales analysis
  • Population studies
  • Experimental data visualization
  • Time-series analysis

Conclusion

The plot() function is the foundation of graphics in R. By changing parameters such as type, col, pch, and lwd, a variety of plots can be produced. Additional functions like lines(), points(), text(), and legend() can be used to enrich the graph. Mastering plot() is essential before learning advanced graphics packages such as ggplot2.

Comments

Popular posts from this blog

Statistical Methods Lab ( R Language) PCCBL308 Semester 3 KTU BTech CB and CU 2024 Scheme - Dr Binu V P

Programs in R - using control statements - Assignment 2

Basic R Programs to Try - Assignment 1