R Tutorial: Data Visualization with lattice in R

Learn Lattice, an R graphics framework that balances speed for exploration with customization for polished reports. Using county-level cancer death rates, the tutorial covers histograms and scatter plots, showing how female death rates tend lower than male rates across counties.
Chapters
Welcome to the course. Data visualization is an essential tool for any data analyst. It's important for exploratory data analysis where you try to find and understand patterns in your data as quickly as possible. It's also important when presenting or reporting your results, but in that case, clarity and ease of customization are more important than speed. There are many R packages for data visualization, but most of them are based on one of three graphics frameworks. The first one is Base R Graphics, which has been available in R from the beginning and provides a rich collection of tools. However, it's not very flexible. Lattice Graphics is in some ways a successor to Base Graphics and tries to address many of its shortcomings. The third and newest framework is based on the ggplot2 package. All these frameworks are complete by themselves, but they are also important because of the large ecosystem of packages built around them. In this course, you will learn to use Lattice graphics for both exploration and presentation. In the first couple of chapters, you will use a data set that records death rates due to cancer at the US county level separately for males and females. The main variable of interest is the annual rate of death due to cancer. State is also available as a covariate. Your goal will be to explore how the distribution of death rate varies with gender and location. Here is a familiar plot. The Histogram Histograms show the distribution of a continuous variable, which in this case is the death rate among males. This histogram is not particularly exciting. Later we'll improve on it. Here's another familiar visualization, a scatter plot. This one shows the death rates among females versus males, where each circle represents the rates corresponding to one county. The plot suggests that the rates are correlated and that there are some possible outliers, but otherwise it's not remarkable. These two plots are produced by the lattice functions Histogram and xyplot. They are similar to Base R graphics functions HIST and Plot, respectively, but one important difference is in how the variables in the plot are specified. The lattice functions take a formula and a data frame as input. The left hand side of the formula names variables on the y axis and the right hand side names variables on the x axis. A histogram does not need the y axis to be specified, so the left hand side remains blank in that case. The main advantage of the formula interface is that it lets you compactly describe the plot without making all the variables in the data set visible in the workspace. This is similar to modeling functions such as lm, like in this call. To fit a linear regression model with red female as response and red male as predictor. The plots you just saw are very minimal. Such minimal plots are usually sufficient for exploration, but for presentation purposes, plots need to be more polished. For example, here's a more elaborate version of the previous scatter plot. It has more descriptive labels, a reference grid, and a reference line along the Y x diagonal. This makes one point very clear that was harder to see in the earlier scatter plot. In absolute terms, the death rate in females is substantially lower than in males for almost all counties throughout the course, you will learn how to customize your lattice plots in different ways. Now you go on to draw some histograms and scatter plots.
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







