How to plot: Connected BEFORE and AFTER hormone levels with lines?

52 views Asked by At

I have never posted on stack overflow (or any coding website) so I hope I can ask this well...

I am trying to make a plot showing how corticosterone (a hormone) increases in 30 minutes from baseline (base) to stress-induced (SI) levels in birds.

I captured starlings and took a baseline blood sample (Basecort), then waited 30 minutes and took a second blood sample (SIcort).

I would like to make a plot with each individual bird's Basecort and SIcort connected by lines. (I have been on Google for 2 hours (not an exaggeration) and can't make anything work).

I used the following code to make this plot:

create list of variables

x <- list('Base CORT' = df_adults$Base.cort, 'SI CORT' = df_adults$SI.cort)
x

create plot that contains one strip chart per variable

stripchart(x,
       main = 'Individual Changes in CORT',
       xlab = 'CORT Sample', 
       col = c('#9A8822', '#F5CDB4'),
       pch = 16,
       method = 'jitter',
       vertical = TRUE)

SEE PLOT HERE

I can't get any kind of "group" variable to work.

Does anyone have a clue how to connect the dots by BirdID?

This is what my dataframe looks like: Dataframe

Thank you SO MUCH to anyone who's able to help.

1

There are 1 answers

0
tjebo On

As per comments, this has been asked in other threads - to help you here a suggestion how to do this on your data (as a wiki, I will close this question thereafter).

Please do your readers (and reviewers!) a favour and plot your data as a scatter plot!

library(tidyr)
library(dplyr)
library(ggplot2)

## I've slightly modified the data from above suggested threads. 
df <- structure(list(BirdID = c("id_1", "id_2", "id_3", "id_4", "id_5", "id_6", "id_7", "id_8", "id_9", "id_10", "id_11", "id_12", "id_13", "id_14", "id_15", "id_16", "id_17", "id_18", "id_19", "id_20"), Basecort = c(9L, 7L, 2L, 2L, 1L, 5L, 6L, 7L, 5L, 9L, 5L, 2L, 9L, 4L, 6L, 10L, 4L, 10L, 7L, 9L), SIcort = c(4L, 1L, 7L, 3L, 5L, 10L, 10L, 9L, 5L, 9L, 5L, 10L, 1L, 3L, 10L, 6L, 4L, 9L, 6L, 8L)), row.names = c(NA, -20L), class = c("tbl_df", "tbl", "data.frame"))

df %>%
  pivot_longer(-BirdID, "type", "value") %>%
  mutate(value = jitter(value), 
         x = jitter(as.integer(factor(type)))) %>%
  ggplot(aes(x, value, group = BirdID)) +
  geom_point() +
  geom_line() +
  ## you will need to change the x axis labels
  scale_x_continuous(breaks = 1:2, labels = c("Basecort", "SIcort"))


## MUCH better
ggplot(df) +
  geom_point(aes(Basecort, SIcort)) +
  ## you can add a line of equality to make it even more intuitive 
  geom_abline(intercept = 0, slope = 1, lty = 2, linewidth = .2) +
  coord_equal()