I'm trying to make a plot of species sensitivity and response speed to discharge. I need the max lag position as a x value, and the correlation of that lag position as the y value.
I have this sample data...
structure(list(Date = structure(c(16222, 16617, 14518, 15156,
15918, 17075, 15522, 16679, 16010, 15187, 15461, 16283, 17379,
15553, 17410, 15553, 16191, 16314, 14549, 15979), class = "Date"),
Commonname = c("Black Sea Bass", "Pinfish", "Pigfish", "Pinfish",
"Silver Perch", "Black Sea Bass", "Pigfish", "Pinfish", "Pigfish",
"Silver Perch", "Silver Perch", "Black Sea Bass", "Pinfish",
"Pinfish", "Silver Perch", "Pigfish", "Black Sea Bass", "Silver Perch",
"Silver Perch", "Black Sea Bass"), CPUE = c(1.25513090974505,
9.41478783154444, 1.63667465565289, 3.13779141143018, 4.26313144106683,
2.32564938844104, 2.70394855189782, 8.49969670589948, 1.7329255861366,
2.0845409179642, 0.269832703723692, 1.21288437532366, 11.8739506505966,
8.55504246458105, 2.21256794002004, 4.51336797979511, 1.47695928524315,
1.10425042966867, 0.632732705722451, 1.59167844861806), Discharge = c(14.8521616,
5.23042759111111, 1.42663083211115, 0.184551018105263, 48.9156538971429,
2.29765846588235, 33.25524992, 4.06629248, 1.610659584, 0.21166808,
0.0607489749333333, 2.22029454545455, 12.90821328, 31.9696672,
8.05754544, 32.7267690105263, 43.493472128, 6.77337856, 1.10646621744,
4.37803470545454)), row.names = c(NA, -20L), class = c("tbl_df",
"tbl", "data.frame"))
I'm using this code to plot all the ccf functions...
df.split <- split(df, df$Commonname, drop = TRUE)
par(mfrow=c(3,4))
lapply(seq_along(df.split), function(x) ccf(df.split[[x]]$CPUE,df.split[[x]]$Discharge, lag.max = 5,
ylab = "", xlab = "", main= names(df.split)[x]))
But what is need is to extract those values for each species and get them into vectors so i can plot them on some sort of quantile plot.
My ideal output would be a plot in which the x axis would be from 0-5 or whatever I as lag.max
(in the code above its 5). The y axis would be from -1 to 1 (unless 0 to 1 with the absolute value of the correlation makes more sense).
The code above outputs the values in the console and plots the ccf function for all species included in the split, but I have ~130 species and it would be too tedious to fill in the values manually.
so it would look something like this... Where each species would have one point. Would be labeled with the species name. And as a bonus would have an * by the name is the lag correlation value (y
) was significant. Again the x value would be the lag with the highest correlation.
Dear @Johnny5ish I'll try to help you. However, at the beginning I have to admit that I do not know what the ccf
function is and and what does it return. Nevertheless, I am convinced that I will be able to show you how to approach this task.
Let's start with the data in the df
variable.
I divided everything into 4 simple steps. I will describe in turn what they give.
library(tidyverse)
fccf = function(data) ccf(data$CPUE,data$Discharge, lag.max = 5, plot = FALSE)
facf = function(acf) tibble(aacf = acf$acf[,,1], lag = acf$lag[,,1])
df %>% group_by(Commonname) %>%
nest() %>% #Step 1
mutate(ccf = map(data, ~fccf(.x))) %>% #Step 2
mutate(acf = map(ccf, ~facf(.x))) %>% #Step 3
unnest(acf) %>% #Step 4
ggplot(aes(lag, aacf, fill=Commonname, color=Commonname))+
geom_point()
Step 1.
I group the data of the variable Commonname
and fold it with the function nest
.
# A tibble: 4 x 2
# Groups: Commonname [4]
Commonname data
<chr> <list>
1 Black Sea Bass <tibble [5 x 3]>
2 Pinfish <tibble [5 x 3]>
3 Pigfish <tibble [4 x 3]>
4 Silver Perch <tibble [6 x 3]>
Step. 2
I am using a simple fccf
function which will return the results of the ccf
function to a variable with the same name.
# A tibble: 4 x 3
# Groups: Commonname [4]
Commonname data ccf
<chr> <list> <list>
1 Black Sea Bass <tibble [5 x 3]> <acf>
2 Pinfish <tibble [5 x 3]> <acf>
3 Pigfish <tibble [4 x 3]> <acf>
4 Silver Perch <tibble [6 x 3]> <acf>
Step 3
I use the facf
function which converts the output of the ccf
function to tibble
. Modify this function according to your needs.
# A tibble: 4 x 4
# Groups: Commonname [4]
Commonname data ccf acf
<chr> <list> <list> <list>
1 Black Sea Bass <tibble [5 x 3]> <acf> <tibble [9 x 2]>
2 Pinfish <tibble [5 x 3]> <acf> <tibble [9 x 2]>
3 Pigfish <tibble [4 x 3]> <acf> <tibble [7 x 2]>
4 Silver Perch <tibble [6 x 3]> <acf> <tibble [11 x 2]>
As you can see, we have a very neat tibble
here with all the intermediate steps of the calculation. In Step 4, I develop the results that interest me with the unnest
function.
# A tibble: 36 x 5
# Groups: Commonname [4]
Commonname data ccf aacf lag
<chr> <list> <list> <dbl> <dbl>
1 Black Sea Bass <tibble [5 x 3]> <acf> 0.0911 -4
2 Black Sea Bass <tibble [5 x 3]> <acf> -0.518 -3
3 Black Sea Bass <tibble [5 x 3]> <acf> 0.932 -2
4 Black Sea Bass <tibble [5 x 3]> <acf> -0.470 -1
5 Black Sea Bass <tibble [5 x 3]> <acf> -0.248 0
6 Black Sea Bass <tibble [5 x 3]> <acf> 0.213 1
7 Black Sea Bass <tibble [5 x 3]> <acf> 0.0109 2
8 Black Sea Bass <tibble [5 x 3]> <acf> -0.0110 3
9 Black Sea Bass <tibble [5 x 3]> <acf> 0.000854 4
10 Pinfish <tibble [5 x 3]> <acf> 0.146 -4
# ... with 26 more rows
And such data is easy to create on a nice plot using the ggplot2
package.
Here, I crossed out the acf
values in the lag
function with the breakdown into individual Commonname
groups.
Perhaps this is not the kind of graph you expected. However, I hope that, already knowing the correct and simple path, you will be able to adjust it to your needs. If you have any questions, write in the comment. Good luck!
Update 1 You can do like this
df %>% group_by(Commonname) %>%
nest() %>% #1
mutate(ccf = map(data, ~fccf(.x))) %>% #2
mutate(acf = map(ccf, ~facf(.x))) %>% #3
unnest(acf) %>% #4
ggplot(aes(lag, aacf, fill = Commonname, colour = Commonname))+
geom_point()+
geom_text(aes(label = Commonname),
size=3.5, alpha = 0.6, colour = "black",
hjust = 1, vjust = 1)
... geom_label(aes(label = Commonname),
size=3.5, alpha = 0.2, colour = "black",
hjust = 1, vjust = 1)
Or anything else. The ggplot2
package is amazing in this regard!