data visualization in r - s3. · pdf filedata visualization in r using the points() function...
TRANSCRIPT
![Page 1: DATA VISUALIZATION IN R - s3. · PDF fileData Visualization in R Using the points() function to add points to a plot > library(MASS) > plot(UScereal$fibre) > index 20)](https://reader031.vdocuments.us/reader031/viewer/2022021509/5a9ad4607f8b9a9c5b8de244/html5/thumbnails/1.jpg)
DATA VISUALIZATION IN R
The plot() function and its options
![Page 2: DATA VISUALIZATION IN R - s3. · PDF fileData Visualization in R Using the points() function to add points to a plot > library(MASS) > plot(UScereal$fibre) > index 20)](https://reader031.vdocuments.us/reader031/viewer/2022021509/5a9ad4607f8b9a9c5b8de244/html5/thumbnails/2.jpg)
Data Visualization in R
Some options can or must be specified globally
> library(MASS) > par(mfrow = c(1, 2)) # mfrow specified globally > par(cex.main = 0.8) # cex.main specified either locally or globally > plot(whiteside$Temp, whiteside$Gas) > title("Gas vs. Temp scatterplot") > plot(whiteside$Insul) > title("Insul barplot")
![Page 3: DATA VISUALIZATION IN R - s3. · PDF fileData Visualization in R Using the points() function to add points to a plot > library(MASS) > plot(UScereal$fibre) > index 20)](https://reader031.vdocuments.us/reader031/viewer/2022021509/5a9ad4607f8b9a9c5b8de244/html5/thumbnails/3.jpg)
Data Visualization in R
> library(MASS) > indexA <- which(whiteside$Insul == "After") > indexB <- which(whiteside$Insul == "Before") > x <- whiteside$Temp > y <- whiteside$Gas > plot(x[indexA], y[indexA], type = "o", pch = 16, xlim = range(x), ylim = range(y), xlab = "Outside temperature", ylab = "Heating gas consumption") # high-level function > lines(x[indexB], y[indexB], type = "o", pch = 1) # low-level function > legend("topright", pch = c(1, 16), legend = c("Before insulation", "After insulation")) > title("A local specification: type = 'o'")
Other options can only be specified locally
type = "n"
![Page 4: DATA VISUALIZATION IN R - s3. · PDF fileData Visualization in R Using the points() function to add points to a plot > library(MASS) > plot(UScereal$fibre) > index 20)](https://reader031.vdocuments.us/reader031/viewer/2022021509/5a9ad4607f8b9a9c5b8de244/html5/thumbnails/4.jpg)
DATA VISUALIZATION IN R
Let’s practice!
![Page 5: DATA VISUALIZATION IN R - s3. · PDF fileData Visualization in R Using the points() function to add points to a plot > library(MASS) > plot(UScereal$fibre) > index 20)](https://reader031.vdocuments.us/reader031/viewer/2022021509/5a9ad4607f8b9a9c5b8de244/html5/thumbnails/5.jpg)
DATA VISUALIZATION IN R
Adding lines and points to plots
![Page 6: DATA VISUALIZATION IN R - s3. · PDF fileData Visualization in R Using the points() function to add points to a plot > library(MASS) > plot(UScereal$fibre) > index 20)](https://reader031.vdocuments.us/reader031/viewer/2022021509/5a9ad4607f8b9a9c5b8de244/html5/thumbnails/6.jpg)
Data Visualization in R
Using the points() function to add points to a plot
> library(MASS) > plot(UScereal$fibre) > index <- which(UScereal$fibre > 20) > points(index, UScereal$fibre[index], pch = 16, col = "red") > title("Using the points() function to highlight outliers")
![Page 7: DATA VISUALIZATION IN R - s3. · PDF fileData Visualization in R Using the points() function to add points to a plot > library(MASS) > plot(UScereal$fibre) > index 20)](https://reader031.vdocuments.us/reader031/viewer/2022021509/5a9ad4607f8b9a9c5b8de244/html5/thumbnails/7.jpg)
Data Visualization in R
A refresher
![Page 8: DATA VISUALIZATION IN R - s3. · PDF fileData Visualization in R Using the points() function to add points to a plot > library(MASS) > plot(UScereal$fibre) > index 20)](https://reader031.vdocuments.us/reader031/viewer/2022021509/5a9ad4607f8b9a9c5b8de244/html5/thumbnails/8.jpg)
Data Visualization in R
> library(MASS) > truehist(geyser$duration) > lines(density(geyser$duration), lwd = 2, col = "blue") > title("Old Faithful geyser duration data: \n Histogram with Overlaid Density Plot")
Using the lines() functon to add lines to a plot
![Page 9: DATA VISUALIZATION IN R - s3. · PDF fileData Visualization in R Using the points() function to add points to a plot > library(MASS) > plot(UScereal$fibre) > index 20)](https://reader031.vdocuments.us/reader031/viewer/2022021509/5a9ad4607f8b9a9c5b8de244/html5/thumbnails/9.jpg)
Data Visualization in R
> library(MASS) > plot(Cars93$Price, Cars93$Max.Price, pch = 17, xlab = "Average price", ylab = "Min & max prices") > points(Cars93$Price, Cars93$Min.Price, pch = 6) > abline(a = 0, b = 1, lty = 2, lwd = 2) > title("Min & Max Price vs. Average Price \n With an Equality Reference Line")
Using the abline() functon to add lines to a plot
line showing equality
![Page 10: DATA VISUALIZATION IN R - s3. · PDF fileData Visualization in R Using the points() function to add points to a plot > library(MASS) > plot(UScereal$fibre) > index 20)](https://reader031.vdocuments.us/reader031/viewer/2022021509/5a9ad4607f8b9a9c5b8de244/html5/thumbnails/10.jpg)
DATA VISUALIZATION IN R
Let’s practice!
![Page 11: DATA VISUALIZATION IN R - s3. · PDF fileData Visualization in R Using the points() function to add points to a plot > library(MASS) > plot(UScereal$fibre) > index 20)](https://reader031.vdocuments.us/reader031/viewer/2022021509/5a9ad4607f8b9a9c5b8de244/html5/thumbnails/11.jpg)
DATA VISUALIZATION IN R
Adding text to plots
![Page 12: DATA VISUALIZATION IN R - s3. · PDF fileData Visualization in R Using the points() function to add points to a plot > library(MASS) > plot(UScereal$fibre) > index 20)](https://reader031.vdocuments.us/reader031/viewer/2022021509/5a9ad4607f8b9a9c5b8de244/html5/thumbnails/12.jpg)
Course Title
Explanatory text● Axis labels
● Titles
● Legends
● Text in plot itself
xlab() ylab()
![Page 13: DATA VISUALIZATION IN R - s3. · PDF fileData Visualization in R Using the points() function to add points to a plot > library(MASS) > plot(UScereal$fibre) > index 20)](https://reader031.vdocuments.us/reader031/viewer/2022021509/5a9ad4607f8b9a9c5b8de244/html5/thumbnails/13.jpg)
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")
![Page 14: DATA VISUALIZATION IN R - s3. · PDF fileData Visualization in R Using the points() function to add points to a plot > library(MASS) > plot(UScereal$fibre) > index 20)](https://reader031.vdocuments.us/reader031/viewer/2022021509/5a9ad4607f8b9a9c5b8de244/html5/thumbnails/14.jpg)
Course Title
text(x, y, labels, adj)
![Page 15: DATA VISUALIZATION IN R - s3. · PDF fileData Visualization in R Using the points() function to add points to a plot > library(MASS) > plot(UScereal$fibre) > index 20)](https://reader031.vdocuments.us/reader031/viewer/2022021509/5a9ad4607f8b9a9c5b8de244/html5/thumbnails/15.jpg)
Data Visualization in R
Adding explanatory text to a plot> 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")
![Page 16: DATA VISUALIZATION IN R - s3. · PDF fileData Visualization in R Using the points() function to add points to a plot > library(MASS) > plot(UScereal$fibre) > index 20)](https://reader031.vdocuments.us/reader031/viewer/2022021509/5a9ad4607f8b9a9c5b8de244/html5/thumbnails/16.jpg)
Data Visualization in R
Varying fonts, orientations, and other text features
> library(MASS) > plot(Boston$rad) > text(350, 24, adj = 1, "Inner city? -->", srt = 30, font = 2, cex = 1.2, col = "red") > text(100, 15, "Suburbs? -->", srt = -45, font = 3, col = "green") > title("Text with varying orientations, fonts, sizes & colors")
![Page 17: DATA VISUALIZATION IN R - s3. · PDF fileData Visualization in R Using the points() function to add points to a plot > library(MASS) > plot(UScereal$fibre) > index 20)](https://reader031.vdocuments.us/reader031/viewer/2022021509/5a9ad4607f8b9a9c5b8de244/html5/thumbnails/17.jpg)
DATA VISUALIZATION IN R
Let’s practice!
![Page 18: DATA VISUALIZATION IN R - s3. · PDF fileData Visualization in R Using the points() function to add points to a plot > library(MASS) > plot(UScereal$fibre) > index 20)](https://reader031.vdocuments.us/reader031/viewer/2022021509/5a9ad4607f8b9a9c5b8de244/html5/thumbnails/18.jpg)
DATA VISUALIZATION IN R
Adding or modifying other plot details
![Page 19: DATA VISUALIZATION IN R - s3. · PDF fileData Visualization in R Using the points() function to add points to a plot > library(MASS) > plot(UScereal$fibre) > index 20)](https://reader031.vdocuments.us/reader031/viewer/2022021509/5a9ad4607f8b9a9c5b8de244/html5/thumbnails/19.jpg)
Data Visualization in R
Adding legends to a plot> library(MASS) > plot(Cars93$Price, Cars93$Max.Price, pch = 17, col = "red", xlab = "Average Price", ylab = "Min & Max Price") > points(Cars93$Price, Cars93$Min.Price, pch = 16, col = "green") > legend(x = "topleft", pch = c(16, 17), col = c("green", "red"), legend = c("Min Price", "Max Price")) > title("The legend() function adds boxed explanatory text")
![Page 20: DATA VISUALIZATION IN R - s3. · PDF fileData Visualization in R Using the points() function to add points to a plot > library(MASS) > plot(UScereal$fibre) > index 20)](https://reader031.vdocuments.us/reader031/viewer/2022021509/5a9ad4607f8b9a9c5b8de244/html5/thumbnails/20.jpg)
Data Visualization in R
Adding custom axes to a plot> library(MASS) > boxplot(MPG.city ~ Cylinders, data = Cars93, varwidth = TRUE, axes = FALSE) > axis(side = 3, at = Cars93$Cylinders, labels = as.character(Cars93$Cylinders), las = 2) > axis(side = 4, col = "red", las = 1)
![Page 21: DATA VISUALIZATION IN R - s3. · PDF fileData Visualization in R Using the points() function to add points to a plot > library(MASS) > plot(UScereal$fibre) > index 20)](https://reader031.vdocuments.us/reader031/viewer/2022021509/5a9ad4607f8b9a9c5b8de244/html5/thumbnails/21.jpg)
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")
![Page 22: DATA VISUALIZATION IN R - s3. · PDF fileData Visualization in R Using the points() function to add points to a plot > library(MASS) > plot(UScereal$fibre) > index 20)](https://reader031.vdocuments.us/reader031/viewer/2022021509/5a9ad4607f8b9a9c5b8de244/html5/thumbnails/22.jpg)
DATA VISUALIZATION IN R
Let’s practice!