Related
I have a data set of juvenile fish lengths collected in 1992 at different sites. I have also assigned each value a uniqueID (due to previous errors while using the pivot_wider function). The data is as follows:
df<-structure(list(year = c(92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L,
92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L, 92L
), site = structure(c(5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L,
4L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 13L, 13L, 13L, 13L, 13L, 13L,
13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 15L, 18L, 18L, 18L, 18L,
18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L,
18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L,
18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L,
18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L,
18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L,
18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L,
18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L,
18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L,
18L, 18L, 18L, 18L, 18L, 19L, 20L, 20L, 20L, 20L, 20L, 20L, 20L,
20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L,
20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L,
20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L,
20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L,
20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L,
20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 21L, 21L,
21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L,
21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L,
21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L,
21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L,
21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L,
21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L,
21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L,
21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L,
21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L,
21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L,
21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L,
21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L,
21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 22L, 22L, 22L,
22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L,
22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L,
22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 23L, 23L, 23L, 23L, 23L,
23L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 23L,
23L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 23L,
23L, 23L, 23L, 23L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L,
24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L,
24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L,
24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 25L, 25L, 25L, 25L, 25L,
25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L,
25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L,
25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L,
26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L,
26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L,
26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L,
26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L,
26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L,
26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L,
26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 27L, 28L, 28L,
28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L,
28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L,
28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L,
28L, 28L, 28L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L,
29L, 29L, 30L, 30L, 30L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L,
31L, 31L, 31L, 32L, 32L, 32L, 32L, 33L, 33L, 33L, 33L, 33L, 33L,
33L, 33L, 33L, 34L, 35L, 35L, 35L, 35L, 35L, 35L, 35L, 35L, 35L,
35L, 35L, 35L, 35L, 35L, 35L, 36L, 36L, 36L, 36L, 36L, 37L, 37L,
37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L,
37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L,
37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L, 38L, 38L,
38L, 38L, 38L, 38L, 38L, 39L, 39L, 39L, 39L, 40L, 40L, 40L, 41L,
41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L,
41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L,
41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L,
41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L,
41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L,
41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L,
41L, 41L, 41L, 41L, 41L, 41L, 41L, 41L, 42L, 42L, 42L, 42L, 42L,
42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L,
42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L,
42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L,
42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L,
42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L,
42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L,
42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L,
42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L,
42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L,
42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L,
42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L,
42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L,
42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L,
42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L,
42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L,
42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L,
42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L,
42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L,
42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L,
42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L, 42L,
42L, 42L, 42L, 42L, 43L, 43L, 43L, 43L, 43L, 44L, 44L, 44L, 44L,
44L, 44L, 44L, 44L, 44L, 44L, 44L, 44L, 44L, 44L, 44L, 44L, 44L,
44L, 44L, 44L, 44L, 44L, 44L, 44L, 44L, 44L, 44L, 44L, 44L, 44L,
44L, 44L, 44L, 44L, 44L, 44L, 44L, 44L, 44L, 44L, 44L, 44L, 44L,
44L, 44L, 44L, 44L, 44L, 44L, 44L, 44L, 44L, 44L, 44L, 44L, 44L,
44L, 44L, 44L, 44L, 44L, 44L, 44L), .Label = c("1", "2", "3",
"5", "10", "16", "17", "18", "19", "20", "26", "27", "28", "29",
"30", "32", "33", "34", "35", "40", "41", "46", "50", "51", "52",
"53", "57", "58", "65", "67", "68", "69", "70", "71", "72", "75",
"76", "77", "78", "79", "80", "81", "84", "85"), class = "factor"),
length = c(64L, 71L, 70L, 64L, 53L, 55L, 53L, 61L, 74L, 62L,
66L, 65L, 57L, 66L, 71L, 65L, 74L, 68L, 70L, 67L, 73L, 67L,
71L, 45L, 50L, 60L, 70L, 96L, 133L, 72L, 127L, 69L, 66L,
68L, 142L, 52L, 68L, 62L, 67L, 65L, 147L, 167L, 157L, 145L,
136L, 128L, 146L, 144L, 129L, 121L, 130L, 129L, 200L, 124L,
101L, 86L, 83L, 90L, 59L, 61L, 88L, 57L, 56L, 59L, 67L, 58L,
47L, 59L, 43L, 89L, 65L, 71L, 71L, 69L, 62L, 68L, 65L, 60L,
61L, 63L, 62L, 61L, 43L, 64L, 67L, 85L, 35L, 176L, 197L,
66L, 46L, 44L, 43L, 42L, 45L, 45L, 178L, 77L, 40L, 66L, 42L,
62L, 47L, 71L, 42L, 74L, 60L, 58L, 45L, 49L, 50L, 65L, 46L,
60L, 48L, 36L, 40L, 46L, 38L, 46L, 43L, 37L, 46L, 51L, 57L,
69L, 69L, 41L, 44L, 42L, 63L, 47L, 44L, 43L, 47L, 49L, 37L,
47L, 46L, 40L, 44L, 45L, 38L, 41L, 47L, 47L, 54L, 37L, 43L,
47L, 42L, 39L, 39L, 44L, 39L, 45L, 47L, 38L, 42L, 65L, 41L,
44L, 38L, 42L, 42L, 42L, 42L, 42L, 42L, 45L, 36L, 43L, 48L,
44L, 40L, 44L, 44L, 41L, 39L, 40L, 36L, 42L, 41L, 43L, 38L,
39L, 44L, 40L, 47L, 43L, 50L, 44L, 45L, 36L, 46L, 43L, 46L,
38L, 39L, 49L, 40L, 232L, 222L, 131L, 154L, 151L, 75L, 44L,
40L, 44L, 44L, 43L, 46L, 223L, 198L, 167L, 146L, 84L, 45L,
50L, 42L, 40L, 47L, 40L, 45L, 55L, 40L, 51L, 42L, 46L, 43L,
46L, 40L, 42L, 45L, 50L, 40L, 44L, 48L, 44L, 42L, 56L, 43L,
46L, 39L, 40L, 46L, 41L, 40L, 37L, 49L, 46L, 40L, 43L, 42L,
43L, 46L, 40L, 42L, 45L, 50L, 40L, 44L, 48L, 44L, 42L, 56L,
43L, 46L, 39L, 40L, 46L, 41L, 40L, 37L, 49L, 46L, 40L, 43L,
42L, 45L, 44L, 40L, 44L, 34L, 220L, 184L, 155L, 152L, 167L,
163L, 157L, 138L, 130L, 98L, 98L, 137L, 92L, 85L, 82L, 89L,
85L, 216L, 220L, 205L, 143L, 147L, 150L, 152L, 163L, 132L,
156L, 157L, 147L, 145L, 132L, 91L, 90L, 81L, 86L, 89L, 84L,
84L, 134L, 45L, 205L, 157L, 163L, 166L, 134L, 140L, 87L,
66L, 95L, 85L, 89L, 90L, 96L, 91L, 87L, 83L, 91L, 80L, 88L,
88L, 82L, 60L, 43L, 37L, 43L, 176L, 182L, 142L, 146L, 147L,
92L, 97L, 140L, 100L, 94L, 91L, 90L, 87L, 92L, 92L, 86L,
84L, 89L, 51L, 48L, 49L, 42L, 90L, 145L, 87L, 93L, 135L,
92L, 90L, 83L, 87L, 86L, 89L, 79L, 95L, 91L, 96L, 93L, 84L,
47L, 45L, 37L, 89L, 102L, 89L, 84L, 163L, 131L, 94L, 95L,
93L, 80L, 87L, 75L, 46L, 50L, 82L, 96L, 68L, 46L, 41L, 51L,
36L, 48L, 94L, 87L, 82L, 66L, 95L, 82L, 67L, 46L, 40L, 64L,
46L, 41L, 45L, 41L, 232L, 150L, 173L, 155L, 152L, 155L, 96L,
141L, 132L, 93L, 48L, 90L, 174L, 152L, 152L, 133L, 158L,
139L, 93L, 90L, 77L, 73L, 155L, 97L, 75L, 77L, 78L, 73L,
79L, 67L, 65L, 74L, 46L, 79L, 75L, 88L, 75L, 90L, 47L, 51L,
49L, 81L, 78L, 71L, 83L, 74L, 82L, 85L, 80L, 77L, 81L, 77L,
80L, 68L, 80L, 91L, 82L, 75L, 84L, 79L, 42L, 83L, 90L, 89L,
88L, 85L, 95L, 87L, 92L, 83L, 90L, 88L, 85L, 78L, 79L, 88L,
76L, 84L, 87L, 79L, 82L, 90L, 67L, 83L, 80L, 84L, 88L, 84L,
90L, 86L, 85L, 79L, 81L, 80L, 82L, 84L, 77L, 92L, 88L, 90L,
88L, 81L, 91L, 87L, 88L, 80L, 90L, 91L, 87L, 84L, 87L, 82L,
76L, 85L, 75L, 73L, 87L, 89L, 77L, 96L, 82L, 82L, 89L, 86L,
84L, 84L, 92L, 91L, 86L, 87L, 93L, 77L, 83L, 82L, 93L, 87L,
86L, 76L, 82L, 68L, 91L, 92L, 76L, 94L, 88L, 86L, 98L, 91L,
84L, 83L, 100L, 95L, 79L, 98L, 89L, 88L, 79L, 84L, 93L, 87L,
103L, 92L, 85L, 94L, 83L, 97L, 96L, 83L, 91L, 73L, 84L, 87L,
96L, 79L, 96L, 69L, 85L, 95L, 91L, 89L, 86L, 77L, 80L, 54L,
153L, 90L, 80L, 139L, 94L, 89L, 91L, 98L, 95L, 87L, 91L,
86L, 94L, 86L, 92L, 92L, 98L, 82L, 85L, 87L, 87L, 87L, 84L,
86L, 85L, 96L, 83L, 83L, 139L, 97L, 80L, 85L, 86L, 150L,
89L, 78L, 85L, 80L, 93L, 95L, 93L, 92L, 79L, 168L, 96L, 98L,
87L, 91L, 103L, 86L, 93L, 90L, 80L, 84L, 84L, 41L, 152L,
85L, 91L, 89L, 83L, 87L, 87L, 89L, 84L, 64L, 89L, 83L, 90L,
84L, 90L, 94L, 87L, 78L, 83L, 99L, 95L, 90L, 89L, 96L, 96L,
92L, 99L, 82L, 80L, 84L, 94L, 74L, 70L, 111L, 129L, 141L,
135L, 88L, 78L, 64L, 68L, 58L, 67L, 50L, 57L, 50L, 170L,
139L, 172L, 142L, 121L, 121L, 144L, 155L, 136L, 131L, 125L,
127L, 133L, 124L, 73L, 83L, 44L, 52L, 44L, 39L, 43L, 48L,
47L, 52L, 46L, 47L, 50L, 172L, 133L, 93L, 128L, 244L, 252L,
150L, 162L, 150L, 106L, 95L, 98L, 84L, 108L, 48L, 47L, 84L,
136L, 49L, 47L, 131L, 73L, 70L, 54L, 59L, 52L, 55L, 48L,
55L, 45L, 222L, 238L, 40L, 40L, 43L, 45L, 67L, 47L, 50L,
41L, 61L, 54L, 43L, 42L, 89L, 43L, 42L, 48L, 64L, 55L, 40L,
52L, 46L, 49L, 46L, 49L, 45L, 45L, 42L, 176L, 154L, 97L,
83L, 97L, 132L, 88L, 95L, 98L, 82L, 81L, 92L, 96L, 89L, 63L,
90L, 93L, 82L, 97L, 93L, 100L, 85L, 78L, 85L, 72L, 73L, 62L,
62L, 60L, 58L, 128L, 98L, 87L, 96L, 56L, 86L, 87L, 78L, 58L,
73L, 55L, 54L, 62L, 46L, 85L, 193L, 140L, 97L, 95L, 87L,
40L, 208L, 210L, 146L, 46L, 206L, 40L, 262L, 145L, 52L, 72L,
49L, 85L, 70L, 47L, 93L, 69L, 65L, 54L, 60L, 46L, 44L, 48L,
56L, 58L, 49L, 42L, 52L, 42L, 50L, 51L, 50L, 53L, 42L, 48L,
49L, 45L, 45L, 49L, 42L, 47L, 60L, 51L, 53L, 46L, 49L, 46L,
56L, 57L, 57L, 48L, 52L, 49L, 50L, 46L, 56L, 62L, 51L, 46L,
50L, 46L, 49L, 50L, 51L, 52L, 46L, 51L, 48L, 42L, 48L, 42L,
47L, 43L, 55L, 50L, 44L, 46L, 52L, 46L, 44L, 45L, 53L, 53L,
56L, 57L, 45L, 42L, 41L, 55L, 50L, 51L, 50L, 47L, 51L, 45L,
280L, 192L, 185L, 150L, 183L, 150L, 189L, 211L, 159L, 134L,
134L, 138L, 132L, 136L, 145L, 125L, 147L, 149L, 161L, 126L,
44L, 142L, 148L, 138L, 154L, 135L, 150L, 134L, 152L, 128L,
233L, 188L, 155L, 137L, 121L, 148L, 138L, 240L, 214L, 163L,
143L, 149L, 127L, 115L, 156L, 124L, 132L, 166L, 159L, 152L,
95L, 171L, 154L, 142L, 170L, 155L, 140L, 163L, 123L, 139L,
111L, 148L, 142L, 149L, 137L, 154L, 183L, 136L, 207L, 138L,
155L, 160L, 148L, 150L, 141L, 137L, 210L, 198L, 149L, 212L,
240L, 222L, 171L, 165L, 187L, 177L, 167L, 139L, 137L, 103L,
122L, 127L, 149L, 154L, 156L, 137L, 145L, 174L, 144L, 127L,
124L, 96L, 168L, 163L, 136L, 136L, 124L, 128L, 192L, 169L,
192L, 163L, 177L, 133L, 155L, 169L, 135L, 158L, 145L, 154L,
132L, 152L, 136L, 126L, 116L, 183L, 157L, 155L, 141L, 145L,
203L, 160L, 146L, 152L, 182L, 157L, 149L, 164L, 142L, 160L,
183L, 127L, 150L, 123L, 128L, 154L, 126L, 127L, 133L, 105L,
135L, 117L, 131L, 150L, 121L, 258L, 278L, 243L, 241L, 222L,
110L, 155L, 149L, 159L, 155L, 159L, 152L, 134L, 133L, 198L,
164L, 138L, 127L, 156L, 141L, 129L, 135L, 153L, 148L, 136L,
133L, 158L, 137L, 133L, 132L, 117L, 134L, 150L, 145L, 145L,
135L, 150L, 157L, 145L, 136L, 127L, 133L, 121L, 143L, 134L,
255L, 126L, 137L, 201L, 182L, 150L, 118L, 157L, 160L, 143L,
150L, 142L, 194L, 186L, 156L, 151L, 177L, 155L, 128L, 128L,
145L, 133L, 130L, 121L, 125L, 135L, 147L, 121L, 135L, 167L,
155L, 148L, 144L, 137L, 135L, 150L, 162L, 151L, 156L, 151L,
160L, 166L, 150L, 122L, 146L, 152L, 162L, 162L, 122L, 144L,
147L, 145L, 142L, 150L, 145L, 121L, 137L, 117L, 140L, 142L,
134L, 140L, 134L, 131L, 136L, 116L, 135L, 127L, 129L, 185L,
46L, 220L, 142L, 152L, 127L, 45L, 47L, 45L, 54L, 51L, 56L,
49L, 49L, 58L, 49L, 51L, 45L, 47L, 44L, 69L, 57L, 48L, 52L,
60L, 40L, 51L, 46L, 43L, 49L, 43L, 47L, 45L, 56L, 46L, 48L,
46L, 49L, 48L, 50L, 66L, 49L, 59L, 47L, 59L, 50L, 43L, 53L,
48L, 56L, 44L, 52L, 42L, 51L, 44L, 51L, 53L, 40L, 50L, 50L,
34L, 48L, 51L, 51L, 46L, 47L, 53L, 32L), uniqueID = 1:1282), row.names = c(NA,
-1282L), class = "data.frame")
I am trying to transform this data from long to wide format. I have been trying to do this by using:
df1<- df %>% group_by(length) %>% pivot_wider(names_from=site, values_from=length) %>% select(-uniqueID)
It gives me an output, but it has a lot of NA's. How do I make it look like table 1 and not table 2? Thanks in advance!
You probably need an ID for each combination of year and site. Please test the following to see if this is what you need.
library(tidyverse)
df1 <- df %>%
select(-uniqueID) %>%
group_by(year, site) %>%
mutate(ID = 1:n()) %>%
pivot_wider(names_from=site, values_from=length)
entrezid_downgene=structure(list(SYMBOL = c("ARHGEF16", "ILDR1", "TMPRSS4", "MAP7", "SERINC2", "C9orf152", "TSPAN1", "RHEX", "TMC4", "CRB3", "UGT8", "CD24", "MAPK13", "AGR2", "GJB1", "ERBB3", "CNDP2", "LOC105378644", "GCNT3", "CEACAM1", "GPR160", "PRSS8", "HOOK1", "ABHD17C", "MOCOS", "CWH43", "EHF", "ACSL5", "SLC44A4", "RAP1GAP", "MUC13", "PPM1H", "ATP2C2", "RAB25", "H2BC5", "H4C12", "TJP3", "RXFP1", "GSTO2", "OVOL2", "TMEM125", "LIMS1", "DLX5", "ST6GALNAC1", "HNF1B", "STX19", "F2RL1", "MT1G", "PLPP2", "TMEM238", "SLC30A2", "GABRP", "EPCAM", "CLDN10", "HOXB5", "PRAME", "MAL2", "PLA2G10", "TSPAN12", "FAM174B", "TMC5", "ASRGL1", "SCNN1A", "FOXL2", "ALDH3B2", "ELF3", "SLC7A1", "MT1F", "CLDN3", "SPINT2", "SFN", "VWC2", "C9orf116", "SLC39A6", "TCN1", "IL20RA", "ACSM3", "FOXL2NB", "HGD", "PAX8", "IDO1", "C4BPA", "RHPN2", "HMGCR", "UGT2B11", "PIGR", "MUC20", "SLC3A1", "PLLP", "PSAT1", "SCGB2A1", "WNT5A", "DEFB1", "FGL1", "SLC2A8", "HOXB8", "CYP2J2", "WWC1", "MUC1", "PRKX", "RASEF", "BAIAP2L2", "PAPSS1", "MME", "HOMER2", "STRA6", "ARG2", "MOGAT1", "CDS1", "SCGB2A2", "MPZL2", "PHYHIPL", "INAVA", "IDO2", "GALNT4", "TMEM101", "HSD17B2", "AOC1", "CDCA7", "CAPS", "TFCP2L1", "PAEP", "PLAC9P1", "GAL", "RORB", "CCNO", "XDH", "C15orf48", "SLC1A1", "GPT2", "VNN1", "NWD1", "HABP2", "UGT2B7", "CYP26A1", "MSX1", "ENPP3", "KIR2DL3", "ADAMTS9", "KIR2DL4", "BRINP1", "PROM1", "APCDD1", "AGR3", "EYA2", "SLC2A1", "GNLY", "COL7A1", "FOXJ1", "MS4A8", "C20orf85", "RSPH1", "SCGB1D2", "SPP1", "RASD1", "CST1", "SCGB1D4", "LEFTY1", "LAMC3", "TEKT1", "LCN2", "VTCN1", "IRX3", "ROPN1L", "FAM183A", "NDP", "TUBB3", "DIO2", "IL2RB", "ADAMTS8", "SERPINA5", "NKG7", "ABCC8", "STC1", "LRRC26"),
ENTREZID = c("27237", "286676", "56649", "9053", "347735", "401546", "10103", "440712", "147798", "92359", "7368", "100133941", "5603", "10551", "2705", "2065", "55748", "105378644", "9245", "634", "26996", "5652", "51361", "58489", "55034", "80157", "26298", "51703", "80736", "5909", "56667", "57460", "9914", "57111", "3017", "8362", "27134", "59350", "119391", "58495", "128218", "3987", "1749", "55808", "6928", "415117", "2150", "4495", "8612", "388564", "7780", "2568", "4072", "9071", "3215", "23532", "114569", "8399", "23554", "400451", "79838", "80150", "6337", "668", "222", "1999", "6541", "4494", "1365", "10653", "2810", "375567", "138162", "25800", "6947", "53832", "6296", "401089", "3081", "7849", "3620", "722", "85415", "3156", "10720", "5284", "200958", "6519", "51090", "29968", "4246", "7474", "1672", "2267", "29988", "3218", "1573", "23286", "4582", "5613", "158158", "80115", "9061", "4311", "9455", "64220", "384", "116255", "1040", "4250", "10205", "84457", "55765", "169355", "8693", "84336", "3294", "26", "83879", "828", "29842", "5047", "389033", "51083", "6096", "10309", "7498", "84419", "6505", "84706", "8876", "284434", "3026", "7364", "1592", "4487", "5169", "3804", "56999", "3805", "1620", "8842", "147495", "155465", "2139", "6513", "10578", "1294", "2302", "83661", "128602", "89765", "10647", "6696", "51655", "1469", "404552", "10637", "10319", "83659", "3934", "79679", "79191", "83853", "440585", "4693", "10381", "1734", "3560", "11095", "5104", "4818", "6833", "6781", "389816")),
row.names = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 50L, 51L, 52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L, 60L, 61L, 62L, 63L, 64L, 65L, 66L, 67L, 68L, 69L, 70L, 71L, 72L, 73L, 74L, 75L, 76L, 77L, 78L, 79L, 80L, 81L, 82L, 83L, 84L, 85L, 86L, 87L, 88L, 89L, 90L, 91L, 92L, 93L, 94L, 95L, 96L, 97L, 98L, 99L, 100L, 101L, 102L, 103L, 104L, 105L, 106L, 107L, 108L, 109L, 110L, 111L, 112L, 113L, 114L, 115L, 116L, 117L, 118L, 119L, 120L, 121L, 123L, 124L, 125L, 126L, 127L, 128L, 129L, 130L, 131L, 132L, 133L, 134L, 135L, 136L, 137L, 138L, 139L, 140L, 141L, 142L, 143L, 144L, 145L, 146L, 147L, 148L, 149L, 150L, 151L, 152L, 153L, 154L, 155L, 156L, 157L, 158L, 159L, 160L, 161L, 162L, 163L, 164L, 165L, 166L, 167L, 168L, 169L, 170L, 171L, 172L, 173L, 174L, 175L, 176L, 177L), class = "data.frame")
down_ekk <- enrichKEGG(gene= c(entrezid_downgene$ENTREZID),
organism = 'hsa',
pvalueCutoff = 0.05,
minGSSize = 50,
maxGSSize = 500,
)
dot <- dotplot(down_ekk,font.size=6,title='down_kegg')
dot
Error in ans[ypos] <- rep(yes, length.out = len)[ypos] : Change the
parameter length to zero Warning message: In rep(yes, length.out =
len) : 'x' is NULL so the result will be NULL
please How to solve the error?
This is normal you can't plot the dotplot because you have no significant ontologies.
You can check with down_ekk :
downekk
#
# over-representation test
#
#...#organism hsa
#...#ontology KEGG
#...#keytype kegg
#...#gene chr [1:175] "27237" "286676" "56649" "9053" "347735" "401546" "10103" "440712" "147798" "92359" "7368" "100133941" "5603" "10551" "2705" "2065" "55748" "105378644" "9245" "634" "26996" "5652" ...
#...pvalues adjusted by 'BH' with cutoff <0.05
#...0 enriched terms found
#...Citation
Guangchuang Yu, Li-Gen Wang, Yanyan Han and Qing-Yu He.
clusterProfiler: an R package for comparing biological themes among
gene clusters. OMICS: A Journal of Integrative Biology
2012, 16(5):284-287
"0 enriched terms found" so this is why you get the error as no dotplot can be plotted
I have irregular polygons defined by a set of points. I can find the the maximum chord location and length, but I'm not quite sure how to analyze the points to find the longest chord location and length that is perpendicular to the maximum chord.
Here's what I've got so far, some example data of points to define a polygon:
points_ex <- structure(list(V1 = c(68L, 67L, 66L, 66L, 65L, 65L, 64L, 63L,
62L, 61L, 61L, 60L, 59L, 58L, 57L, 56L, 56L, 55L, 55L, 55L, 55L,
54L, 54L, 54L, 54L, 54L, 54L, 54L, 53L, 53L, 53L, 52L, 52L, 52L,
51L, 51L, 50L, 50L, 49L, 49L, 49L, 48L, 48L, 47L, 47L, 46L, 46L,
45L, 45L, 45L, 44L, 44L, 44L, 44L, 44L, 44L, 44L, 43L, 43L, 42L,
42L, 41L, 41L, 41L, 41L, 40L, 40L, 40L, 40L, 40L, 40L, 39L, 39L,
38L, 38L, 38L, 38L, 37L, 37L, 36L, 36L, 36L, 35L, 35L, 35L, 35L,
35L, 34L, 34L, 34L, 33L, 33L, 33L, 32L, 32L, 32L, 32L, 32L, 32L,
32L, 32L, 32L, 32L, 32L, 32L, 32L, 32L, 32L, 32L, 32L, 32L, 32L,
32L, 32L, 32L, 32L, 32L, 32L, 32L, 32L, 32L, 32L, 32L, 33L, 33L,
33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L, 33L,
33L, 33L, 32L, 32L, 32L, 32L, 32L, 32L, 32L, 31L, 31L, 31L, 31L,
31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L,
30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L,
30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 31L, 31L,
31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L,
31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 32L, 32L, 33L, 33L,
34L, 34L, 35L, 36L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 43L,
44L, 44L, 45L, 46L, 46L, 47L, 47L, 48L, 48L, 49L, 49L, 50L, 51L,
51L, 51L, 52L, 52L, 52L, 52L, 53L, 53L, 53L, 53L, 54L, 54L, 54L,
54L, 54L, 54L, 54L, 53L, 53L, 53L, 53L, 53L, 53L, 52L, 52L, 52L,
51L, 51L, 51L, 51L, 51L, 51L, 51L, 51L, 52L, 52L, 52L, 52L, 52L,
53L, 53L, 54L, 54L, 55L, 55L, 56L, 57L, 58L, 59L, 60L, 61L, 62L,
63L, 64L, 65L, 66L, 67L, 68L, 69L, 70L, 71L, 72L, 73L, 74L, 75L,
76L, 77L, 78L, 79L, 80L, 81L, 82L, 83L, 84L, 84L, 85L, 85L, 86L,
86L, 86L, 87L, 87L, 88L, 88L, 88L, 89L, 89L, 90L, 90L, 91L, 91L,
92L, 93L, 94L, 95L, 96L, 97L, 98L, 99L, 100L, 101L, 102L, 103L,
104L, 105L, 105L, 106L, 106L, 107L, 108L, 109L, 109L, 110L, 111L,
112L, 113L, 114L, 115L, 116L, 117L, 118L, 118L, 119L, 120L, 121L,
122L, 123L, 123L, 124L, 125L, 126L, 126L, 127L, 127L, 127L, 127L,
127L, 128L, 128L, 128L, 128L, 128L, 128L, 128L, 128L, 128L, 129L,
129L, 129L, 129L, 129L, 129L, 130L, 130L, 130L, 130L, 131L, 131L,
131L, 131L, 131L, 132L, 132L, 132L, 132L, 132L, 133L, 133L, 133L,
133L, 133L, 133L, 133L, 132L, 132L, 132L, 131L, 131L, 130L, 130L,
129L, 129L, 128L, 128L, 128L, 128L, 128L, 128L, 128L, 128L, 128L,
129L, 129L, 129L, 129L, 129L, 129L, 129L, 129L, 129L, 129L, 128L,
127L, 126L, 125L, 125L, 124L, 123L, 123L, 122L, 121L, 120L, 120L,
119L, 119L, 118L, 117L, 117L, 116L, 115L, 115L, 115L, 114L, 114L,
113L, 113L, 112L, 111L, 111L, 110L, 110L, 109L, 109L, 109L, 109L,
108L, 108L, 108L, 108L, 107L, 107L, 107L, 107L, 106L, 106L, 106L,
106L, 105L, 105L, 105L, 105L, 104L, 104L, 104L, 104L, 103L, 103L,
103L, 103L, 103L, 102L, 102L, 102L, 102L, 102L, 102L, 102L, 102L,
102L, 102L, 102L, 102L, 101L, 101L, 101L, 101L, 101L, 101L, 101L,
100L, 100L, 100L, 100L, 100L, 100L, 100L, 99L, 99L, 99L, 99L,
99L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 97L, 97L,
97L, 97L, 97L, 97L, 96L, 96L, 96L, 96L, 96L, 96L, 96L, 96L, 96L,
96L, 95L, 95L, 95L, 95L, 95L, 95L, 95L, 95L, 95L, 95L, 95L, 95L,
94L, 94L, 94L, 94L, 93L, 93L, 92L, 92L, 91L, 90L, 90L, 89L, 89L,
89L, 88L, 88L, 88L, 88L, 88L, 87L, 87L, 87L, 86L, 86L, 86L, 85L,
84L, 83L, 82L, 81L, 80L, 79L, 78L, 77L, 76L, 75L, 74L, 73L, 72L,
71L, 70L, 69L), V2 = c(20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L,
28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L,
41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L,
54L, 55L, 56L, 57L, 58L, 59L, 60L, 61L, 62L, 63L, 64L, 65L, 66L,
67L, 68L, 69L, 70L, 71L, 72L, 73L, 74L, 75L, 76L, 77L, 78L, 79L,
80L, 81L, 82L, 83L, 84L, 85L, 86L, 87L, 88L, 89L, 90L, 91L, 92L,
93L, 94L, 95L, 96L, 97L, 98L, 99L, 100L, 101L, 102L, 103L, 104L,
105L, 106L, 107L, 108L, 109L, 110L, 111L, 112L, 113L, 114L, 115L,
116L, 117L, 118L, 119L, 120L, 121L, 122L, 123L, 124L, 125L, 126L,
127L, 128L, 129L, 130L, 131L, 132L, 133L, 134L, 135L, 136L, 137L,
138L, 139L, 140L, 141L, 142L, 143L, 144L, 145L, 146L, 147L, 148L,
149L, 150L, 151L, 152L, 153L, 154L, 155L, 156L, 157L, 158L, 159L,
160L, 161L, 162L, 163L, 164L, 165L, 166L, 167L, 168L, 169L, 170L,
171L, 172L, 173L, 174L, 175L, 176L, 177L, 178L, 179L, 180L, 181L,
182L, 183L, 184L, 185L, 186L, 187L, 188L, 189L, 190L, 191L, 192L,
193L, 194L, 195L, 196L, 197L, 198L, 199L, 200L, 201L, 202L, 203L,
204L, 205L, 206L, 207L, 208L, 209L, 210L, 211L, 212L, 213L, 214L,
215L, 216L, 217L, 218L, 219L, 220L, 221L, 222L, 223L, 224L, 225L,
226L, 227L, 228L, 229L, 230L, 231L, 232L, 233L, 234L, 235L, 236L,
237L, 238L, 239L, 240L, 241L, 242L, 243L, 244L, 245L, 246L, 247L,
248L, 249L, 250L, 251L, 252L, 253L, 254L, 255L, 256L, 257L, 258L,
259L, 260L, 261L, 262L, 263L, 264L, 265L, 266L, 267L, 268L, 269L,
270L, 271L, 272L, 273L, 274L, 275L, 276L, 277L, 278L, 279L, 280L,
281L, 282L, 283L, 284L, 285L, 286L, 287L, 288L, 289L, 290L, 291L,
292L, 293L, 294L, 295L, 296L, 297L, 298L, 299L, 300L, 301L, 302L,
303L, 304L, 305L, 306L, 307L, 308L, 308L, 308L, 309L, 309L, 309L,
310L, 310L, 310L, 311L, 311L, 312L, 312L, 312L, 312L, 312L, 312L,
312L, 311L, 311L, 310L, 310L, 310L, 310L, 309L, 308L, 307L, 306L,
305L, 304L, 303L, 302L, 301L, 300L, 299L, 298L, 297L, 296L, 295L,
294L, 293L, 292L, 291L, 290L, 289L, 288L, 287L, 286L, 285L, 284L,
283L, 282L, 281L, 280L, 279L, 279L, 278L, 277L, 276L, 275L, 274L,
273L, 272L, 271L, 270L, 269L, 268L, 267L, 266L, 265L, 264L, 263L,
262L, 261L, 260L, 259L, 258L, 257L, 256L, 255L, 254L, 253L, 252L,
252L, 251L, 250L, 249L, 248L, 247L, 246L, 245L, 244L, 243L, 242L,
241L, 240L, 239L, 238L, 237L, 236L, 235L, 234L, 233L, 232L, 231L,
230L, 229L, 228L, 227L, 226L, 225L, 224L, 223L, 222L, 221L, 220L,
219L, 218L, 217L, 216L, 215L, 214L, 213L, 212L, 211L, 210L, 209L,
208L, 207L, 206L, 205L, 204L, 203L, 202L, 201L, 200L, 199L, 198L,
197L, 196L, 195L, 194L, 193L, 192L, 191L, 190L, 189L, 188L, 187L,
186L, 185L, 184L, 183L, 182L, 181L, 180L, 179L, 178L, 177L, 176L,
175L, 174L, 173L, 172L, 171L, 170L, 169L, 168L, 167L, 166L, 165L,
164L, 163L, 162L, 161L, 160L, 159L, 158L, 157L, 156L, 155L, 154L,
153L, 152L, 151L, 150L, 149L, 148L, 147L, 146L, 145L, 144L, 143L,
142L, 141L, 140L, 139L, 138L, 137L, 136L, 135L, 134L, 133L, 132L,
131L, 130L, 129L, 128L, 127L, 126L, 125L, 124L, 123L, 122L, 121L,
120L, 119L, 118L, 117L, 116L, 115L, 114L, 113L, 112L, 111L, 110L,
109L, 108L, 107L, 106L, 105L, 104L, 103L, 102L, 101L, 100L, 99L,
98L, 97L, 96L, 95L, 94L, 93L, 92L, 91L, 90L, 89L, 88L, 87L, 86L,
85L, 84L, 83L, 82L, 81L, 80L, 79L, 78L, 77L, 76L, 75L, 74L, 73L,
72L, 71L, 70L, 69L, 68L, 67L, 66L, 65L, 64L, 63L, 62L, 61L, 60L,
59L, 58L, 57L, 56L, 55L, 54L, 53L, 52L, 51L, 50L, 49L, 48L, 47L,
46L, 45L, 44L, 43L, 42L, 41L, 40L, 39L, 38L, 37L, 36L, 35L, 34L,
33L, 32L, 31L, 30L, 29L, 28L, 27L, 26L, 26L, 26L, 25L, 25L, 24L,
24L, 23L, 23L, 22L, 22L, 21L, 21L, 20L, 20L, 20L)), class = "data.frame", row.names = c(NA,
-613L))
Looks like this:
I can find the maximum chord and draw that:
# draw max dimension points and line
suppressPackageStartupMessages(library(tidyverse))
df_dist = data.frame(as.matrix(dist(cbind(points_ex$V1,points_ex$V2))))
df_dist_x = df_dist %>%
mutate(row.1 = 1:nrow(df_dist)) %>%
mutate(Y = paste0("Y", row_number())) %>%
gather(X, distance, X1:nrow(.)) %>%
select(X, Y, distance) %>%
mutate_at(vars(X, Y), parse_number)
df_dist_x_max <-
df_dist_x %>%
dplyr::filter(distance == max(distance))
points(points_ex[df_dist_x_max$X[1],], col = "red", cex = 2)
points(points_ex[df_dist_x_max$X[2],], col = "red", cex = 2.5)
segments(points_ex[df_dist_x_max$X[1], 'V1'],
points_ex[df_dist_x_max$X[1], 'V2'],
points_ex[df_dist_x_max$X[2], 'V1'],
points_ex[df_dist_x_max$X[2], 'V2'],
col = "green")
And this is what I've been trying to get the longest chord perpendicular to the maximum length chord:
# transform the points and lines into spatial objects
library(sf)
library(sp)
library(rgeos)
points_sf <- st_as_sf(points_ex, coords = c("V1", "V2"))
newline = matrix(c(points_ex[df_dist_x_max$X[1], 'V1'],
points_ex[df_dist_x_max$X[1], 'V2'],
points_ex[df_dist_x_max$X[2], 'V1'],
points_ex[df_dist_x_max$X[2], 'V2']), byrow = T, nrow = 2)
spline <- as(st_as_sfc(st_as_text(st_linestring(newline))), "Spatial") # there is probably a more straighforward solution...
position <- gProject(spline, as(points_sf, "Spatial"))
position <- coordinates(gInterpolate(spline, position))
colnames(position) <- c("X2", "Y2")
segments <-
data.frame(st_coordinates(points_sf), position)
segments$dist <- NULL
for(i in 1:nrow(segments)){
segments$dist[i] <-
proxy::dist(data.frame(segments$X[i], segments$Y[i]),
data.frame(segments$X2[i], segments$Y2[i]))
}
# max width perpendicular to length axis
max_segment <- segments[which.max(segments$dist), ]
max_segment <- segments[segments$Y == max_segment$Y, ]
segments(max_segment$X[1], max_segment$Y[1],
max_segment$X2[1], max_segment$Y2[1],
col = "purple")
segments(max_segment$X[2], max_segment$Y[2],
max_segment$X2[2], max_segment$Y2[2],
col = "purple")
Looks roughly ok, but my problem is that this method for finding the longest chord perpendicular to maximum chord only looks at one side of the maximum chord for the longest distance to the edge of the polygon.
I don't know how to measure every distance from edge to edge of the polygon that is perpendicular to the maximum chord.
This means my method doesn't generalise well at all, here it is applied to another polygon (data here: https://pastebin.com/XpiB6UnX because the dput output made this post too long)
Obviously this is bad because the two purple segments should not be on the same side of the green segment, and it doesn't look like the right location at all for the longest chord perpendicular to the maximum chord.
How can I robustly find the longest chord that is perpendicular to the maximum chord?
Here's a full solution. First some functions:
## this returns the i,j of the largest elements in matrix `m`
findmax <- function(m){
v = which.max(m) - 1
c(v %% nrow(m)+1, v %/% nrow(m)+1)
}
## Return an sf line through a point at an angle of a given length
pline <- function(pt, angle, length){
st_linestring(
cbind(
pt[1] + c(length,-length) * sin(angle),
pt[2] + c(length,-length) * cos(angle)
)
)
}
## return the line that is the chord at angle perp.angle of length through any of the polygon vertices
max_perp_chord <- function(polygon, perp.angle, length){
## get polygon vertices
pts = st_coordinates(polygon)[,c(1,2)]
## return the perpendicular lines
perplines = lapply(1:nrow(pts), function(i){
## through the i-th vertex
xy = pts[i,,drop=FALSE]
perpline = pline(xy, perp.angle, length)
## intersect it with the polygon
inters = st_intersection(polygon, perpline)
inters
}
)
## get the vector of intersection lengths, find the largest
perplengths = unlist(lapply(perplines, st_length))
longest = which.max(perplengths)
## return the longest line
perplines[[longest]]
}
### find the max length chord across all pairs of vertex points
max_chord <- function(polygon){
## get polygon coordinates
xy = st_coordinates(polygon)[,1:2]
## compute the distance matrix and find largest element
df_dist = as.matrix(dist(xy))
maxij = findmax(df_dist)
## those elements define the largest chord
chord = rbind(
xy[maxij[1],],
xy[maxij[2],]
)
chord
}
find_max_chord <- function(spolygon, chord=max_chord(spolygon)){
## Now compute the length and angle of the longest chord
chord.length = sqrt(diff(chord[,1])^2 + diff(chord[,2])^2)
chord.theta = atan2(diff(chord[,1]), diff(chord[,2]))
## The perpendicular is at this angle plus pi/2 radians
perp = chord.theta + pi/2
max_perp_chord(spolygon, perp, chord.length)
}
Here's how this goes together. The only dependency is sf - nothing else but base R is used:
library(sf)
Next massage your data into an sf polygon object:
## construct an sf polygon from points:
polygon = st_polygon(list(as.matrix(rbind(points_ex, points_ex[1,]))))
Get the max length chord between vertices:
chord = max_chord(polygon)
Plot the polygon and the chord and the chord points:
plot(polygon)
points(chord, col="red",cex=2)
lines(chord,col="green",lwd=2)
Now the meat. Get the chord perpendicular to the max chord through one of the polygon vertices:
## get the max perpendicular chord
pchord = find_max_chord(polygon, chord)
Plot it.
plot(pchord,add=TRUE)
I've tested this on another example, but not the second one you posted... Should work but...
It does...
If your polygon is monotone, you can apply the next approach:
I assume that polygon is rotated to make the maximum length chord vertical (as your two images show).
Start from the top point. Traverse left part in CCW direction, right part in clockwise direction.
Key moment: the longest chord must touch some polygon vertex (because max cannot be reached on the line segment between two inner points at edges).
So get vertex with the highest Y-coordinate (either at the left part or at the right part). Calculate point at the edge on opposite side. Get length.
Get vertex with the next Y-coordinate. Calculate point at the edge on opposite side. Get length. Continue.
I am trying to use case_when in order to pad out a string in R, dependent on the string length.
I take the following 3 examples with lengths 11, 12 and 13:
V1 V2
74300000330 00074300000330
811693200042 08011693200042
8829999820128 88029999820128
V1 is the column I am trying to match with V2
The first row in V1 has 11 digits, if the row has 11 digits then add 3 zeros at the begining of the number.
I have tried the following code without any luck (I have also tried it with paste0());
df %>%
mutate(col3 = case_when(length(col1) == 11 ~ str_pad(14, width = 3, pad = "0")))
The second has 12 digits, so I should add one zero at the begining of the number and then another zero between (counting from the left) the first digit and (counting from right) 11th digit, so row 2 would go from 81169... to 0801169....
The third row has 13 digits so I should paste a zero between the (counting from the left) 2nd digit and (counting from the right) the 11th digit. So the begining of the sequence goes from 88299 to 880299.
The total number of digits in the sequence should be exactly 14.
Data:
df <- structure(list(col1 = structure(c(1L, 1L, 1L, 2L, 2L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L,
4L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 7L, 7L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 11L, 12L, 12L, 13L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L,
20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 21L,
21L, 21L, 22L, 22L, 22L, 22L, 22L, 23L, 23L, 23L, 23L, 23L, 23L,
23L, 23L, 23L, 24L, 24L, 24L, 24L, 25L, 26L, 27L, 27L, 27L, 27L,
27L, 27L, 27L, 27L, 27L, 27L, 28L, 28L, 28L, 29L, 30L, 30L, 30L,
31L, 32L, 33L, 33L, 33L, 33L, 33L, 34L, 34L, 34L, 34L, 35L, 36L,
36L, 36L, 36L, 36L, 36L, 36L, 36L, 36L, 37L, 38L, 38L, 38L, 38L,
38L, 39L, 39L, 39L, 39L, 40L, 41L, 41L, 41L, 42L, 42L, 43L, 44L,
45L, 45L, 45L, 45L, 45L, 46L, 46L, 47L, 47L, 47L, 47L, 47L, 47L,
47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L,
47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L,
47L, 48L, 49L, 49L, 49L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L,
50L, 50L, 50L, 50L, 50L, 50L, 51L, 51L, 51L, 51L, 51L, 51L, 51L,
51L, 51L, 51L, 51L, 52L, 52L, 53L, 53L, 53L, 53L, 54L, 55L, 56L,
56L, 56L, 56L, 56L, 56L, 56L, 56L, 57L, 58L, 59L, 59L, 60L, 60L,
60L, 60L, 60L, 60L, 60L, 60L, 60L, 60L, 60L, 61L, 61L, 61L, 61L,
61L, 62L, 62L, 63L, 64L, 65L, 66L, 66L, 66L, 66L, 66L, 66L, 66L,
66L, 66L, 66L, 66L, 66L, 66L, 66L, 66L, 66L, 66L, 67L, 67L, 68L,
68L, 69L, 69L, 69L, 70L, 70L, 70L, 70L, 70L, 70L, 71L, 71L, 71L,
71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L,
71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 72L, 72L,
72L, 72L, 72L, 72L, 72L, 72L, 72L, 72L, 72L, 72L, 72L, 72L, 72L,
73L, 73L, 73L, 73L, 73L, 73L, 73L, 73L, 73L, 73L, 73L, 73L, 74L,
74L, 74L, 74L, 74L, 75L, 75L, 75L, 76L, 77L, 77L, 78L, 79L, 80L,
81L, 82L, 83L, 83L, 83L, 83L, 83L, 83L, 83L, 83L, 83L, 84L, 84L,
84L, 85L, 86L, 86L, 87L, 87L, 87L, 87L, 88L, 89L, 90L, 91L, 92L,
93L, 93L, 93L, 94L, 94L, 95L, 95L, 95L, 95L, 95L, 96L, 97L, 97L,
97L, 98L, 99L, 100L, 100L, 100L, 100L, 101L, 102L, 102L, 103L,
104L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 106L,
107L, 107L, 108L, 109L, 109L, 109L, 109L, 109L, 109L, 109L, 110L,
110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 111L, 111L, 111L,
111L, 112L, 112L, 112L, 112L, 112L, 112L, 112L, 113L, 113L, 113L,
113L, 113L, 113L, 114L, 114L, 114L, 114L, 114L, 114L, 114L, 114L,
115L, 116L, 116L, 117L, 117L, 117L, 118L, 118L, 118L, 118L, 118L,
118L, 118L, 118L, 118L, 118L, 119L, 119L, 119L, 119L, 119L, 119L,
119L, 119L, 119L, 120L, 120L, 120L, 121L, 122L, 122L, 122L, 122L,
122L, 122L, 122L, 123L, 123L, 123L, 123L, 123L, 123L, 123L), .Label = c("11114110010",
"11114110022", "11114110029", "11114110036", "11114110210", "11114110230",
"11114110261", "11114110271", "11114110281", "11114110291", "11114110316",
"11114110526", "11780900029", "11780900050", "11780900660", "11780900661",
"12451500878", "12451567602", "12550000033", "12550000365", "12550000366",
"12550000367", "12550000371", "12550000376", "12550000377", "12550000384",
"12550000388", "12550000392", "12550000393", "12550000397", "12550000401",
"12550000402", "12550000538", "12550006763", "12550006764", "12550020040",
"12550020042", "12550020043", "12550020044", "12550020188", "12550020204",
"12550020212", "12550090015", "12800046631", "12800063141", "12800070612",
"14300002922", "14300002923", "14300002924", "14300002925", "14300002934",
"14300002940", "14300002941", "14300002942", "14300003300", "14300004091",
"14300004296", "14300004299", "14300004301", "14300004648", "14300004650",
"14300004651", "14300070522", "15543760143", "15543760145", "15543760186",
"15543760235", "15543760253", "17089302817", "17103800044", "17103800047",
"17103800048", "17103800053", "17103800056", "17103800058", "17103800059",
"17103801173", "17103801175", "17232305018", "17447100091", "17510100575",
"17510100576", "17510121064", "17510121065", "17510181458", "17732447059",
"17762300048", "17762300060", "18903644280", "19955508003", "19955508050",
"19955508060", "19955508061", "19955508531", "19955508534", "19955508758",
"19955508792", "19955508800", "19955508801", "19955508832", "19955508992",
"19955509803", "19955538570", "19955538696", "19955538725", "19955538792",
"21291912261", "21780900078", "22550081121", "22550081122", "22800025406",
"22800030050", "24300070590", "25543760142", "25543760521", "29955539550",
"31291912240", "39955508520", "41114110525", "57103800060", "74300000330",
"8,11693E+11", "8,83E+12"), class = "factor"), col2 = structure(c(1L,
1L, 1L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 4L, 4L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 7L,
7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 11L, 12L, 12L, 13L, 13L, 14L, 15L,
16L, 17L, 18L, 19L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L,
20L, 20L, 20L, 20L, 21L, 21L, 21L, 22L, 22L, 22L, 22L, 22L, 23L,
23L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 24L, 24L, 24L, 24L, 25L,
26L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 28L, 28L,
28L, 29L, 30L, 30L, 30L, 31L, 32L, 33L, 33L, 33L, 33L, 33L, 34L,
34L, 34L, 34L, 35L, 36L, 36L, 36L, 36L, 36L, 36L, 36L, 36L, 36L,
37L, 38L, 38L, 38L, 38L, 38L, 39L, 39L, 39L, 39L, 40L, 41L, 41L,
41L, 42L, 42L, 43L, 44L, 45L, 45L, 45L, 45L, 45L, 46L, 46L, 47L,
47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L,
47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L,
47L, 47L, 47L, 47L, 47L, 47L, 48L, 49L, 49L, 49L, 50L, 50L, 50L,
50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 51L, 51L,
51L, 51L, 51L, 51L, 51L, 51L, 51L, 51L, 51L, 52L, 52L, 53L, 53L,
53L, 53L, 54L, 55L, 56L, 56L, 56L, 56L, 56L, 56L, 56L, 56L, 57L,
58L, 59L, 59L, 60L, 60L, 60L, 60L, 60L, 60L, 60L, 60L, 60L, 60L,
60L, 61L, 61L, 61L, 61L, 61L, 62L, 62L, 63L, 64L, 65L, 66L, 66L,
66L, 66L, 66L, 66L, 66L, 66L, 66L, 66L, 66L, 66L, 66L, 66L, 66L,
66L, 66L, 67L, 67L, 68L, 68L, 69L, 69L, 69L, 70L, 70L, 70L, 70L,
70L, 70L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L,
71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L,
71L, 71L, 71L, 72L, 72L, 72L, 72L, 72L, 72L, 72L, 72L, 72L, 72L,
72L, 72L, 72L, 72L, 72L, 73L, 73L, 73L, 73L, 73L, 73L, 73L, 73L,
73L, 73L, 73L, 73L, 74L, 74L, 74L, 74L, 74L, 75L, 75L, 75L, 76L,
77L, 77L, 78L, 79L, 80L, 81L, 82L, 83L, 83L, 83L, 83L, 83L, 83L,
83L, 83L, 83L, 84L, 84L, 84L, 85L, 86L, 86L, 87L, 87L, 87L, 87L,
88L, 89L, 90L, 91L, 92L, 93L, 93L, 93L, 94L, 94L, 95L, 95L, 95L,
95L, 95L, 96L, 97L, 97L, 97L, 98L, 99L, 100L, 100L, 100L, 100L,
101L, 102L, 102L, 103L, 104L, 105L, 105L, 105L, 105L, 105L, 105L,
105L, 105L, 105L, 106L, 107L, 107L, 108L, 109L, 109L, 109L, 109L,
109L, 109L, 109L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L,
110L, 111L, 111L, 111L, 111L, 112L, 112L, 112L, 112L, 112L, 112L,
112L, 113L, 113L, 113L, 113L, 113L, 113L, 114L, 114L, 114L, 114L,
114L, 114L, 114L, 114L, 115L, 116L, 116L, 117L, 117L, 117L, 118L,
118L, 118L, 118L, 118L, 118L, 118L, 118L, 118L, 118L, 119L, 119L,
119L, 119L, 119L, 119L, 119L, 119L, 119L, 120L, 120L, 120L, 121L,
123L, 122L, 123L, 123L, 123L, 123L, 123L, 127L, 124L, 126L, 126L,
127L, 127L, 125L), .Label = c("00011114110010", "00011114110022",
"00011114110029", "00011114110036", "00011114110210", "00011114110230",
"00011114110261", "00011114110271", "00011114110281", "00011114110291",
"00011114110316", "00011114110526", "00011780900029", "00011780900050",
"00011780900660", "00011780900661", "00012451500878", "00012451567602",
"00012550000033", "00012550000365", "00012550000366", "00012550000367",
"00012550000371", "00012550000376", "00012550000377", "00012550000384",
"00012550000388", "00012550000392", "00012550000393", "00012550000397",
"00012550000401", "00012550000402", "00012550000538", "00012550006763",
"00012550006764", "00012550020040", "00012550020042", "00012550020043",
"00012550020044", "00012550020188", "00012550020204", "00012550020212",
"00012550090015", "00012800046631", "00012800063141", "00012800070612",
"00014300002922", "00014300002923", "00014300002924", "00014300002925",
"00014300002934", "00014300002940", "00014300002941", "00014300002942",
"00014300003300", "00014300004091", "00014300004296", "00014300004299",
"00014300004301", "00014300004648", "00014300004650", "00014300004651",
"00014300070522", "00015543760143", "00015543760145", "00015543760186",
"00015543760235", "00015543760253", "00017089302817", "00017103800044",
"00017103800047", "00017103800048", "00017103800053", "00017103800056",
"00017103800058", "00017103800059", "00017103801173", "00017103801175",
"00017232305018", "00017447100091", "00017510100575", "00017510100576",
"00017510121064", "00017510121065", "00017510181458", "00017732447059",
"00017762300048", "00017762300060", "00018903644280", "00019955508003",
"00019955508050", "00019955508060", "00019955508061", "00019955508531",
"00019955508534", "00019955508758", "00019955508792", "00019955508800",
"00019955508801", "00019955508832", "00019955508992", "00019955509803",
"00019955538570", "00019955538696", "00019955538725", "00019955538792",
"00021291912261", "00021780900078", "00022550081121", "00022550081122",
"00022800025406", "00022800030050", "00024300070590", "00025543760142",
"00025543760521", "00029955539550", "00031291912240", "00039955508520",
"00041114110525", "00057103800060", "00074300000330", "08011693200041",
"08011693200042", "88029999819907", "88029999820074", "88029999820083",
"88029999820128"), class = "factor")), row.names = c(NA, -513L
), class = "data.frame")
A few issues here. Your columns appear to be factors, which can create confusing problems when you apply string functions to them. You want them to be character, not factor. The correct way to check the length of a string is with nchar (spoiler alert: does not work with factor data!).
Your rules for padding seem a little arbitrary, but the following should work. For padding "within" the digit string, gsub and regular expressions work wonders.
df2 <- mutate_at(df, vars(col1, col2), as.character) %>%
mutate(col3 = case_when(
nchar(col1) == 11 ~ str_pad(col1, width = 14, pad = '0'),
nchar(col1) == 12 ~ gsub('(\\d)(\\d+)', '0\\10\\2', col1),
nchar(col1) == 13 ~ gsub('(\\d\\d)(\\d+)', '\\10\\2', col1),
T ~ col1
))
col1 col3
<chr> <chr>
1 74300000330 00074300000330
2 811693200042 08011693200042
3 8829999820128 88029999820128
Goal
My data consists of numerous points which would be too much to represent as a scatter plot. I would like to plot a point density.
Specifically, I am wondering if there is a way to obtain something similar to this with ggplot2:
Current code
pEPNSABobs1<-ggplot(dataE, aes(comp1,mod1obs, group=1))+
geom_smooth(aes(color="chartreuse"), se=F, linetype="longdash", size=1)+
stat_smooth(data=dataE, fill="chartreuse", color="chartreuse4", linetype="blank")+
geom_smooth(data=dataS, aes(comp1,mod1obs, group=1, color="lightpink"), se=F, linetype="longdash", size=1)+
stat_smooth(data=dataS, fill="lightpink",color="lightpink4", linetype="blank")+
scale_colour_manual(name = 'Legend',
values =c('lightpink'='lightpink4','chartreuse'='chartreuse4'), labels = c('PIM','ABB'))+
scale_size_area() +
xlab("CDD0.year") +
ylab(expression(f[CDD0.year.obs]))+
labs(title='(A)')+
theme(plot.title = element_text(hjust = 0, vjust=1),axis.title = element_text(size = rel(1.2)), title=element_text(size = rel(1.2)),axis.text=element_text(size = rel(1.2)),
legend.text=element_text(size = rel(1.2)))
Data
DataE <- structure(list(comp1 = c(1338.2461, 1721.8119, 1878.2578, 1781.8827, 1813.2432, 1711.5277, 2033.0855, 1636.394, 1580.0748, 1834.4927, 2150.4177, 1790.7859, 1980.2718, 1610.0624, 2064.5809, 2002.82, 1652.7442, 2143.8216, 2015.1028, 2201.7947, 1610.4855, 1983.2706, 1979.9317, 2282.4141, 1763.288, 2204.3358, 1781.4969, 2114.2082, 1567.7841, 2089.6192, 1653.2401, 1709.9662, 2118.7251, 1843.5898, 1561.9472, 1839.0119, 2441.8013, 1684.3595, 1862.4287, 2043.3588, 2043.1502, 1999.5591, 1929.3686, 1897.746, 2073.494, 1345.488, 1622.3962, 1692.8681, 1847.7492, 1863.1212, 1759.8359, 2092.2891, 1671.9526, 1646.3103, 1918.8867, 2187.7568, 1833.4847, 2031.483, 1653.4428, 2118.2413, 2049.7907, 1705.1815, 2191.5739, 2054.9909, 2253.6989, 1664.6036, 2038.5609, 2021.5454, 2380.9635, 1788.6053, 2225.5046, 1834.5804, 2104.5817, 1523.2706, 2150.1426, 1722.6452, 1750.2867, 2142.4975, 1871.6772, 1562.7973, 1865.9236, 2424.8774, 1700.2864, 1850.6267, 2059.4913, 2047.4193, 2002.2637, 1941.7118, 1916.4346, 2081.8719, 1357.3087, 1645.5375, 1739.0249, 1984.4754, 2500.5373, 2126.0111, 2067.2634, 2163.0708, 2149.2563, 1939.6116, 1872.462, 2024.5612, 2158.432, 1732.0504, 2287.9286, 1640.2811, 2332.7904, 2475.8456, 2097.2091, 1942.9509, 2121.2583, 1899.7503, 2060.2692, 2459.2143, 2395.0327, 2280.1373, 2628.2383, 2118.6924, 2209.724, 2228.5074, 2008.7249, 1819.8529, 2144.0186, 2466.9707, 2479.4277, 2483.6576, 2403.0477, 2025.4349, 2150.5789, 2332.0055, 2115.4415, 1981.608, 1912.8366, 2296.8201, 1985.4613, 1875.0743, 1969.649, 2048.9522, 1900.5933, 1921.7856, 2066.8277, 1859.8671, 1698.0369, 1493.0678, 1910.5082, 2102.3289, 2048.3883, 1949.4451, 1838.7137, 2257.4143, 1958.3303, 1836.1803, 2138.1416, 2194.0097, 1919.8365, 2200.6554, 1829.6413, 2290.396, 2156.0543, 1854.5949, 2240.8976, 2060.3596, 2362.8724, 1794.141, 2183.8272, 2129.4498, 2517.4392, 1901.8863, 2440.1299, 2059.9263, 2291.6513, 1642.4418, 2281.0632, 1888.9796, 1931.7574, 2232.7776, 1953.4144, 1613.9024, 2000.765, 2485.617, 1774.7963, 1900.3834, 2192.5563, 2149.8866, 2090.371, 2076.542, 2050.3292, 2152.2821, 1482.7591, 1674.3366, 1905.3904, 1971.8098, 1596.7659, 1899.0917, 2400.7906, 2048.1242, 1968.4794, 2057.2929, 2053.9162, 1837.3839),
mod1obs = c(0.695830136555704, 1.06337206841913, 1.14861889402282, 1.02790421278896, 1.11775825622145, 1.43143032263757, 1.66436105688503, 1.79407632202943, 1.54246894155172, 1.47641083551176, 1.61115035184228, 1.68350958534902, 1.67052919588235, 1.86924870748066, 1.52139461422218, 1.7206336638354, 1.77255323580939, 1.67014367453142, 1.4828365451913, 1.30632542733184, 1.28066122419783, 1.36281355152317, 0.922105627695953, 0.485097355039981, 0.527121512464299, 0.810559323027329, 1.02715532531355, 1.14244699815587, 0.999551948498188, 0.951386076673739, 0.708742765512991, 0.591036273251615, 0.954269537513021, 1.33390119349384, 1.32340100204498, 1.48365670129187, 1.18354444471793, 1.17538278706292, 1.13658706470025, 1.09659824662134, 1.33968115088882, 0.97520848249232, 0.926838756160161, 0.916231908195398, 0.899325038616274, 1.16824595028327, 1.11899066863449, 1.11661928913426, 1.12284805168664, 1.05643303658747, 1.06710730526053, 1.22833157533542, 0.816937586646849, 1.00335563406519, 1.402274505934, 1.41743848783742, 1.14038690876476, 0.985366123273008, 0.843537703363177, 0.99753466530825, 0.956105174325841, 0.969042102699615, 1.0638789437522, 0.996797745632918, 0.941726356849215, 0.926215042532983, 1.03697021488655, 1.24561208170624, 0.903344544294698, 0.852585271143484, 1.14969403609893, 0.951461942556407, 0.969946352436395, 0.856006282102713, 1.11792476085488, 1.13412438712209, 1.15896430227978, 1.13984814117922, 1.26400232474479, 1.3157371290327, 1.10269366567886, 0.852330476973561, 1.06803837916353, 1.00555346861516, 1.07980330220743, 0.999413354748127, 0.835341074425715, 0.750343026273598, 0.750086242001393, 0.845059990359667, 1.01550610388101, 0.97183262908527, 1.02351377836664, 0.646201913922726, 0.832803589659996, 1.02112583534199, 1.05276036662667, 0.778087551043244, 1.04311980643046, 1.14374772863475, 1.26330982513545, 1.20248850820938, 1.03073057299644, 1.06049050327533, 0.834166914153454, 0.984789639654932, 1.08805486508421, 1.40007199005997, 1.33458208694833, 1.09447880143235, 1.32094094746787, 1.01033105751792, 1.15916733600421, 0.83814699368444, 0.922361964325012, 0.821150439904281, 0.757025921805821, 0.796454341025722, 0.753864630868797, 0.709679353471203, 0.947247272243527, 1.00883103860073, 0.954505648136825, 0.921522424138087, 0.845557836615405, 0.959332247443167, 0.726910705401073, 0.825894718073668, 0.849038625330317, 0.860196521211579, 0.88419468576954, 0.986401829980697, 0.844208566273473, 0.733125744746535, 0.718381697185048, 0.826648914954142, 0.787244473508777, 0.65512488225044, 0.752501264072651, 0.723469068584019, 0.743094728670719, 1.11846155736291, 1.08257557110011, 0.982979407982961, 0.885561974111481, 0.898359447667526, 0.770274340254794, 0.964105752093132, 1.419398413941, 1.06346164710011, 0.94787165982562, 1.21888848634176, 0.902929635142765, 0.924412982720593, 1.10282712434799, 0.899503164916735, 0.850644250882259, 1.01891899751745, 1.21897925575381, 1.26360292391086, 0.924187893834376, 0.930518507010402, 1.03052665863541, 1.15564902628815, 1.01660158016781, 0.904615715917612, 0.523182725557325, 0.625326640420025, 1.03256402151814, 1.19178281509474, 1.21239098183734, 1.00933857234806, 1.01170794284609, 1.02137919901009, 0.784528093252022, 0.97997559028248, 1.15073167836534, 1.09499252446889, 1.02629257850687, 0.865796462253095, 1.08500359034302, 1.14790809213261, 0.96317054146643, 1.1105434300635, 0.817447776121345, 0.888257155328381, 0.884423797252747, 0.935227482404514, 1.07740310874975, 1.02969991871418, 0.922959386699878, 0.642079018955661, 0.653388944491712, 0.597053974284795, 0.882728190259291, 0.918365607321804, 0.814327793136811, 1.00391967118938, 1.09246216046294, 1.07787732161824)),
row.names = c(2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 51L, 52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L, 60L, 61L, 62L, 63L, 64L, 65L, 66L, 67L, 68L, 69L, 70L, 71L, 72L, 73L, 74L, 75L, 76L, 77L, 78L, 79L, 80L, 81L, 82L, 83L, 84L, 85L, 86L, 87L, 88L, 89L, 90L, 91L, 92L, 93L, 94L, 95L, 97L, 98L, 99L, 100L, 101L, 102L, 103L, 104L, 105L, 106L, 107L, 108L, 109L, 110L, 111L, 112L, 113L, 114L, 115L, 116L, 117L, 118L, 119L, 120L, 121L, 122L, 123L, 124L, 125L, 126L, 127L, 128L, 129L, 130L, 131L, 132L, 133L, 134L, 135L, 136L, 137L, 138L, 139L, 140L, 141L, 142L, 143L, 144L, 145L, 146L, 147L, 148L, 149L, 150L, 151L, 152L, 153L, 154L, 155L, 156L, 157L, 158L, 159L, 160L, 161L, 162L, 163L, 164L, 165L, 166L, 167L, 168L, 169L, 170L, 171L, 172L, 173L, 174L, 175L, 176L, 177L, 178L, 179L, 180L, 181L, 182L, 183L, 184L, 185L, 186L, 187L, 188L, 189L, 190L, 191L, 192L, 193L, 194L, 195L, 196L, 197L, 198L, 199L, 200L, 201L, 202L, 203L),
class = "data.frame")
and
DataS <- structure(list(comp1 = c(1800.2451, 1781.6087, 1737.2914, 1816.9749, 1643.348, 1438.5377, 1338.2461, 1721.8119, 1878.2578, 1781.8827, 1813.2432, 1711.5277, 2033.0855, 1636.394, 1580.0748, 1834.4927, 2150.4177, 1790.7859, 1980.2718, 1610.0624, 2064.5809, 2002.82, 1652.7442, 2143.8216, 2015.1028, 2201.7947, 1610.4855, 1983.2706, 1979.9317, 2282.4141, 1763.288, 2204.3358, 1781.4969, 2114.2082, 1567.7841, 2089.6192, 1653.2401, 1709.9662, 2118.7251, 1843.5898, 1561.9472, 1839.0119, 2441.8013, 1684.3595, 1862.4287, 2043.3588, 2043.1502, 1999.5591, 1929.3686, 1897.746, 2073.494, 1345.488, 1622.3962, 1692.8681, 1845.1739, 1661.4121, 1746.5679, 2225.7098, 1973.0317, 1877.0764, 1875.4714, 1996.5981, 1712.4015, 1715.2255, 2149.4741, 2303.7732, 1872.9362, 2279.4588, 1513.787, 2184.5552, 2361.3593, 2148.0699, 1978.381, 2008.029, 1955.9995, 1986.7266, 2079.5143, 1960.9757, 1809.0558, 2193.2401, 1729.3818, 1926.0217, 2157.6266, 1788.1556, 1475.6945, 1854.1782, 2090.9429, 2242.7276, 2120.1378, 1982.7239, 1860.3072, 1906.2126, 2115.5587, 1970.3582, 1809.3253, 1851.249, 2147.1908, 1774.673, 1816.4041, 1934.3119, 1901.6345, 1943.6281, 1793.2585, 1826.9192, 1727.9485, 1506.61, 1440.656, 1748.6882, 1956.9693, 1938.5561, 1850.1536, 1645.5995, 2086.7712, 1723.6074, 1805.0789, 2089.9296, 2139.9376, 1891.8347, 2158.9863, 1525.1479, 2132.2023, 2061.2398, 1707.3251, 2202.9106, 2017.0934, 2359.064, 1781.5352, 2081.9002, 2091.391, 2307.257, 1722.3419, 2200.4204, 1752.5609, 2110.2504, 1432.5841, 2121.8681, 1734.5244, 1912.6964, 2134.543, 1996.4716, 1720.3715, 1902.6841, 2402.3462, 1850.2001, 1919.6724, 2123.1945, 2177.1916, 2134.0072, 1969.5795, 1896.8108, 2132.3515, 1451.0973, 1612.5966, 1725.3021, 1581.0971, 2347.5604, 2483.6352, 2035.8553, 2426.3861, 1655.1293, 2382.9981, 2468.8676, 2256.6275, 2087.2235, 2124.0978, 2068.831, 2150.4903, 2262.8365, 2198.5672, 1968.5931, 2418.6407, 1953.0811, 2108.4554, 2361.584, 1979.866, 1741.6303, 2087.9856, 2286.7883, 2395.2738, 2270.3263, 2075.8854, 1874.2124, 2038.3772, 2245.1034, 2075.8632, 1946.548, 1955.202, 2355.13, 2011.3717, 1975.8039, 2099.3788, 2136.0128, 1909.835, 1965.8319, 1829.9619, 1630.4157, 1506.5606, 1897.0851, 2069.4124, 2024.9848),
mod1obs = c(1.13466310792768, 0.931923716274403, 1.48217365869915, 1.21838260146353, 1.79940276419435, 1.76643559356371, 2.45574670199418, 1.85759003858763, 1.52414873587958, 1.56924844587154, 1.75782289196183, 1.21854647206407, 1.67402271470746, 1.57661741696957, 1.54915950383801, 1.16767311295364, 1.51513098169545, 1.58721974127532, 1.48203349385421, 1.25150055063036, 1.55523168986921, 1.16757976105911, 1.06103912158879, 1.38682784393209, 1.26706181742404, 0.922200575100749, 1.15475122512565, 0.919460864722599, 0.622581998594566, 0.888857360932288, 0.561258461668506, 0.784221352878807, 1.05841796267749, 0.954913917366984, 0.864989456398651, 0.405901971360923, 0.437238711188011, 0.238832040897009, 0.398444779604082, 0.596968751447861, 0.780373761619182, 0.593964337126505, 0.674617006647725, 0.803014994811282, 0.914435218814932, 0.832419908437008, 0.870618364283852, 0.66205782789832, 0.668271801470194, 0.582218329504303, 0.551471500972662, 0.546862377936455, 0.782666777798501, 0.797397188682925, 0.233703554128143, 0.33078183414865, 0.477622787992685, 0.788352242940992, 0.655192745268702, 0.629025187982574, 0.606750329361944, 0.688876228035905, 0.811952779578203, 0.878151955673824, 0.666102340741867, 0.234485500963779, 0.377020837708222, 0.431015138976773, 0.599545478659815, 0.485341173599891, 0.54118137004067, 0.822700963102403, 0.680465320458669, 0.750819741408091, 0.914425077741122, 1.03163581838662, 0.750374660256944, 0.994337592683161, 1.00941746422589, 1.07118881485789, 1.2413361662074, 0.862210731330022, 0.807945428753615, 1.01206355371074, 1.1466781183792, 1.04520757430783, 0.906546865827641, 0.802138329712516, 0.927446849752543, 0.909606938810288, 0.726203964235328, 0.63174380758208, 0.827670510708034, 0.796140009519362, 0.908331184943455, 1.24437229369026, 1.01038032593664, 1.20106590210506, 1.47001915216196, 1.4216087908593, 1.19649249116061, 1.28729733432818, 1.050493946874, 0.490028901207541, 0.472897051757391, 0.474117219731324, 0.85500755809614, 1.16164758242536, 1.3257510136531, 1.28575789166575, 1.63477173725642, 2.45069111586777, 2.50502453325512, 1.70995863697504, 2.4729188484128, 2.65244156137921, 2.52288384634894, 2.91133218222026, 2.80631798084833, 2.85068631154525, 2.50623525545215, 2.39950652546403, 2.49930350794263, 2.47640980454363, 2.5743114316923, 2.27093700546959, 1.9784591153946, 1.794185941834, 1.59685624413893, 1.21577767933037, 1.1993875662254, 0.905547156548879, 0.791419465032495, 1.5777113878502, 1.46727507228871, 1.03812304747397, 1.04471285876448, 0.89791346550258, 1.00531560652384, 1.03963310517253, 1.55611837868102, 1.32499035130457, 1.32064172678175, 1.61378461617361, 1.77550053496517, 1.7444038412628, 1.88853641144745, 1.44554726136069, 1.52169806808946, 1.38482876983135, 1.50693746767208, 1.56467290333013, 1.55664813927757, 1.65044541980974, 1.50357779174674, 0.764054807030921, 0.495778285132959, 0.596411488294826, 0.693937433662632, 0.932011439769366, 1.10514738904478, 0.904927677550852, 0.969827490109055, 1.05101670841548, 1.12392075468322, 1.33509689588176, 1.20342821574007, 0.993967001310557, 1.20898145783709, 1.05447146612782, 0.744232955898758, 0.844458068128595, 0.780144262373508, 0.547168299748365, 2.83490197058459, 1.23839568773548, 1.10988758878693, 0.867733363063826, 0.73962833236251, 0.85416901457008, 0.784618799106827, 0.725934558423183, 0.834658973996715, 0.875948198454898, 0.881964176473614, 0.933974735332668, 0.870382237844421, 0.772181200080244, 0.829814527221943, 0.929018949289857, 0.892966747157662, 0.891484338497291, 0.942086202624782, 0.671953671900624, 0.788435092592626, 0.620007838797593, 0.87841213508972, 1.09789753017474, 0.917411139887858, 1.12120717286975)),
row.names = c(2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L, 60L, 61L, 62L, 63L, 64L, 65L, 66L, 67L, 68L, 69L, 70L, 71L, 72L, 73L, 74L, 75L, 76L, 77L, 78L, 79L, 80L, 81L, 82L, 83L, 84L, 85L, 86L, 87L, 88L, 89L, 90L, 91L, 92L, 93L, 94L, 95L, 96L, 97L, 98L, 99L, 100L, 101L, 102L, 103L, 104L, 105L, 106L, 107L, 108L, 109L, 110L, 111L, 112L, 113L, 114L, 115L, 116L, 117L, 118L, 119L, 120L, 121L, 122L, 123L, 124L, 125L, 126L, 127L, 128L, 129L, 130L, 131L, 132L, 133L, 134L, 135L, 136L, 137L, 138L, 139L, 140L, 141L, 142L, 143L, 144L, 145L, 146L, 147L, 148L, 149L, 150L, 151L, 152L, 153L, 154L, 155L, 156L, 158L, 159L, 160L, 161L, 162L, 163L, 164L, 165L, 166L, 167L, 168L, 169L, 170L, 171L, 172L, 173L, 174L, 175L, 176L, 177L, 178L, 179L, 180L, 181L, 182L, 183L, 184L, 185L, 186L, 187L, 188L, 189L, 190L, 191L, 192L, 193L, 194L, 195L, 196L, 197L, 198L, 199L, 200L, 201L, 202L),
class = "data.frame")
Option 1: Density Polygons
ggplot(DataS, aes(comp1, mod1obs)) +
stat_density_2d(aes(fill = ..level..),
geom = "polygon",
bins = 10) +
scale_fill_gradientn(colors = c("#FFEDA0",
"#FEB24C",
"#F03B20"))
Option 2: Hexagon Bins
ggplot(DataS, aes(comp1, mod1obs)) +
geom_hex(bins = 10) +
scale_fill_gradientn(colors = c("#FFEDA0",
"#FEB24C",
"#F03B20"))
Data
DataS <- structure(list(comp1 = c(1800.2451, 1781.6087, 1737.2914, 1816.9749, 1643.348, 1438.5377, 1338.2461, 1721.8119, 1878.2578, 1781.8827, 1813.2432, 1711.5277, 2033.0855, 1636.394, 1580.0748, 1834.4927, 2150.4177, 1790.7859, 1980.2718, 1610.0624, 2064.5809, 2002.82, 1652.7442, 2143.8216, 2015.1028, 2201.7947, 1610.4855, 1983.2706, 1979.9317, 2282.4141, 1763.288, 2204.3358, 1781.4969, 2114.2082, 1567.7841, 2089.6192, 1653.2401, 1709.9662, 2118.7251, 1843.5898, 1561.9472, 1839.0119, 2441.8013, 1684.3595, 1862.4287, 2043.3588, 2043.1502, 1999.5591, 1929.3686, 1897.746, 2073.494, 1345.488, 1622.3962, 1692.8681, 1845.1739, 1661.4121, 1746.5679, 2225.7098, 1973.0317, 1877.0764, 1875.4714, 1996.5981, 1712.4015, 1715.2255, 2149.4741, 2303.7732, 1872.9362, 2279.4588, 1513.787, 2184.5552, 2361.3593, 2148.0699, 1978.381, 2008.029, 1955.9995, 1986.7266, 2079.5143, 1960.9757, 1809.0558, 2193.2401, 1729.3818, 1926.0217, 2157.6266, 1788.1556, 1475.6945, 1854.1782, 2090.9429, 2242.7276, 2120.1378, 1982.7239, 1860.3072, 1906.2126, 2115.5587, 1970.3582, 1809.3253, 1851.249, 2147.1908, 1774.673, 1816.4041, 1934.3119, 1901.6345, 1943.6281, 1793.2585, 1826.9192, 1727.9485, 1506.61, 1440.656, 1748.6882, 1956.9693, 1938.5561, 1850.1536, 1645.5995, 2086.7712, 1723.6074, 1805.0789, 2089.9296, 2139.9376, 1891.8347, 2158.9863, 1525.1479, 2132.2023, 2061.2398, 1707.3251, 2202.9106, 2017.0934, 2359.064, 1781.5352, 2081.9002, 2091.391, 2307.257, 1722.3419, 2200.4204, 1752.5609, 2110.2504, 1432.5841, 2121.8681, 1734.5244, 1912.6964, 2134.543, 1996.4716, 1720.3715, 1902.6841, 2402.3462, 1850.2001, 1919.6724, 2123.1945, 2177.1916, 2134.0072, 1969.5795, 1896.8108, 2132.3515, 1451.0973, 1612.5966, 1725.3021, 1581.0971, 2347.5604, 2483.6352, 2035.8553, 2426.3861, 1655.1293, 2382.9981, 2468.8676, 2256.6275, 2087.2235, 2124.0978, 2068.831, 2150.4903, 2262.8365, 2198.5672, 1968.5931, 2418.6407, 1953.0811, 2108.4554, 2361.584, 1979.866, 1741.6303, 2087.9856, 2286.7883, 2395.2738, 2270.3263, 2075.8854, 1874.2124, 2038.3772, 2245.1034, 2075.8632, 1946.548, 1955.202, 2355.13, 2011.3717, 1975.8039, 2099.3788, 2136.0128, 1909.835, 1965.8319, 1829.9619, 1630.4157, 1506.5606, 1897.0851, 2069.4124, 2024.9848),
mod1obs = c(1.13466310792768, 0.931923716274403, 1.48217365869915, 1.21838260146353, 1.79940276419435, 1.76643559356371, 2.45574670199418, 1.85759003858763, 1.52414873587958, 1.56924844587154, 1.75782289196183, 1.21854647206407, 1.67402271470746, 1.57661741696957, 1.54915950383801, 1.16767311295364, 1.51513098169545, 1.58721974127532, 1.48203349385421, 1.25150055063036, 1.55523168986921, 1.16757976105911, 1.06103912158879, 1.38682784393209, 1.26706181742404, 0.922200575100749, 1.15475122512565, 0.919460864722599, 0.622581998594566, 0.888857360932288, 0.561258461668506, 0.784221352878807, 1.05841796267749, 0.954913917366984, 0.864989456398651, 0.405901971360923, 0.437238711188011, 0.238832040897009, 0.398444779604082, 0.596968751447861, 0.780373761619182, 0.593964337126505, 0.674617006647725, 0.803014994811282, 0.914435218814932, 0.832419908437008, 0.870618364283852, 0.66205782789832, 0.668271801470194, 0.582218329504303, 0.551471500972662, 0.546862377936455, 0.782666777798501, 0.797397188682925, 0.233703554128143, 0.33078183414865, 0.477622787992685, 0.788352242940992, 0.655192745268702, 0.629025187982574, 0.606750329361944, 0.688876228035905, 0.811952779578203, 0.878151955673824, 0.666102340741867, 0.234485500963779, 0.377020837708222, 0.431015138976773, 0.599545478659815, 0.485341173599891, 0.54118137004067, 0.822700963102403, 0.680465320458669, 0.750819741408091, 0.914425077741122, 1.03163581838662, 0.750374660256944, 0.994337592683161, 1.00941746422589, 1.07118881485789, 1.2413361662074, 0.862210731330022, 0.807945428753615, 1.01206355371074, 1.1466781183792, 1.04520757430783, 0.906546865827641, 0.802138329712516, 0.927446849752543, 0.909606938810288, 0.726203964235328, 0.63174380758208, 0.827670510708034, 0.796140009519362, 0.908331184943455, 1.24437229369026, 1.01038032593664, 1.20106590210506, 1.47001915216196, 1.4216087908593, 1.19649249116061, 1.28729733432818, 1.050493946874, 0.490028901207541, 0.472897051757391, 0.474117219731324, 0.85500755809614, 1.16164758242536, 1.3257510136531, 1.28575789166575, 1.63477173725642, 2.45069111586777, 2.50502453325512, 1.70995863697504, 2.4729188484128, 2.65244156137921, 2.52288384634894, 2.91133218222026, 2.80631798084833, 2.85068631154525, 2.50623525545215, 2.39950652546403, 2.49930350794263, 2.47640980454363, 2.5743114316923, 2.27093700546959, 1.9784591153946, 1.794185941834, 1.59685624413893, 1.21577767933037, 1.1993875662254, 0.905547156548879, 0.791419465032495, 1.5777113878502, 1.46727507228871, 1.03812304747397, 1.04471285876448, 0.89791346550258, 1.00531560652384, 1.03963310517253, 1.55611837868102, 1.32499035130457, 1.32064172678175, 1.61378461617361, 1.77550053496517, 1.7444038412628, 1.88853641144745, 1.44554726136069, 1.52169806808946, 1.38482876983135, 1.50693746767208, 1.56467290333013, 1.55664813927757, 1.65044541980974, 1.50357779174674, 0.764054807030921, 0.495778285132959, 0.596411488294826, 0.693937433662632, 0.932011439769366, 1.10514738904478, 0.904927677550852, 0.969827490109055, 1.05101670841548, 1.12392075468322, 1.33509689588176, 1.20342821574007, 0.993967001310557, 1.20898145783709, 1.05447146612782, 0.744232955898758, 0.844458068128595, 0.780144262373508, 0.547168299748365, 2.83490197058459, 1.23839568773548, 1.10988758878693, 0.867733363063826, 0.73962833236251, 0.85416901457008, 0.784618799106827, 0.725934558423183, 0.834658973996715, 0.875948198454898, 0.881964176473614, 0.933974735332668, 0.870382237844421, 0.772181200080244, 0.829814527221943, 0.929018949289857, 0.892966747157662, 0.891484338497291, 0.942086202624782, 0.671953671900624, 0.788435092592626, 0.620007838797593, 0.87841213508972, 1.09789753017474, 0.917411139887858, 1.12120717286975)),
row.names = c(2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L, 60L, 61L, 62L, 63L, 64L, 65L, 66L, 67L, 68L, 69L, 70L, 71L, 72L, 73L, 74L, 75L, 76L, 77L, 78L, 79L, 80L, 81L, 82L, 83L, 84L, 85L, 86L, 87L, 88L, 89L, 90L, 91L, 92L, 93L, 94L, 95L, 96L, 97L, 98L, 99L, 100L, 101L, 102L, 103L, 104L, 105L, 106L, 107L, 108L, 109L, 110L, 111L, 112L, 113L, 114L, 115L, 116L, 117L, 118L, 119L, 120L, 121L, 122L, 123L, 124L, 125L, 126L, 127L, 128L, 129L, 130L, 131L, 132L, 133L, 134L, 135L, 136L, 137L, 138L, 139L, 140L, 141L, 142L, 143L, 144L, 145L, 146L, 147L, 148L, 149L, 150L, 151L, 152L, 153L, 154L, 155L, 156L, 158L, 159L, 160L, 161L, 162L, 163L, 164L, 165L, 166L, 167L, 168L, 169L, 170L, 171L, 172L, 173L, 174L, 175L, 176L, 177L, 178L, 179L, 180L, 181L, 182L, 183L, 184L, 185L, 186L, 187L, 188L, 189L, 190L, 191L, 192L, 193L, 194L, 195L, 196L, 197L, 198L, 199L, 200L, 201L, 202L),
class = "data.frame")