Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

### Problem

Making graphics with base R is annoying for many reasons, but a big one is having to type the name of the data frame over and over again to reference different columns.

### Context

Back to our Mississippi River fish data. I’ve aggregated my sampling points into polygons, and now I want to explore some of their characteristics. To do that, I’d like to make some tables and plots, and because these are just quick, exploratory plots, I don’t feel like dealing with ggplot.

Load in the data (accessible on GitHub).

# Load data from GitHub

# Look at what we're dealing with
dim(polygon) # How big is the data set?
[1] 527  21
head(polygon, 3) # Look at the first few rows
poly_id propsnag n_points habitat_code
1 P04_CFL_13      0.8        5          CFL
2 P04_CFL_14      0.2        5          CFL
pool      Area Perimeter max_depth
1    4 105288.80  2067.890      1.30
2    4  42668.28  1770.465      0.74
avg_depth tot_vol shoreline_density_index
1 0.3625869   33955                1.797759
2 0.3291391    5953                2.417852
pct_aqveg pct_terr pct_prm_wetf
1  19.13396 93.87983     79.67522
2  41.25270 94.76871     42.44244
med_dist_to_land med_dist_to_forest
1         34.13379           34.13379
2         18.90166           32.64112
med_current wingdam revetment tributary
1        0.02       0         1         0
2        0.02       0         0         0
pct_shallow_area
1        0.9278354
2        1.0000000

First, I’d like to see how total volume tot_vol of the aquatic area scales with its Area.

In base R:

# Formula notation
plot(polygon$tot_vol ~ polygon$Area)
# OR:
# Comma notation
plot(polygon$Area, polygon$tot_vol)

Either way, we get this:

Or a more informative plot, with both variables on a log scale:

plot(log(polygon$tot_vol) ~ log(polygon$Area))

This isn’t too too clunky, but if the data frame name or column names are long, it can get a little annoying.

### Solution

The with() function allows you to specify the data frame your variables are coming from and then reference the variables with respect to the data frame, similar to the ggplot argument data =. Handy.

# Plot using the with() function
with(polygon, plot(tot_vol ~ Area))

You can add any other arguments inside of the function, as normal, it’s just now wrapped in with().

# Log-transform variables and make the points blue dots, because why not?
with(polygon, plot(log(tot_vol) ~ log(Area), # log-transform
pch = 20, # dots instead of circles
col = "blue", # make the dots blue
main = "Polygon volume by area, log-transformed") # title
)

It’s worth noting that this works for other functions besides plot(), too. Here’s an example with table(): let’s look at how many sampling polygons include revetment, broken down by navigation pool (area of the river). The data set contains three navigation pools: 4, 8, and 13.

# Two-way table by pool and revetment
with(polygon, table(revetment, pool))

### Outcome

Quick plots and data manipulation made even quicker!

### Resources

Discussion of when to use with():
https://stackoverflow.com/questions/42283479/when-to-use-with-function-and-why-is-it-good