Base R plots

Author

Amanda Ng, Abigail Frix

Numerical Variable

In the previous sections, we focused on using tidyverse and ggplot functions to wrangle, summarize and visualize data. There are also some non-tidyverse functions that may come in handy. R comes built in with functions for plotting. Although they are not as popular as ggplot, they are still extremely useful, and in many cases, more customizeable.

When we were using tidyverse syntax, we took a logical, step-by-step approach to our coding through piping (%>%). In base R functions, we do not use piping. Instead, we specify the data frame to which the variables we are referring to. We do so by using the syntax df$var where df is the data frame name, and var is the variable name. If you do not include the data frame name first, your code won’t run. Let’s use base R functions to compute some summary statistics for pfas_data:

We can also create a stem plot for quantative variables using base R code.

We can also calculate the correlation with cor() between two numerical variables as follows:

Categorical Variable I

Recall the recreational use frequency classifications we added to the data frame cannabis_data in the previous section. We create an additional frequency classification on annual income as follow

cannabis_data <- cannabis_data %>%
  mutate(recreational_use_frequency = factor(use_freq_rec, 
                       levels = 1:7,
                       labels = c("< 1 day per month", "1 day per month", "2-3 days a month", "1-2 days a week", "3-4 days a week", "5-6 days a week", "Daily"))) %>% 
  mutate(annual_income = factor(income_recode, 
                         levels = 1:4,
                         labels = c("<$50k", "$50K to <$100K", 
                                    "$100K to <$150K", ">150K "))) 

Let’s create some summaries of the distribution of frequency classifications for these individuals!

Frequency table

A frequency table lists a set of values and how often each set of values appears. We use table() function to create such table. This frequency table looks at how many observations there are for each classification of recreational cannabis use frequency:

Proportion table

A proportion table, also known as a relative frequency table, is essentially a frequency table that shows the proportion of a categories frequency relative to the total number of observations. We wrap the frequency table with prop.table() to create a proportion table. For example:

Bar plots (using base R)

We can also create bar plots with base R function barplot() wrapping the frequency or proportion table.

Pie chart

Pie charts are categorical data visualizations where each “slice” represents a proportion of the whole. Note that ggplot has no pie chart option, so this is where base R comes into handy! They are created using the pie() function wrapping the frequency table.

However, pie charts have a lot of shortcomings and are often not the most effective visualizations, such as:

  • too many categories/“slices”;
  • multiple similar values between categories;
  • ineffective visual display of exact proportions.

In general, it is simply more effective to use bar charts given their versatility and increased informational capacity. Pie charts are only really an effective display when a part-to-whole comparison is meaningful, there are few categories, and the slices carve out identifiable proportions in the chart (such as multiples of 1/4, or 1/3).

How to customize base R plots

To customize these plots, use the help() function to obtain the R help documentation for the function you are using. An internet search for code which you can adapt to suit your needs and preferences is a good place to start too (just make sure you understand what the code is doing!)

Categorical Variable II

While we can create a two-way table using dplyr code, base R code is simply shorter. Consider the variables annual_income and recreational_use_frequency in cannabis_data dataset. We can produce two-way tables of counts and proportions and obtain joint distributions and conditional distributions.

Two-way table of counts

When we insert two variables into table(), the first variable will be displayed in rows and the second variable will be displayed in columns.

Two-way table of proportions or Joint Distribution

Similarly with prop.table(), the first variable will be displayed in rows and the second variable will be displayed in columns.

Two-way table of proportions or Conditional distribution given row variable

To obtain conditional distribution given the row variable, simply set margin = 1.

Two-way table of proportions or Conditional distribution given column variable

To obtain conditional distribution given the column variable, simply set margin = 2. Try coding it out yourself!

prop.table(table(cannabis_data$annual_income,
                 cannabis_data$recreational_use_frequency),
           margin=2)

Barplots and Mosaic plots

If we are interested in comparing annual income classifications versus cannabis usage frequency, we can use the second conditional distribution table above. Alternatively, we can also visualize this in a stacked barplot by wrapping the proportion table with barplot(). In this case, each bar corresponds to a category in the marginalized variable (shown on the x-axis), and the bars are further categorized by the other variable.

We can also create a mosaic plot for these variables using base R functions. Note that this function expects tables entered in the opposite order to the tables and bar plots above. The variable you would like the columns to represent in the mosaic plot should be entered as the rows (i.e., first variable in the table), and the variable you would like the rows to represent entered as the column (i.e., second variable in the table).

Return to Learning Hub Homepage

Learning Hub Homepage