That is pretty standard
Hi,
I spent the entire day cracking my brain on some code, so I don’t have many words left, but hey. Let’s talk about data standardisation, because if you play with data more often than just during your monthly budget reviews, you’ll find this topic important.
I’m very monothematic, so I’ll use my birds as an example. Imagine you want to test a hypothesis that chicks that hatch later in the breeding season are less chunky than those that hatch early. You know that as the season goes on, there’s usually less food available, so it seems quite plausible.
You’ve got a big dataset spanning multiple years — that’s great! The issue is, the start of the season in, say, 2020 might be the middle of the season in 2024. Birds are smart beasts and time their breeding depending on the weather, so that’s a likely scenario 🐦.
How do you deal with that? By standardising your data and using standard deviations as units on the x-axis.
### Libraries ###
library(dplyr)
library(ggplot2)
### Creating a sample dataset ###
chick_data <- data.frame(
BroodID = 1:10,
Year = c(2020, 2020, 2020, 2021, 2021, 2021, 2021, 2022, 2022, 2022),
LayingDate = c(138, 125, 123, 120, 123, 111, 127, 121, 146, 113),
Mass = c(8.5, 10.1, 11.8, 13.0, 9.9, 13.2, 10.3, 11.7, 8.9, 9.8)
)
Now we can add the necessary columns by calculating the annual mean of the laying date and how much each row-specific laying date differs from it:
### Standardising the egg laying data - our proxy for hatching ###
chick_data <- chick_data %>%
group_by(Year) %>%
mutate(
Mean_LD = mean(LayingDate, na.rm = TRUE),
SD_LD = sd(LayingDate, na.rm = TRUE),
Standardised_LD = (LayingDate - Mean_LD) / SD_LD
) %>%
ungroup()
And finally, we can plot it:
### Plotting ###
ggplot(chick_data, aes(x = Standardised_LD, y = Mass)) +
geom_point(size = 2) +
labs(
x = "Standardized Laying Date",
y = "Chick Mass"
) +
theme_minimal()
Enjoy your evening,
Aga
PS: Here is the survey in which you can tell me what R topic you find particularly confusing and why you want to learn it, so that we can shape this space together!