Saturday, January 31, 2026

Chapter-5:- Scatter Plot in R-programming

 Chapter-5:- Scatter Plot in R-programming



Example-1:- 
Create a scatter plot of car weight on x-axis and miles per gallon on y-axis by pre-program data "mtcars" in R-language

Code:-
mtcars
plot(mtcars$wt, mtcars$mpg,
     main = "Car Weight vs Fuel Efficiency",
     xlab = "car weight",
     ylab = "Miles Per Gallon",
     pch = 19,         # Solid circle shape
     col = "blue", # Change point color
     frame = FALSE)    # Remove the surrounding box


Result:-



Explanation:-
  • In 1st line, "mtcars" is inbuilt program or database in R-programming language, you can in result image with yellow square box, it contain multiple car model with mpg, disp, wt, gear, carb etc. 
  • In 2nd line plot() use, we discuss this already in previous chapter it contain multiple parameter like col, lwd, lty, col, xlim, ylim etc.
  • In 2nd line, you see mtcars$wt, here you see $ sign, it use mainly to collect data from specific column like here we use wt, so it collect data of wt column of different model of cars
  • Also we use mtcars$mpg, it means it collect data from mpg column of database
  • In last line, frame if TRUE it make a box around it, if FALSE then you see no box surround the result image


Example-2:-
Plot sepal length vs sepal width for all species, set the x-axis range from 4 to 8 and y-axis from 2 to 4.5 by pre-program database "iris"

Code:-
iris
plot(iris$Sepal.Length, iris$Sepal.Width,
     col = c("blue", "yellow"),    # For color of the sepal
     pch = 19,                    # solid circle denote by 19
     main = "Custom Species Colors")   #main title


Result:-



Explanation:-
  • Iris is also pre-program or inbuilt data base in R-programming language like the "mtcars"
  • You can see in result image in yellow square box, "iris" contain 5 different column like Sepal.length, Sepal.width, species etc.
  • If you want to collect data from specific column like Sepal.Length then we use $ sign as you can see in 2nd line code
  • For use different color, use c(1st color name, 2nd color name), here c is compulsary to use, If you read previous chapter then no difficulty to understand
  • pch- use for different shape/ symbol to denote
  • main- for main title 


Example-3:- 
If in above example if ask to arrange the data according to petal width (means according to petal width the plot size increase or decrease, higher the petal width more size of plot in result graph)

Code:-
iris
plot(iris$Petal.Width,
     col = "red",    # For color of the sepal
     pch = 19,                    # solid circle denote by 19
     main = "Custom Species Colors",   #main title
     cex=iris$Petal.Width,    #Increase solid circle size according to value
     las=1,     # To make y-axis text horizontal
     frame.plot=FALSE)   #Remove top and right border of box


Result:-


Explanation:-


  • Compare to previous example here additional command "cex" , frame.plot
  • All the point already explain with comment in code


Example-4:-
Create a scatter plot of girth vs volume. Before plotting the points, add a light grey background grid using panel.first= grid() by using pre-program feature "trees", also add regression line to show the trend

Code:-
trees   #Load the database
plot(trees$Girth, trees$Volume,
     panel.first= grid(col="grey",lty="dotted"), #Create grid with background grey and dotted
     col="red",pch=20,cex=1.5)

#Add a red regression line
abline(lm(Volume ~ Girth, data = trees), col = "blue", lwd = 2)

Result:-



Explanation:-
  • "trees" also inbuilt database in R-programming

Component

Function

Details

abline()

Draws a line

Function that draws a straight line on the plot

lm()

Fits a linear model

Calculate the best fit straight line that minimize the distance between line and your data points

Volume ~ Girth

Model formula

Means volume vs girth graph (use here “~”)

data

Specifies data

Like in this case trees which is inbuilt data

col

Color

Set the color of line

lwd

Line width

Sets the line width which default is 1





Example-5:-

Code:-

Result:-

Explanation:- 

1. The Global Mapping (aes)
The aes() (aesthetics) function maps your data variables to visual properties.

x,y

Horizontal and vertical position

color

Outline color of points, lines or borders

fill

Interior color for shapes

size

Diameter of points or thickness of lines

shape

Style of a point (0-25)

alpha

Transparency level (0 for transparent, 1 for opaque)

linetype

Pattern of a line (eg. solid, dashed, dotted)

group

Used to identify distinct sets of data



2. Geometric Layers (geom_*)
These parameters are often used outside of aes() to set "static" values for every point or line in that layer

data

Overrides the global dataset

stat

Overrides the default statistical transformation (eg stat= identity)

position

Adjusts how elements sit (eg. position= “dodge” for side-by-side bars)

na.rm

If TRUE, missing values are removed silently



3. Theme Customization (theme)
The theme() function controls non-data elements. It has nearly 100 parameters, categorized by "element functions" 

Text Element (element_text)

plot.title, axis.title.x, legend.text

Parameters include family, face, size, hjust and vjust

Line Element (element_line)

panel.grid.major, axis.ticks

Parameters include color, linewidth and linetype

Rectangle Elements (element_rect)

panel.background, plot.background

Parameters include fill and color

Blank Elements (element_blank)

Removes an element entirely (eg. panel.grid.minor= element_blank()



4. Scales and Labels (scale_* & labs)
Scales translate data into the visual aesthetics you see

name

Axis or legend title

limits

Min/Max values of the scale

breaks

The specific points where labels/tick appear

labels

The text that appears at those breaks

labs()

A shortcut function to set title, subtitle, caption, x and y labels



5. Faceting and Coordinates (facet_* & coord_*)

Faceting 

facet_wrap (~variable)

facet_grid (rows~cols)

Parameters include scales (eg. free_x) and nrow/ncol

Coordinates

coord_flip()       [swaps X and Y)

coord_polar()    [circular plot)

coord_cartesian ()      [zooming without removing data]

Wednesday, January 21, 2026

Chapter-4:- Line graph in R-programming

Example-1:-
Create a line graph showing a 5-day temperature trend using the vector temps <- c(22,24,23,26,25)

Requirements:-
Set the line type to solid with points at each data value
Color the line red and make it twice the default thickness
Add a title "Weekly Temperature" and label the axes "Day" and "Temp (C)


Code:-
temps <- c(22, 24, 23, 26, 25)
plot(temps, type = "o",
     col = "red",
     lwd = 2, 
     main = "Weekly Temperature",
     xlab = "Day",
     ylab = "Temp (C)",
     xlim=c(1,8),
     ylim=c(10,30),
     lty="dotted",
     cex=0.6, #size of dot
     pch=5)  #different symbol in range 1-25


Solution:-


Explanation:-

All parameter of plot():-

Parameter

Purpose

Accepted Values

type

Specifies plot style

“l” (line), “p” (points), “o” (both overlaid), “b” (both separate), “h” (histogram like vertical line)

col

Color of the line

Color name (eg “blue”) or hex code

bg

Background color

“bg” only work with pch value between 21-25 or “bg” use with par(bg “color name” )

lty 

Line type

1: solid (default), 2:dashed, 3:dotted, 4:dot-dash, 5:long-dash

lwd

Line width

Numerical value; 2 is twice the default thickness

main 

Title of the chart

A text string

sub

Subtitle

A subtitle displayed below x-axis

asp

y/x aspect ratio

If asp=1 then x-axis equal to y-axis, If asp=2 then y-axis more than x-axis

xlab/ ylab

Axis labels

Text string for the X and Y axes

xlim/ ylim

Axis limits

A vector of two values: c(min,max)

pch

Point shape

Integer 0-25 (used when type= “o” or “b”)

cex

Point/ text size

Numerical value (default is 1)

log

Applied logarithmic scaling to the axes

“x”, “y”, or “xy”

axes

Determines if the default axis lines and tick marks are drawn

TRUE (default) or FALSE

frame.plot

Draw a border box around the right and top plot area

TRUE or FALSE

las

Control the orientation of axis 

0 (parallel), 1 (horizontal), 2 (perpendicular), 3 (vertical)

ann

Determine if titles and axis labels are shown

TRUE or FALSE

panel.first

Executes an expression (like a grid) before points are drawn

Any plotting expression (eg. grid(), abline()

panel.list

Executes an expression after the points are drawn

Any plotting expression




Example-2:- If we have multiple line in question then-- y1 <- c(2, 4, 6, 8, 10),
y2 <- c(1, 3, 5, 7, 9),  y3 <- c(5, 4, 3, 2, 1)

Code:-
# Define your data 
y1 <- c(2, 4, 6, 8, 10)
y2 <- c(1, 3, 5, 7, 9)
y3 <- c(5, 4, 3, 2, 1)

# Create the main plot
plot(y1, type="b", col="blue", pch=16, ylim=c(0, 10), ylab="Value", main="Base R Plot")

#If we have to add more line then we use only line command instead of plot
lines (y2, type="b", col="red", pch=17)
lines (y3, type="b", col="green", pch=18)

# Add a legend
legend=c("y1", "y2", "y3"),  col=c("blue", "red", "green"), pch=16:18, lty=1, 
      inset=c(-0.35, 0), cex=0.8) 

Solution:-


Explanation:-
  • In 2nd, 3rd, 4th line we just put the data which given in question
  • To add the line first we use plot command, as you can see above I explain all the parameter in a table , pch= mainly for different symbol (Put value in between 1-25) 
  • To add more line then we use only "lines" command instead of plot here
  • In last command we use legend to create a seperate box to show the data, but when you try to put the legend box out of the main box it not appear (see next example)



Example-3:- Problem in put the legend box out of the main box

Code:-
# Define your data 
y1 <- c(2, 4, 6, 8, 10)
y2 <- c(1, 3, 5, 7, 9)
y3 <- c(5, 4, 3, 2, 1)

# Create the main plot
plot(y1, type="b", col="blue", pch=16, ylim=c(0, 10), ylab="Value", main="Base R Plot")

#If we have to add more line then we use only line command instead of plot
lines (y2, type="b", col="red", pch=17)
lines (y3, type="b", col="green", pch=18)

# Add a legend
legend=c("y1", "y2", "y3"),  col=c("blue", "red", "green"), pch=16:18, lty=1, 
      inset=c(-0.02, 0.4), cex=0.5) 

Result:-

Explanation:- As you can see here when have change the inset value, legend box not seen completely. To fix this problem we use par(xpd=TRUE) , check in next example



Example-4:- Legend box out of the main box 

Code:-
# Define your data 
y1 <- c(2, 4, 6, 8, 10)
y2 <- c(1, 3, 5, 7, 9)
y3 <- c(5, 4, 3, 2, 1)

# Create the main plot
plot(y1, type="b", col="blue", pch=16, ylim=c(0, 10), ylab="Value", main="Base R Plot")

# If want to put legend box out of the main box 
par(xpd=TRUE)

#If we have to add more line then we use only line command instead of plot
lines (y2, type="b", col="red", pch=17)
lines (y3, type="b", col="green", pch=18)

# Add a legend
legend=c("y1", "y2", "y3"),  col=c("blue", "red", "green"), pch=16:18, lty=1, 
      inset=c(-0.02, 0.4), cex=0.5) 

Result:-

Explanation:- In this we use par(xpd=TRUE) to visualize the legend box out of the main box, but you see one problem here that space on the right side is not so much that I put legend box. So, either I have to decrease the font size of legend box by cex command (but that cause font size small and difficult to visualize) or we use par (mar=c ()) command see in next example



Example-5:- To create more space or more data 

Code:-
y1 <- c(2, 4, 6, 8, 10)
y2 <- c(1, 3, 5, 7, 9)
y3 <- c(5, 4, 3, 2, 1)

# Create the main plot
plot(y1, type="b", col="blue", pch=16, ylim=c(0, 10), ylab="Value", main="Base R Plot")

# If want to put legend box out of the main box 
par(xpd=TRUE)

# If you want space on right side as you can see in the image there is no space to put legend box out of the main box
par(mar=c(2, 2, 2, 4) +0.1)

#If we have to add more line then we use only line command instead of plot
lines (y2, type="b", col="red", pch=17)
lines (y3, type="b", col="green", pch=18)

# Add a legend
legend=c("y1", "y2", "y3"),  col=c("blue", "red", "green"), pch=16:18, lty=1, 
      inset=c(-0.02, 0.4), cex=0.5) 

Result:-


Explanation:-

The command par(mar=c(2, 2, 2, 4) +0.1)

Value

Margin

Description

2

Bottom

Sets the bottom margin to approximately 2.1 lines of text (usually use for label x-axis) if space not present

2

Left

Sets the left margin to approximately 2.1 lines (usually use for label y-axis)

2

Top

Sets the top margin to approximately 2.1 lines (usually for the main plot title)

4

Right

Sets the right margin to approximately 4.1 line of text


  • 0.1 in last is optional you can avoid this
  • As you can see "4" in par(mar=c(2,2,2,4) denote the right side , which create space in the right side of image so you put legend box in that space


Chapter-5:- Scatter Plot in R-programming

 Chapter-5:- Scatter Plot in R-programming Example-1:-  Create a scatter plot of car weight on x-axis and miles per gallon on y-axis by pre-...