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
Example-5:-
Code:-
Result:-
Explanation:-
1. The Global Mapping (aes)
The aes() (aesthetics) function maps your data variables to visual properties.
2. Geometric Layers (geom_*)
These parameters are often used outside of aes() to set "static" values for every point or line in that layer
3. Theme Customization (theme)
The theme() function controls non-data elements. It has nearly 100 parameters, categorized by "element functions"
4. Scales and Labels (scale_* & labs)
Scales translate data into the visual aesthetics you see
5. Faceting and Coordinates (facet_* & coord_*)



