First we need to tell R we want to use all the tidyverse packages.
library(tidyverse)
All our our data lives in a spreadsheet. To get that data into R, we need to export it as a .csv file either from Google Sheets or Excel. Save this file to wherever on your computer your .R file is saved. Then run the code below and it will pull up the file finder on your computer!
brachs <- read.csv("sample_lab3_data.csv")
Now our data is loaded, we want to play around with it. To make a new variable out of existing variables, we will use the mutate function. For example, if we wanted to make a variable that was the ratio of the width of the sulcus to the width of the whole specimen, it would look like this.
brachs<-brachs %>% mutate(ratioA=(Sulcus/Width))
Note that the %>% part is called a pipe - and it basically says “now do this”. So the code above says “take the data frame brachs, and then do this mutate function on it”. We could add another pipe after this if we wanted to do something else with this data too - you can link together many commands using pipes, which can be useful.
If you get an error that says ‘/’ not meaningful for factors worry not – what happened was that R interpreted the numbers as factors (like colors, shapes, or other non-numerical variables) so we have to tell it that these are actually numbers.
brachs$Width<-as.numeric(brachs$Width)
brachs$Sulcus<-as.numeric(brachs$Sulcus)
Do the same for the other variables, then go back and make your new variable column.
brachs<-brachs %>% mutate(ratioA=(Sulcus/Width))
brachs
## Sulcus Width ratioA
## 1 0.076863080 0.8968255 0.08570573
## 2 0.313457967 0.6166145 0.50835322
## 3 0.823685334 0.9801156 0.84039613
## 4 0.282848959 0.5125897 0.55180387
## 5 0.379509068 0.5048662 0.75170231
## 6 0.044154587 0.5516736 0.08003752
## 7 0.702683799 0.1427551 4.92230115
## 8 0.372852974 0.3971412 0.93884235
## 9 0.279235967 0.2154156 1.29626631
## 10 0.997054884 0.7769589 1.28327876
## 11 0.171538675 0.4330292 0.39613650
## 12 0.108299455 0.1463392 0.74005765
## 13 0.822422789 0.0968694 8.49001678
## 14 0.933884622 0.1763155 5.29666805
## 15 0.061325520 0.0383458 1.59927585
## 16 0.007874568 0.6055339 0.01300434
## 17 0.514179247 0.8964732 0.57355787
## 18 0.160336870 0.9180515 0.17464910
## 19 0.967695737 0.1527659 6.33450052
## 20 0.315403987 0.3090712 1.02048966
How do we know what our data means?!?! In this lab, we don’t have time to get into all the weeds of data analysis, but we can make a start. Head to https://r4ds.hadley.nz/EDA.html to read a brief primer on Exploratory Data Analysis.
While you’re welcome to plot your data in additional ways, today we’ll focus on going to learn how to make two kinds of plots using R - histograms and scatterplots. Both are pretty straightforward to make so I’m just going to point you to instructions. I recommend scrolling down to the examples first, then you can go back and learn more about the details if you want.
histograms https://ggplot2.tidyverse.org/reference/geom_histogram.html
and
scatter plots with linear regressions https://ggplot2.tidyverse.org/reference/geom_point.html and also https://ggplot2.tidyverse.org/reference/geom_smooth.html (note that in geom_smooth, ‘lm’ stands for linear model, i.e., a linear regression line through your points)
Feel free to use other online resources to help you make your plots but DO NOT use AI to write the code for you.
To download your figures go to the Files panel at right, then click ‘more’ and then ‘export’
In addition, it can be super helpful to name your plots! In tidyverse, the easiest way to do that is to add
+ggtitle(“My plot is so cool”)
at the end of your figure code. To learn how to make nicer axis labels and more, head here: https://ggplot2.tidyverse.org/reference/labs.html
What about just computing some basic statistics like means and standard deviations? R can help you here too, using a variant of the summarise function we saw in lab last week. For example, to find the means of all your columns, just ask. This code also includes a bit to make sure that R will exclude any NAs you have (not applicable, i.e., no data) and it will restrict the calculation just to numerical values.
brachs %>%
summarise(across(where(is.numeric), mean, na.rm = TRUE))
## Warning: There was 1 warning in `summarise()`.
## ℹ In argument: `across(where(is.numeric), mean, na.rm = TRUE)`.
## Caused by warning:
## ! The `...` argument of `across()` is deprecated as of dplyr 1.1.0.
## Supply arguments directly to `.fns` through an anonymous function instead.
##
## # Previously
## across(a:b, mean, na.rm = TRUE)
##
## # Now
## across(a:b, \(x) mean(x, na.rm = TRUE))
## Sulcus Width ratioA
## 1 0.4167654 0.4683875 1.794852
You can see the other statistics the summarise function can calculate for you here: https://dplyr.tidyverse.org/reference/summarise.html