Assign a final date for several variables

Hello,

My file is composed of several females on which we followed the maturation of the eggs by ultrasound. We therefore have several dates with the size of the eggs for 36 females.
Basically this is what the table looks like

Date Female Egg size
05/01/2020 a 0.2
05/01/2020 b 0.25
06/02/2020 a 0.3
06/02/2020 b 0.36
07/15/2020 a 0.52
07/17/2020 b 0.5

I would like to make a graph by assigning the egg-laying date for each female as day 0 and invert the dates according to the days of maturation.
Approximate example: 07/15 = D0, 06/02 = -D43 and 05/01 = -D74

I have this for 36 femelles with ramdom date

Thanks a lot

My graph is like that for the moment

Have a great day

Hi @Ben07,
Welcome to the RStudio Community Forum.

Here's a bit of dplyr followed by a bit of ggplot to get your graph:

suppressPackageStartupMessages(library(tidyverse))

df <- read.table(header=TRUE, text="
Date Female Egg_size
05/01/2020 a 0.2
05/01/2020 b 0.25
06/02/2020 a 0.3
06/02/2020 b 0.36
07/15/2020 a 0.52
07/17/2020 b 0.5")

df
#>         Date Female Egg_size
#> 1 05/01/2020      a     0.20
#> 2 05/01/2020      b     0.25
#> 3 06/02/2020      a     0.30
#> 4 06/02/2020      b     0.36
#> 5 07/15/2020      a     0.52
#> 6 07/17/2020      b     0.50

df %>%
  mutate(Date = as.Date(Date, format="%m/%d/%Y")) %>% 
  arrange(Female,Date) %>% 
  group_by(Female) %>% 
  mutate(egglay = max(Date)) %>% 
  mutate(days_to_egglay = Date - egglay) -> df

df
#> # A tibble: 6 x 5
#> # Groups:   Female [2]
#>   Date       Female Egg_size egglay     days_to_egglay
#>   <date>     <chr>     <dbl> <date>     <drtn>        
#> 1 2020-05-01 a          0.2  2020-07-15 -75 days      
#> 2 2020-06-02 a          0.3  2020-07-15 -43 days      
#> 3 2020-07-15 a          0.52 2020-07-15   0 days      
#> 4 2020-05-01 b          0.25 2020-07-17 -77 days      
#> 5 2020-06-02 b          0.36 2020-07-17 -45 days      
#> 6 2020-07-17 b          0.5  2020-07-17   0 days

ggplot(df) +
  aes(x=days_to_egglay, y=Egg_size, pch=Female, lty=Female, col=Female) +
  geom_point() +
  geom_line() +
  scale_x_continuous(breaks=c(-75, -44, 0))

Created on 2021-07-01 by the reprex package (v2.0.0)

OOOOhhhhh great ! Thank you a lot !

I apply your code to my data frame and that's work !

Have a great day and thank you again :slight_smile:

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.