R Tutorial: A preview of some more and less useful techniques

Four base R techniques for plotting two-variable relationships: scatter plots show trends, sunflower plots reveal duplicate points, box plots compare groups, and mosaic plots display categorical data. Learn when to use each and how high-level functions like plot() create the base while low-level functions add details.
Chapters
Base Graphics in R offers many tools for creating and modifying data visualizations, annotating them, and sharing them with others. Let's walk through a preview of four of these tools, each designed to show the relationship between two variables in a data set, but under different working assumptions about those variables. Here is a scatter plot between two numerical variables from the Boston data frame in the MAS package characterizing Boston area house prices in 1978. Each point represents a neighborhood, and the plot shows how the median house price MEDV varies with the average number of rooms rm. While there are some significant exceptions, for example the isolated points across the top, this plot shows that MEDV generally increases as RM increases. Here is a sunflower plot between the numerical variables tax and rad, also from the Boston data frame, which measure the tax rate and the index of accessibility to radial highways for each neighborhood. Sunflower plots are useful when numerical data points are repeated, that is, when the same pair of RAD and tax values occurs more than once. Simple scatter plots give no hint of these repeated values, but sunflower plots represent them as sunflowers. With one petal for each repeated data point, we can see that many neighborhoods with the largest RAD values all have the same large tax value. Here is a box plot summary showing how the range of the numerical crime variable the per capita crime rate for each neighborhood varies across the nine distinct values of rad. Typically, box plots are used to show how the distribution of a numerical variable changes over the discrete levels of a categorical variable, but as this example shows, boxplots can also be useful in displaying the relationship between a numerical Y variable that assumes many different values and a numerical X variable that assumes only a few values. Here is a mosaic plot summary designed to show relationships between two categorical variables, but as this example illustrates, it can also be used with numerical variables that exhibit only a few distinct values, each repeated several times. Specifically, this plot shows the relationship between the GEAR and the CYL variables from the mtcars data frame in the Datasets package. Given this variety of plot types that R makes available, it is important to select useful types to display our data. Despite their popularity, pie charts are generally a poor choice, as illustrated here and demonstrated further in one of the exercises. Basar graphics functions come in two high level functions like plot that create an initial plot, and low level functions like points, lines, and text that allow us to add details. Both types support many optional arguments to specify details like point shapes and sizes, line types and widths, and color, a topic covered in Chapter 5 for both low level and high level graphics functions that display points. The shapes of these points can be specified either by a number, as here, or a single alphanumeric character, as here. Finally, the par function allows us to set many useful graphics parameters globally so that they apply to all subsequent plots until we change them again with another par call. This example shows mf row, which allows us to easily set up multiple plot arrays. A topic covered in Chapter four Once again, it's your turn. The following exercises allow you to try these things for yourself.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







