0% found this document useful (0 votes)
13 views11 pages

26th Lecturer PDF

lecture notes

Uploaded by

pankaj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views11 pages

26th Lecturer PDF

lecture notes

Uploaded by

pankaj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Confidential - Oracle Restricted

Using plot with Coordinate Vectors

1. Concept of Plotting in R:

o The screen is treated as a blank, two-dimensional canvas for


plotting.

o Points and lines are plotted using x- and y-coordinates.

2. Coordinate Representation:

o Coordinates are typically written as pairs (x value, y value) on


paper.

o In R, the plot function requires two vectors:

 A vector for x-coordinates.

 A vector for y-coordinates.

3. Functionality of plot in R:

o The plot() function opens a graphics device and displays the plot.

o If a graphics device is already open, R refreshes the device and


overwrites the existing plot with the new one.

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):

o Define the x-coordinates: foo <- c(1.1, 2, 3.5, 3.9, 4.2)

o Define the y-coordinates: bar <- c(2, 2.2, -1.3, 0, 0.2)

5. Plotting Example in R:

foo <- c(1.1, 2, 3.5, 3.9, 4.2)

bar <- c(2, 2.2, -1.3, 0, 0.2)

plot(foo, bar)

6. Key Points About plot Behavior:

o X-locations (foo) are provided as the first argument.

o Y-locations (bar) are provided as the second argument.

o The plot shows the specified points on the graphics device.

Confidential - Oracle Restricted


Confidential - Oracle Restricted

 Matrix as Input for plot:

 In R, x and y coordinates can also be provided in a matrix or list


format instead of separate vectors.

 The matrix should have x values in the first column and y values in the
second column.

 Creating a Matrix Example:

 Using cbind(), you can combine foo (x values) and bar (y values) into a
matrix.

 Example:

baz <- cbind(foo, bar)

print(baz)

This produces:

Confidential - Oracle Restricted


Confidential - Oracle Restricted

csharp

foo bar

[1,] 1.1 2.0

[2,] 2.0 2.2

[3,] 3.5 -1.3

[4,] 3.9 0.0

[5,] 4.2 0.2

 Plotting Using a Matrix:

 The plot() function can directly take this matrix as input to create the
scatterplot.

 Example:

plot(baz)

 Versatility of plot Function:

 plot() is a generic function in R, meaning its behavior changes based


on the object type.

 For scatterplots, the version used is plot.default. This is the default


method for plotting.

Graphical Parameters in R

1. Purpose of Graphical Parameters:

o Enhance visuals and customize technical aspects of plots.

o Used as arguments in plot and other plotting functions.

2. Common Graphical Parameters:

o type: Defines how points are plotted (e.g., as dots, lines, or


both).

o main, xlab, ylab: Add a title and labels for the x-axis and y-axis.

Confidential - Oracle Restricted


Confidential - Oracle Restricted

o col: Specifies the color(s) of points and lines.

o pch: Chooses the character or symbol for plotting points.

o cex: Controls the size of the points or characters.

o lty: Sets the line type (e.g., solid, dashed, or dotted).

o lwd: Adjusts the line thickness.

o xlim, ylim: Define the horizontal and vertical range of the plot.

Automatic Plot Types in R

1. Default Plot Type:

o plot() generates a scatterplot with individual points by default.

2. Customizing Plot Types:

o Use the type argument to control how data is displayed.

 "p": Points (default).

 "l": Lines connecting points (useful for time series).

 "b": Both points and lines.

3. Example: Plot with Lines:

o Using foo (x-coordinates) and bar (y-coordinates) from earlier


examples:

plot(foo, bar, type = "l") # Plots lines connecting points

Confidential - Oracle Restricted


Confidential - Oracle Restricted

Key Example

1. Plotting Individual Points:

plot(foo, bar) # Default: individual points

2. Connecting Points with Lines:

plot(foo, bar, type = "l") # Points connected by lines

3. Customizing Appearance:

plot(foo, bar, type = "b", col = "blue", pch = 19, lty = 2, lwd = 2,

main = "Custom Plot", xlab = "X-Axis Label", ylab = "Y-Axis Label")

This creates a plot with:

o Blue points and lines.

o Solid circular points (pch = 19). pch in R is "point character"

o Dashed lines (lty = 2) with thickness 2 (lwd = 2).

Lty= "Line Type" 

o lty = 1: Solid line


o lty = 2: Dashed line
o lty = 3: Dotted line
o lty = 4: Dot-dash line

lwd= Line Width

o lwd = 1: Standard thickness


o lwd = 2: Thicker line
o lwd = 3: Even thicker line

o A title and axis labels.

Title and Axis Labels


1. Default Plot Titles and Axis Labels:

Confidential - Oracle Restricted


Confidential - Oracle Restricted

o By default, a basic plot in R doesn't include a main title or axis


labels.

o The x-axis and y-axis are labeled with the names of the vectors
being plotted.

2. Adding a Title and Axis Labels:

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.

3. Escape Sequences in Labels:

o The labels can include escape sequences (e.g., \n for new


lines). This helps in formatting titles or axis labels for better
readability.

4. Example of Adding Titles and Labels:

o The following code adds a title and axis labels to a plot:

plot(foo, bar, type = "b", main = "My lovely plot", xlab = "x axis label", ylab
= "location y")

5. Using Escape Sequences for Multi-line Titles:

o You can use \n to split the title into multiple lines:

plot(foo, bar, type = "b", main = "My lovely plot\ntitle on two lines", xlab =
"", ylab = "")

6. Empty Axis Labels:

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.

Confidential - Oracle Restricted


Confidential - Oracle Restricted

Colors
 Importance of Color in Graphs:

 Color is not just for aesthetics; it can significantly improve data clarity.

 It helps in distinguishing between different levels of factors or


highlighting important data points.

 Setting Colors in R:

 The col parameter in the plot function is used to set the color of points
and lines.

 Colors can be specified in several ways:

o Integer Selector: Integer values corresponding to specific


colors.

o Character String: Using named colors (e.g., "red", "blue",


"seagreen4").

 Examples of Color Usage:

 Using an integer for color:

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).

 Using a color name:

Confidential - Oracle Restricted


Confidential - Oracle Restricted

plot(foo, bar, type = "b", main = "My lovely plot", xlab = "", ylab = "", col =
"seagreen4")

This plots the graph with the color "seagreen4".

 Available Colors:

 There are 8 integer values representing basic colors.

 R also recognizes around 650 color names that can be used directly
as character strings.

 Example of basic colors: 1 is black, 2 is red, 3 is green, etc.

 To view the available colors, use the command:

colors()

 Advanced Color Options:

 Beyond basic colors, you can define colors using RGB (Red, Green,
Blue) values or create custom color palettes.

Line and Point Appearances


 Altering the Appearance of Points and Lines:

 pch: Controls the character used to plot the points.

 lty: Alters the line type used in the plot.

 pch (Point Character):

 The pch parameter allows you to select a specific symbol for each
plotted point.

 You can specify a single character or a number between 1 and 25 to


select predefined symbols.

 Example:

o pch = 1: Circle

o pch = 2: Triangle

Confidential - Oracle Restricted


Confidential - Oracle Restricted

o pch = 8: Star

 A reference plot showing all the point characters can be found in


Figure 7-5 (middle plot).

 lty (Line Type):

 The lty parameter controls the type of line used to connect the points.

 lty can take values from 1 to 6:

o lty = 1: Solid line

o lty = 2: Dashed line

o lty = 3: Dotted line

o Values from 1 to 6 represent different line styles, as shown in


Figure 7-5 (rightmost plot).

 cex (Character Expansion):

 The cex parameter controls the size of plotted points.

 Default value is 1, and it can be adjusted to resize the points.

o Example:

 cex = 0.5: Half-size points.

 cex = 2.3: Larger points.

 lwd (Line Width):

 The lwd parameter controls the thickness of the lines.

 The default value is 1, and it can be increased for thicker lines.

o Example:

 lwd = 2: Double-thick lines.

 lwd = 3.3: Thicker lines.

 Example of Plot Customization:


The following code creates plots with customized points and lines using pch,
lty, cex, and lwd:

Confidential - Oracle Restricted


Confidential - Oracle Restricted

plot(foo, bar, type = "b", main = "My lovely plot", xlab = "", ylab = "",

col = 4, pch = 8, lty = 2, cex = 2.3, lwd = 3.3)

plot(foo, bar, type = "b", main = "My lovely plot", xlab = "", ylab = "",

col = 6, pch = 15, lty = 3, cex = 0.7, lwd = 2)

 First plot:

o col = 4 (color: blue), pch = 8 (star symbol), lty = 2 (dashed line),


cex = 2.3 (larger points), lwd = 3.3 (thicker lines).

 Second plot:

o col = 6 (color: purple), pch = 15 (filled square), lty = 3 (dotted


line), cex = 0.7 (smaller points), lwd = 2 (double-thick lines).

Plotting Region Limits


 Default Plotting Region Limits:

 By default, R automatically sets the axis range based on the minimum


and maximum values of the supplied x and y data.

 It adds a small padding around the outermost data points to make the
plot easier to view.

 Customizing Plot Limits:

 xlim: Controls the horizontal axis (x-axis) limits.

 ylim: Controls the vertical axis (y-axis) limits.

 Both parameters accept a numeric vector of length 2: c(lower, upper),


where lower and upper specify the limits of the respective axis.

 Example 1: Expanding the Plotting Area:

 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))

Confidential - Oracle Restricted


Confidential - Oracle Restricted

This adds a larger plotting area to fit additional annotations or points outside
the range of the original data.

 Example 2: Restricting the Plotting Area:

 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.

 Effects of xlim and ylim:

 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.

Confidential - Oracle Restricted

You might also like