Page 267 - Data Science class 11
P. 267
png(file = "scatterplot.png")
# Plotting the chart for cars with weight between 2.5 to 5 and mileage between 15
and 30.
plot(x = input$wt, y = input$mpg,
xlab = "Weight",
ylab = "Mileage ",
xlim = c(2.5,5),
ylim = c(15,30),
main = "Weight vs Mileage"
)
# Saving the file.
dev.off()
The scatterplot( ) function in the car package offers many enhanced features, including fit lines, marginal box plots,
conditioning on a factor, and interactive point identification. Each of these features is optional. We will discuss fitline
only here because it is used for regression.
A scatter plot can be created using the function plot(x, y). The function lm() will be used to fit linear models between
y and x. A regression line will be added on the plot using the function abline(), which takes the output of lm() as an
argument.
A line of best fit refers to a line through a scatter plot of data points that best expresses the relationship between
those points.
A trend line is a straight line that best represents the points on a scatterplot. The trend line may go through some
points but need not go through them all. The trend line is used to show the pattern of the data. The Trendline is the
best line that fits through the points.
Example
The below script will create a scatterplot graph for the producing fitline in relation to wt(weight) and mpg(miles per
gallon).
Coding for Data Science Visualisation using R-Studio 265

