The plot () f u nction and its options DATA VISU AL IZATION IN R - - PowerPoint PPT Presentation

the plot f u nction and its options
SMART_READER_LITE
LIVE PREVIEW

The plot () f u nction and its options DATA VISU AL IZATION IN R - - PowerPoint PPT Presentation

The plot () f u nction and its options DATA VISU AL IZATION IN R Ron Pearson Instr u ctor Some options can or m u st be specified globall y library(MASS) # mfrow specified globally par(mfrow = c(1, 2)) # cex.main specified locally or


slide-1
SLIDE 1

The plot() function and its options

DATA VISU AL IZATION IN R

Ron Pearson

Instructor

slide-2
SLIDE 2

DATA VISUALIZATION IN R

Some options can or must be specified globally

library(MASS) # mfrow specified globally par(mfrow = c(1, 2)) # cex.main specified locally or globally par(cex.main = 0.8) plot(whiteside$Temp, whiteside$Gas) title("Gas vs. Temp scatterplot") plot(whiteside$Insul) title("Insul barplot")

slide-3
SLIDE 3

DATA VISUALIZATION IN R

Other options can only be specified locally

library(MASS) indexA <- which(whiteside$Insul == "After") indexB <- which(whiteside$Insul == "Before") x <- whiteside$Temp y <- whiteside$Gas # high-level function plot(x[indexA], y[indexA], type = "o", pch = 16, xlim = range(x), ylim = range(y), xlab = "Outside temperature", ylab = "Heating gas consumption") # low-level function lines(x[indexB], y[indexB], type = "o", pch = 1) legend("topright", pch = c(1, 16), legend = c("Before insulation", "After insulation")) title("A local specification: type = 'o'")

slide-4
SLIDE 4

DATA VISUALIZATION IN R

library(MASS) indexA <- which(whiteside$Insul == "After") indexB <- which(whiteside$Insul == "Before") x <- whiteside$Temp y <- whiteside$Gas # high-level function plot(x[indexA], y[indexA], type = "o", pch = 16, xlim = range(x), ylim = range(y), xlab = "Outside temperature", ylab = "Heating gas consumption") # low-level function lines(x[indexB], y[indexB], type = "o", pch = 1) legend("topright", pch = c(1, 16), legend = c("Before insulation", "After insulation")) title("A local specification: type = 'o'")

slide-5
SLIDE 5

DATA VISUALIZATION IN R

library(MASS) indexA <- which(whiteside$Insul == "After") indexB <- which(whiteside$Insul == "Before") x <- whiteside$Temp y <- whiteside$Gas # high-level function plot(x[indexA], y[indexA], type = "o", pch = 16, xlim = range(x), ylim = range(y), xlab = "Outside temperature", ylab = "Heating gas consumption") # low-level function lines(x[indexB], y[indexB], type = "o", pch = 1) legend("topright", pch = c(1, 16), legend = c("Before insulation", "After insulation")) title("A local specification: type = 'o'")

slide-6
SLIDE 6

DATA VISUALIZATION IN R

library(MASS) indexA <- which(whiteside$Insul == "After") indexB <- which(whiteside$Insul == "Before") x <- whiteside$Temp y <- whiteside$Gas # high-level function plot(x[indexA], y[indexA], type = "o", pch = 16, xlim = range(x), ylim = range(y), xlab = "Outside temperature", ylab = "Heating gas consumption") # low-level function lines(x[indexB], y[indexB], type = "o", pch = 1) legend("topright", pch = c(1, 16), legend = c("Before insulation", "After insulation")) title("A local specification: type = 'o'")

slide-7
SLIDE 7

DATA VISUALIZATION IN R

library(MASS) indexA <- which(whiteside$Insul == "After") indexB <- which(whiteside$Insul == "Before") x <- whiteside$Temp y <- whiteside$Gas # high-level function plot(x[indexA], y[indexA], type = "o", pch = 16, xlim = range(x), ylim = range(y), xlab = "Outside temperature", ylab = "Heating gas consumption") # low-level function lines(x[indexB], y[indexB], type = "o", pch = 1) legend("topright", pch = c(1, 16), legend = c("Before insulation", "After insulation")) title("A local specification: type = 'o'")

type = "n"

slide-8
SLIDE 8

Let's practice!

DATA VISU AL IZATION IN R

slide-9
SLIDE 9

Adding lines and points to plots

DATA VISU AL IZATION IN R

Ron Pearson

Instructor

slide-10
SLIDE 10

DATA VISUALIZATION IN R

Using the points() function to add points to a plot

library(MASS) # Plot fibre column plot(UScereal$fibre) # Find outlier index <- which(UScereal$fibre > 20) # Create points with points() points(index, UScereal$fibre[index], pch = 16, col = "red")

slide-11
SLIDE 11

DATA VISUALIZATION IN R

A refresher

slide-12
SLIDE 12

DATA VISUALIZATION IN R

Using the lines() function to add lines to a plot

library(MASS) # Create histogram truehist(geyser$duration) # Add density line lines(density(geyser$duration), lwd = 2, col = "blue") # Add title title("Old Faithful geyser duration data:? \n Histogram with Overlaid Density Plot")

slide-13
SLIDE 13

DATA VISUALIZATION IN R

Using the lines() function to add lines to a plot

library(MASS) # Create histogram truehist(geyser$duration) # Add density line lines(density(geyser$duration), lwd = 2, col = "blue") # Add title title("Old Faithful geyser duration data:? \n Histogram with Overlaid Density Plot")

slide-14
SLIDE 14

DATA VISUALIZATION IN R

Using the abline() function to add lines to a plot

library(MASS) # Create a plot plot(Cars93$Price, Cars93$Max.Price, pch = 17, xlab = "Average price", ylab = "Min & max prices") # Add points points(Cars93$Price, Cars93$Min.Price, pch = 6) # Add equality line abline(a = 0, b = 1, lty = 2, lwd = 2) title("Min & Max Price vs. Average Price \n With an Equaliy Reference Line")

slide-15
SLIDE 15

DATA VISUALIZATION IN R

Using the abline() function to add lines to a plot

library(MASS) # Create a plot plot(Cars93$Price, Cars93$Max.Price, pch = 17, xlab = "Average price", ylab = "Min & max prices") # Add points points(Cars93$Price, Cars93$Min.Price, pch = 6) # Add equality line abline(a = 0, b = 1, lty = 2, lwd = 2) title("Min & Max Price vs. Average Price \n With an Equaliy Reference Line")

slide-16
SLIDE 16

DATA VISUALIZATION IN R

Using the abline() function to add lines to a plot

library(MASS) # Create a plot plot(Cars93$Price, Cars93$Max.Price, pch = 17, xlab = "Average price", ylab = "Min & max prices") # Add points points(Cars93$Price, Cars93$Min.Price, pch = 6) # Add equality line abline(a = 0, b = 1, lty = 2, lwd = 2) title("Min & Max Price vs. Average Price \n With an Equaliy Reference Line")

slide-17
SLIDE 17

DATA VISUALIZATION IN R

Using the abline() function to add lines to a plot

library(MASS) # Create a plot plot(Cars93$Price, Cars93$Max.Price, pch = 17, xlab = "Average price", ylab = "Min & max prices") # Add points points(Cars93$Price, Cars93$Min.Price, pch = 6) # Add equality line abline(a = 0, b = 1, lty = 2, lwd = 2) title("Min & Max Price vs. Average Price \n With an Equaliy Reference Line")

slide-18
SLIDE 18

Let's practice!

DATA VISU AL IZATION IN R

slide-19
SLIDE 19

Adding text to plots

DATA VISU AL IZATION IN R

Ron Pearson

Instructor

slide-20
SLIDE 20

DATA VISUALIZATION IN R

Explanatory text

Axis labels

xlab() ylab()

Titles Legends Text in plot yourself

slide-21
SLIDE 21

DATA VISUALIZATION IN R

Overriding default titles

library(MASS) par(mfrow = c(1, 2)) plot(density(geyser$waiting)) plot(density(geyser$waiting), main = "Estimated density: \n Old Faithful waiting times")

slide-22
SLIDE 22

DATA VISUALIZATION IN R

Overriding default titles

library(MASS) par(mfrow = c(1, 2)) plot(density(geyser$waiting)) plot(density(geyser$waiting), main = "Estimated density: \n Old Faithful waiting times")

slide-23
SLIDE 23

DATA VISUALIZATION IN R

Overriding default titles

library(MASS) par(mfrow = c(1, 2)) plot(density(geyser$waiting)) plot(density(geyser$waiting), main = "Estimated density: \n Old Faithful waiting times")

slide-24
SLIDE 24

DATA VISUALIZATION IN R

The text() function

text(x, y, adj)

slide-25
SLIDE 25

DATA VISUALIZATION IN R

The text() function

library(MASS) plot(UScereal$fibre) text(5, 28, "<-- Outliers [left-justified text at (5, 28)]", adj = 0) text(65, 23, "[Right-justified text at (65, 23)]", adj = 1, col = "red") text(31, 18, "[Centered text (default) at (31, 18)]", col = "blue")

slide-26
SLIDE 26

DATA VISUALIZATION IN R

Fonts, orientations, and other text features

library(MASS) plot(Boston$rad) # "Inner city" with adjusted colour and rotation text(350, 24, adj = 1, "Inner city? -->", srt = 30, font = 2, cex = 1.2, col = "red") # "Suburbs" with adjusted colour and rotation text(100, 15, "Suburbs? -->", srt = -45, font = 3, col = "green") title("Text with varying orientations, fonts, sizes & colors")

slide-27
SLIDE 27

DATA VISUALIZATION IN R

Fonts, orientations, and other text features

library(MASS) plot(Boston$rad) # "Inner city" with adjusted colour and rotation text(350, 24, adj = 1, "Inner city? -->", srt = 30, font = 2, cex = 1.2, col = "red") # "Suburbs" with adjusted colour and rotation text(100, 15, "Suburbs? -->", srt = -45, font = 3, col = "green") title("Text with varying orientations, fonts, sizes & colors")

slide-28
SLIDE 28

DATA VISUALIZATION IN R

Fonts, orientations, and other text features

library(MASS) plot(Boston$rad) # "Inner city" with adjusted colour and rotation text(350, 24, adj = 1, "Inner city? -->", srt = 30, font = 2, cex = 1.2, col = "red") # "Suburbs" with adjusted colour and rotation text(100, 15, "Suburbs? -->", srt = -45, font = 3, col = "green") title("Text with varying orientations, fonts, sizes & colors")

slide-29
SLIDE 29

DATA VISUALIZATION IN R

Fonts, orientations, and other text features

library(MASS) plot(Boston$rad) # "Inner city" with adjusted colour and rotation text(350, 24, adj = 1, "Inner city? -->", srt = 30, font = 2, cex = 1.2, col = "red") # "Suburbs" with adjusted colour and rotation text(100, 15, "Suburbs? -->", srt = -45, font = 3, col = "green") title("Text with varying orientations, fonts, sizes & colors")

slide-30
SLIDE 30

Let's practice!

DATA VISU AL IZATION IN R

slide-31
SLIDE 31

Adding or modifying

  • ther plot details

DATA VISU AL IZATION IN R

Ron Pearson

Instructor

slide-32
SLIDE 32

DATA VISUALIZATION IN R

Adding legends to a plot

library(MASS) # Create plot plot(Cars93$Price, Cars93$Max.Price, pch = 17, col = "red", xlab = "Average Price", ylab = "Min & Max Price") # Add points points(Cars93$Price, Cars93$Min.Price, pch = 16, col = "green") # Add legend legend(x = "topleft", pch = c(16, 17), col = c("green", "red"), legend = c("Min Price", "Max Price")) # Add title title("The legend() function adds boxed explanatory text")

slide-33
SLIDE 33

DATA VISUALIZATION IN R

Adding custom axes to a plot

library(MASS) boxplot(MPG.city ~ Cylinders, data = Cars93, varwidth = TRUE, axes = FALSE) # Create an x-axis above the plot axis(side = 3, at = Cars93$Cylinders, labels = as.character(Cars93$Cylinders), las = 2) # Create a y-axis to the right of the plot axis(side = 4, col = "red", las = 1)

slide-34
SLIDE 34

DATA VISUALIZATION IN R

Adding custom axes to a plot

library(MASS) boxplot(MPG.city ~ Cylinders, data = Cars93, varwidth = TRUE, axes = FALSE) # Create an x-axis above the plot axis(side = 3, at = Cars93$Cylinders, labels = as.character(Cars93$Cylinders), las = 2) # Create a y-axis to the right of the plot axis(side = 4, col = "red", las = 1)

slide-35
SLIDE 35

DATA VISUALIZATION IN R

Adding custom axes to a plot

library(MASS) boxplot(MPG.city ~ Cylinders, data = Cars93, varwidth = TRUE, axes = FALSE) # Create an x-axis above the plot axis(side = 3, at = Cars93$Cylinders, labels = as.character(Cars93$Cylinders), las = 2) # Create a y-axis to the right of the plot axis(side = 4, col = "red", las = 1)

slide-36
SLIDE 36

DATA VISUALIZATION IN R

Adding custom axes to a plot

library(MASS) boxplot(MPG.city ~ Cylinders, data = Cars93, varwidth = TRUE, axes = FALSE) # Create an x-axis above the plot axis(side = 3, at = Cars93$Cylinders, labels = as.character(Cars93$Cylinders), las = 2) # Create a y-axis to the right of the plot axis(side = 4, col = "red", las = 1)

slide-37
SLIDE 37

DATA VISUALIZATION IN R

Adding a smooth trend line

library(MASS) plot(Boston$rm, Boston$medv) trend <- supsmu(Boston$rm, Boston$medv) lines(trend, lwd = 2, col = "blue")

slide-38
SLIDE 38

Let's practice!

DATA VISU AL IZATION IN R