26th Lecturer PDF
26th Lecturer PDF
1. Concept of Plotting in R:
2. Coordinate Representation:
3. Functionality of plot in R:
o The plot() function opens a graphics device and displays the plot.
4. Example Coordinates:
To plot the points (1.1, 2), (2, 2.2), (3.5, −1.3), (3.9, 0), and (4.2, 0.2):
5. Plotting Example in R:
plot(foo, bar)
The matrix should have x values in the first column and y values in the
second column.
Using cbind(), you can combine foo (x values) and bar (y values) into a
matrix.
Example:
print(baz)
This produces:
csharp
foo bar
The plot() function can directly take this matrix as input to create the
scatterplot.
Example:
plot(baz)
Graphical Parameters in R
o main, xlab, ylab: Add a title and labels for the x-axis and y-axis.
o xlim, ylim: Define the horizontal and vertical range of the plot.
Key Example
3. Customizing Appearance:
plot(foo, bar, type = "b", col = "blue", pch = 19, lty = 2, lwd = 2,
o The x-axis and y-axis are labeled with the names of the vectors
being plotted.
o You can add a main title, x-axis label, and y-axis label by
providing text as character strings.
o Use the main, xlab, and ylab parameters for this purpose.
plot(foo, bar, type = "b", main = "My lovely plot", xlab = "x axis label", ylab
= "location y")
plot(foo, bar, type = "b", main = "My lovely plot\ntitle on two lines", xlab =
"", ylab = "")
o In the second plot, xlab and ylab are set to an empty string ("")
to prevent R from labeling the axes with the names of the
vectors.
Colors
Importance of Color in Graphs:
Color is not just for aesthetics; it can significantly improve data clarity.
Setting Colors in R:
The col parameter in the plot function is used to set the color of points
and lines.
plot(foo, bar, type = "b", main = "My lovely plot", xlab = "", ylab = "", col =
2)
This plots the graph with the second color in R’s default color set (usually
red).
plot(foo, bar, type = "b", main = "My lovely plot", xlab = "", ylab = "", col =
"seagreen4")
Available Colors:
R also recognizes around 650 color names that can be used directly
as character strings.
colors()
Beyond basic colors, you can define colors using RGB (Red, Green,
Blue) values or create custom color palettes.
The pch parameter allows you to select a specific symbol for each
plotted point.
Example:
o pch = 1: Circle
o pch = 2: Triangle
o pch = 8: Star
The lty parameter controls the type of line used to connect the points.
o Example:
o Example:
plot(foo, bar, type = "b", main = "My lovely plot", xlab = "", ylab = "",
plot(foo, bar, type = "b", main = "My lovely plot", xlab = "", ylab = "",
First plot:
Second plot:
It adds a small padding around the outermost data points to make the
plot easier to view.
The first plot expands the plotting area to include additional space
around the data points:
plot(foo, bar, type = "b", main = "My lovely plot", xlab = "", ylab = "",
col = 4, pch = 8, lty = 2, cex = 2.3, lwd = 3.3, xlim = c(-10, 5), ylim = c(-
3, 3))
This adds a larger plotting area to fit additional annotations or points outside
the range of the original data.
The second plot restricts the visible area, focusing only on a subset of
the data:
plot(foo, bar, type = "b", main = "My lovely plot", xlab = "", ylab = "",
col = 6, pch = 15, lty = 3, cex = 0.7, lwd = 2, xlim = c(3, 5), ylim = c(-
0.5, 0.2))
This zooms into a specific portion of the data by narrowing the x- and y-axis
limits.
First Plot: The x-axis and y-axis limits are wider than the data, leaving
extra space around the points.
Second Plot: Only a specific portion of the data is shown, as the plot's
range is constrained to a subset of the data values.