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.
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
Load in the data (accessible on GitHub).
# Load data from GitHub polygon <- read.csv("https://raw.githubusercontent.com/kaijagahm/general/master/polygon_sampling_data_UMR.csv") # Look at what we're dealing with dim(polygon) # How big is the data set?  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
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.
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
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
# 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
revetmentwith(polygon, table(revetment, pool))
Quick plots and data manipulation made even quicker!
Discussion of when to use