How to create a barplot based on a condition in ggplot2 (R)? - r

I have the following code:
set.seed(123)
CIS_data_5 <- data.frame(
CIS$P20,
CIS$P3
)
CIS$P3 <- factor(CIS$P3, labels = c("Mejor", "(NO LEER) Igual", "Peor", "N.S.", "N.C."))
n <- as.numeric(c(CIS$P20))
P20 <- sample(n, 2787, replace = TRUE, prob = NULL)
P20labs <- c("16-29", "30-44", "45-64", ">65", "N.C.")
cut_points <- c(16, 30, 45, 65, Inf)
i <- findInterval(P20, cut_points)
P20_fac <- P20labs[i]
P20_fac[is.na(P20)] <- P20labs[length(P20labs)]
P20_fac <- factor(P20_fac, levels = P20labs)
where P3 has 5 categories, indicating the socioeconomic perception of a sample of Spanish population, and P20_fac has 5 other categories that indicate the age of the respondents.
My desired outcome would be to have 5 joint graphs, in which the socioeconomic situation (P3) is reflected according to the age of the respondents (P20_fac).
I have been stuck on thinking how I can possibly represent this graphically, and after many an hour spent, I have completely run out of ideas (though I think that it can somehow be done by means of facet_wrap() or facet_grid()).
Any help would be much appreciated!
Many thanks in advance!
EDIT
Regarding what is my question, I have already stated that "My desired outcome would be to have 5 (grouped together) graphs, in which the socioeconomic situation (P3) is represented based on the age of the respondents (P20_fac)."
If I knew how to answer it, I most surely would not be making this question on this forum.
UPDATE
The closest that I've got to the desired result is by running the following code:
plot(CIS$P3 ~ P20_fac, data = CIS)
However, it only gets me the graphical results for the first age interval ("16-29") and in vertical instead of horizontal form.
dput(CIS_data_5)
> dput(CIS_data_5)
structure(list(CIS.P20 = structure(c(48L, 33L, 28L, 36L, 32L,
24L, 27L, 50L, 55L, 34L, 48L, 22L, 37L, 24L, 35L, 35L, 19L, 57L,
32L, 45L, 13L, 23L, 24L, 5L, 8L, 34L, 2L, 52L, 66L, 43L, 54L,
24L, 27L, 40L, 40L, 64L, 29L, 12L, 52L, 7L, 26L, 40L, 59L, 19L,
24L, 44L, 19L, 62L, 26L, 16L, 40L, 31L, 14L, 15L, 29L, 24L, 41L,
33L, 41L, 3L, 39L, 4L, 44L, 39L, 1L, 28L, 40L, 8L, 49L, 59L,
50L, 8L, 8L, 9L, 41L, 35L, 35L, 65L, 40L, 38L, 29L, 24L, 54L,
40L, 21L, 29L, 3L, 27L, 45L, 50L, 27L, 50L, 60L, 50L, 41L, 56L,
57L, 33L, 45L, 31L, 58L, 3L, 16L, 43L, 25L, 40L, 32L, 17L, 58L,
46L, 15L, 40L, 22L, 28L, 11L, 38L, 50L, 22L, 25L, 47L, 17L, 32L,
36L, 49L, 40L, 23L, 17L, 34L, 8L, 37L, 67L, 37L, 52L, 60L, 5L,
55L, 45L, 40L, 36L, 46L, 50L, 13L, 73L, 31L, 33L, 47L, 58L, 37L,
55L, 16L, 38L, 32L, 15L, 45L, 33L, 9L, 47L, 10L, 9L, 33L, 41L,
31L, 31L, 21L, 28L, 26L, 68L, 15L, 63L, 38L, 59L, 30L, 47L, 21L,
36L, 50L, 40L, 30L, 14L, 54L, 16L, 60L, 8L, 44L, 49L, 68L, 33L,
24L, 58L, 27L, 34L, 33L, 47L, 50L, 52L, 15L, 21L, 16L, 47L, 59L,
37L, 34L, 11L, 37L, 50L, 9L, 41L, 51L, 31L, 35L, 26L, 5L, 24L,
51L, 6L, 27L, 52L, 68L, 8L, 39L, 33L, 51L, 41L, 53L, 39L, 60L,
43L, 10L, 51L, 26L, 41L, 51L, 54L, 35L, 33L, 35L, 39L, 28L, 48L,
5L, 31L, 50L, 8L, 45L, 50L, 41L, 53L, 54L, 32L, 43L, 65L, 22L,
32L, 24L, 35L, 40L, 34L, 41L, 19L, 48L, 46L, 35L, 24L, 12L, 35L,
19L, 8L, 30L, 58L, 55L, 16L, 37L, 17L, 1L, 35L, 40L, 50L, 50L,
53L, 50L, 42L, 37L, 29L, 54L, 4L, 29L, 47L, 51L, 58L, 10L, 29L,
36L, 16L, 9L, 54L, 36L, 19L, 49L, 59L, 39L, 6L, 35L, 25L, 52L,
3L, 47L, 52L, 51L, 26L, 65L, 23L, 35L, 45L, 55L, 45L, 9L, 19L,
32L, 49L, 47L, 45L, 7L, 31L, 24L, 7L, 21L, 45L, 52L, 57L, 37L,
56L, 39L, 26L, 46L, 35L, 35L, 4L, 20L, 21L, 45L, 23L, 20L, 41L,
23L, 33L, 33L, 32L, 60L, 43L, 27L, 56L, 38L, 44L, 35L, 14L, 58L,
50L, 29L, 25L, 15L, 10L, 57L, 61L, 46L, 43L, 23L, 29L, 57L, 24L,
32L, 36L, 33L, 34L, 31L, 60L, 4L, 42L, 43L, 41L, 51L, 29L, 10L,
29L, 48L, 29L, 37L, 62L, 17L, 37L, 45L, 17L, 47L, 53L, 10L, 35L,
8L, 25L, 32L, 51L, 46L, 39L, 46L, 48L, 4L, 35L, 34L, 37L, 3L,
6L, 74L, 13L, 60L, 36L, 1L, 33L, 13L, 23L, 42L, 60L, 39L, 66L,
49L, 50L, 57L, 41L, 16L, 23L, 28L, 6L, 45L, 53L, 21L, 26L, 40L,
21L, 34L, 37L, 6L, 50L, 48L, 28L, 62L, 50L, 48L, 28L, 14L, 31L,
44L, 17L, 26L, 46L, 17L, 11L, 55L, 24L, 33L, 30L, 52L, 22L, 40L,
43L, 29L, 36L, 60L, 5L, 45L, 33L, 44L, 35L, 32L, 19L, 51L, 19L,
29L, 24L, 35L, 28L, 42L, 6L, 42L, 63L, 19L, 11L, 56L, 48L, 8L,
12L, 30L, 12L, 48L, 56L, 20L, 54L, 32L, 64L, 42L, 44L, 28L, 26L,
42L, 48L, 33L, 46L, 46L, 23L, 28L, 64L, 25L, 19L, 26L, 3L, 31L,
49L, 40L, 65L, 28L, 20L, 47L, 24L, 71L, 13L, 34L, 48L, 27L, 43L,
1L, 28L, 56L, 24L, 25L, 29L, 43L, 54L, 55L, 29L, 38L, 56L, 27L,
72L, 47L, 25L, 34L, 23L, 15L, 37L, 18L, 55L, 49L, 39L, 32L, 54L,
14L, 34L, 14L, 21L, 3L, 56L, 24L, 32L, 38L, 50L, 68L, 44L, 12L,
30L, 29L, 58L, 31L, 51L, 33L, 38L, 48L, 27L, 55L, 62L, 12L, 29L,
71L, 38L, 61L, 48L, 45L, 55L, 23L, 58L, 37L, 55L, 5L, 51L, 20L,
10L, 30L, 60L, 39L, 40L, 21L, 44L, 16L, 25L, 15L, 47L, 18L, 53L,
48L, 54L, 36L, 62L, 33L, 15L, 16L, 59L, 15L, 37L, 33L, 61L, 17L,
17L, 57L, 54L, 22L, 56L, 42L, 22L, 27L, 15L, 27L, 64L, 31L, 40L,
68L, 25L, 54L, 55L, 34L, 50L, 39L, 23L, 67L, 38L, 44L, 52L, 20L,
21L, 18L, 45L, 50L, 15L, 48L, 24L, 25L, 10L, 32L, 67L, 28L, 41L,
61L, 58L, 36L, 63L, 27L, 65L, 64L, 30L, 51L, 25L, 42L, 34L, 49L,
44L, 38L, 47L, 33L, 46L, 62L, 46L, 45L, 46L, 12L, 27L, 21L, 22L,
17L, 12L, 49L, 55L, 39L, 37L, 37L, 59L, 35L, 18L, 11L, 48L, 32L,
4L, 25L, 42L, 42L, 52L, 36L, 74L, 51L, 35L, 66L, 36L, 60L, 31L,
44L, 58L, 10L, 22L, 53L, 49L, 51L, 38L, 42L, 41L, 25L, 50L, 49L,
39L, 51L, 35L, 60L, 30L, 51L, 54L, 50L, 51L, 54L, 13L, 34L, 34L,
54L, 26L, 26L, 43L, 20L, 30L, 35L, 43L, 33L, 4L, 46L, 36L, 35L,
2L, 36L, 41L, 45L, 16L, 33L, 59L, 64L, 19L, 15L, 29L, 6L, 57L,
50L, 59L, 52L, 24L, 49L, 9L, 44L, 39L, 56L, 31L, 50L, 50L, 29L,
11L, 69L, 29L, 40L, 50L, 15L, 55L, 31L, 58L, 13L, 36L, 18L, 21L,
46L, 27L, 58L, 75L, 28L, 22L, 61L, 2L, 46L, 27L, 23L, 48L, 54L,
34L, 44L, 50L, 40L, 18L, 27L, 28L, 40L, 35L, 50L, 39L, 34L, 51L,
28L, 39L, 30L, 65L, 5L, 12L, 27L, 48L, 38L, 54L, 41L, 37L, 31L,
41L, 35L, 1L, 59L, 65L, 49L, 60L, 1L, 46L, 7L, 55L, 42L, 18L,
11L, 32L, 31L, 50L, 52L, 50L, 43L, 45L, 36L, 35L, 10L, 24L, 53L,
19L, 43L, 40L, 38L, 28L, 27L, 46L, 55L, 29L, 37L, 53L, 53L, 55L,
39L, 38L, 46L, 25L, 11L, 17L, 42L, 31L, 33L, 17L, 8L, 15L, 39L,
39L, 47L, 25L, 46L, 37L, 37L, 40L, 62L, 42L, 25L, 21L, 30L, 18L,
54L, 8L, 20L, 32L, 38L, 41L, 16L, 19L, 75L, 19L, 38L, 63L, 23L,
49L, 22L, 32L, 35L, 62L, 38L, 36L, 31L, 31L, 23L, 24L, 52L, 25L,
27L, 36L, 39L, 11L, 16L, 45L, 61L, 8L, 38L, 50L, 18L, 52L, 4L,
53L, 24L, 13L, 34L, 39L, 47L, 59L, 22L, 39L, 9L, 29L, 20L, 73L,
55L, 71L, 34L, 55L, 52L, 35L, 50L, 58L, 37L, 3L, 20L, 3L, 57L,
51L, 29L, 10L, 30L, 46L, 7L, 47L, 64L, 58L, 60L, 47L, 39L, 43L,
46L, 62L, 59L, 38L, 34L, 32L, 28L, 59L, 39L, 25L, 15L, 56L, 37L,
12L, 48L, 51L, 43L, 59L, 19L, 10L, 50L, 42L, 19L, 39L, 58L, 24L,
49L, 27L, 9L, 15L, 16L, 39L, 11L, 49L, 29L, 30L, 30L, 47L, 57L,
59L, 49L, 20L, 51L, 43L, 46L, 19L, 51L, 47L, 47L, 25L, 5L, 52L,
40L, 21L, 36L, 6L, 64L, 35L, 29L, 40L, 63L, 26L, 39L, 39L, 39L,
51L, 43L, 27L, 41L, 19L, 49L, 52L, 61L, 50L, 37L, 42L, 50L, 47L,
41L, 32L, 26L, 23L, 37L, 52L, 27L, 60L, 29L, 22L, 37L, 38L, 15L,
37L, 24L, 21L, 50L, 60L, 36L, 39L, 48L, 59L, 46L, 73L, 35L, 31L,
34L, 25L, 75L, 41L, 20L, 36L, 55L, 10L, 39L, 44L, 62L, 40L, 14L,
29L, 49L, 45L, 59L, 54L, 52L, 41L, 40L, 35L, 36L, 32L, 2L, 51L,
24L, 49L, 22L, 49L, 57L, 28L, 12L, 35L, 5L, 31L, 7L, 6L, 51L,
33L, 24L, 6L, 51L, 25L, 10L, 52L, 54L, 75L, 12L, 55L, 42L, 17L,
15L, 39L, 52L, 29L, 6L, 11L, 59L, 40L, 36L, 17L, 14L, 27L, 14L,
56L, 33L, 59L, 55L, 31L, 31L, 27L, 29L, 39L, 7L, 20L, 50L, 8L,
2L, 31L, 59L, 39L, 71L, 24L, 32L, 8L, 60L, 24L, 21L, 37L, 17L,
24L, 42L, 33L, 25L, 32L, 12L, 38L, 46L, 31L, 40L, 37L, 12L, 19L,
21L, 44L, 20L, 30L, 38L, 45L, 2L, 30L, 60L, 15L, 23L, 34L, 6L,
35L, 23L, 25L, 31L, 18L, 42L, 59L, 40L, 42L, 16L, 46L, 71L, 21L,
36L, 18L, 37L, 38L, 31L, 65L, 34L, 55L, 59L, 34L, 66L, 29L, 31L,
7L, 8L, 58L, 14L, 23L, 65L, 29L, 16L, 52L, 35L, 7L, 20L, 14L,
15L, 46L, 9L, 61L, 45L, 26L, 27L, 59L, 26L, 38L, 29L, 14L, 32L,
47L, 36L, 40L, 57L, 39L, 31L, 25L, 34L, 46L, 39L, 59L, 11L, 26L,
21L, 9L, 13L, 47L, 37L, 43L, 45L, 19L, 52L, 42L, 53L, 26L, 49L,
37L, 33L, 50L, 28L, 25L, 35L, 6L, 55L, 25L, 11L, 16L, 53L, 32L,
20L, 20L, 41L, 55L, 22L, 12L, 50L, 13L, 71L, 22L, 5L, 56L, 33L,
44L, 57L, 52L, 53L, 44L, 18L, 32L, 60L, 59L, 24L, 48L, 29L, 45L,
16L, 53L, 55L, 13L, 41L, 6L, 72L, 30L, 22L, 31L, 32L, 8L, 41L,
34L, 4L, 1L, 34L, 9L, 20L, 33L, 67L, 9L, 48L, 72L, 31L, 40L,
38L, 56L, 50L, 45L, 63L, 18L, 31L, 11L, 35L, 51L, 55L, 57L, 21L,
48L, 50L, 28L, 55L, 39L, 33L, 2L, 35L, 6L, 30L, 25L, 18L, 17L,
30L, 40L, 38L, 31L, 46L, 69L, 36L, 14L, 35L, 42L, 32L, 66L, 55L,
25L, 13L, 76L, 54L, 57L, 24L, 17L, 24L, 35L, 32L, 27L, 56L, 5L,
54L, 30L, 53L, 62L, 41L, 38L, 30L, 2L, 69L, 35L, 46L, 44L, 36L,
33L, 30L, 57L, 61L, 42L, 63L, 30L, 44L, 34L, 17L, 53L, 53L, 15L,
46L, 58L, 43L, 21L, 36L, 27L, 21L, 32L, 28L, 10L, 15L, 8L, 53L,
63L, 43L, 10L, 64L, 41L, 58L, 7L, 36L, 26L, 19L, 26L, 52L, 14L,
36L, 4L, 49L, 20L, 42L, 35L, 9L, 23L, 57L, 5L, 43L, 40L, 31L,
26L, 49L, 31L, 41L, 3L, 13L, 45L, 29L, 42L, 41L, 57L, 67L, 37L,
50L, 25L, 47L, 25L, 52L, 60L, 38L, 49L, 40L, 43L, 45L, 45L, 42L,
50L, 63L, 10L, 42L, 31L, 26L, 26L, 60L, 54L, 9L, 3L, 57L, 63L,
44L, 45L, 31L, 61L, 48L, 3L, 55L, 41L, 40L, 32L, 22L, 23L, 37L,
39L, 30L, 6L, 25L, 2L, 41L, 3L, 15L, 35L, 50L, 48L, 17L, 30L,
55L, 40L, 24L, 39L, 47L, 49L, 48L, 16L, 14L, 5L, 31L, 29L, 32L,
60L, 34L, 55L, 58L, 51L, 30L, 48L, 47L, 52L, 30L, 35L, 65L, 7L,
54L, 9L, 23L, 59L, 25L, 19L, 47L, 38L, 56L, 73L, 70L, 47L, 64L,
43L, 30L, 40L, 45L, 3L, 40L, 35L, 50L, 10L, 35L, 49L, 33L, 28L,
8L, 6L, 60L, 37L, 46L, 65L, 48L, 47L, 27L, 23L, 13L, 39L, 40L,
44L, 55L, 35L, 21L, 21L, 51L, 33L, 49L, 11L, 25L, 25L, 44L, 6L,
25L, 30L, 22L, 45L, 31L, 23L, 56L, 37L, 47L, 20L, 25L, 48L, 23L,
58L, 47L, 46L, 45L, 31L, 28L, 13L, 68L, 18L, 42L, 22L, 33L, 33L,
11L, 47L, 50L, 21L, 2L, 22L, 48L, 38L, 39L, 32L, 29L, 52L, 10L,
35L, 45L, 43L, 2L, 41L, 23L, 3L, 41L, 29L, 39L, 44L, 11L, 44L,
68L, 36L, 27L, 27L, 54L, 44L, 39L, 58L, 24L, 19L, 57L, 22L, 16L,
64L, 25L, 3L, 60L, 51L, 46L, 50L, 32L, 6L, 52L, 43L, 59L, 36L,
36L, 7L, 14L, 49L, 22L, 69L, 40L, 51L, 56L, 44L, 10L, 30L, 46L,
53L, 30L, 17L, 32L, 37L, 36L, 9L, 41L, 2L, 34L, 57L, 27L, 25L,
25L, 55L, 48L, 40L, 36L, 25L, 36L, 7L, 41L, 44L, 22L, 55L, 38L,
31L, 49L, 62L, 15L, 48L, 13L, 23L, 20L, 48L, 21L, 50L, 33L, 23L,
67L, 30L, 39L, 34L, 41L, 32L, 43L, 43L, 26L, 24L, 27L, 10L, 20L,
77L, 22L, 56L, 54L, 67L, 52L, 14L, 38L, 24L, 50L, 33L, 39L, 40L,
67L, 33L, 44L, 51L, 72L, 39L, 47L, 9L, 43L, 54L, 5L, 35L, 52L,
16L, 37L, 45L, 22L, 27L, 35L, 61L, 51L, 2L, 59L, 49L, 45L, 22L,
44L, 31L, 49L, 35L, 22L, 35L, 35L, 44L, 48L, 38L, 39L, 41L, 59L,
27L, 47L, 25L, 28L, 8L, 40L, 37L, 50L, 75L, 16L, 39L, 42L, 52L,
31L, 36L, 43L, 19L, 66L, 21L, 22L, 64L, 38L, 26L, 43L, 64L, 56L,
1L, 3L, 27L, 28L, 10L, 33L, 35L, 38L, 30L, 38L, 39L, 57L, 24L,
55L, 23L, 42L, 35L, 5L, 53L, 71L, 40L, 12L, 37L, 37L, 8L, 7L,
24L, 37L, 31L, 59L, 44L, 50L, 50L, 63L, 42L, 24L, 61L, 2L, 56L,
36L, 48L, 41L, 27L, 10L, 16L, 71L, 33L, 24L, 40L, 16L, 45L, 64L,
45L, 59L, 12L, 26L, 42L, 54L, 52L, 37L, 53L, 43L, 47L, 8L, 51L,
43L, 59L, 14L, 44L, 48L, 3L, 51L, 27L, 3L, 48L, 57L, 31L, 23L,
17L, 52L, 11L, 62L, 39L, 40L, 57L, 43L, 53L, 38L, 31L, 14L, 53L,
43L, 39L, 20L, 56L, 31L, 11L, 37L, 31L, 49L, 36L, 35L, 50L, 19L,
47L, 28L, 53L, 30L, 17L, 27L, 35L, 48L, 34L, 32L, 36L, 23L, 17L,
14L, 24L, 56L, 28L, 58L, 40L, 48L, 32L, 28L, 32L, 21L, 24L, 43L,
44L, 67L, 18L, 63L, 30L, 31L, 29L, 57L, 44L, 35L, 40L, 30L, 32L,
16L, 45L, 27L, 36L, 28L, 23L, 38L, 35L, 30L, 43L, 59L, 57L, 50L,
23L, 16L, 64L, 44L, 50L, 43L, 40L, 21L, 41L, 59L, 22L, 32L, 15L,
22L, 49L, 60L, 29L, 44L, 58L, 40L, 24L, 23L, 34L, 25L, 41L, 14L,
16L, 32L, 37L, 51L, 45L, 54L, 40L, 43L, 24L, 56L, 67L, 47L, 48L,
41L, 47L, 26L, 25L, 25L, 57L, 66L, 47L, 4L, 32L, 48L, 26L, 2L,
24L, 40L, 43L, 30L, 34L, 30L, 29L, 53L, 22L, 19L, 31L, 58L, 3L,
18L, 30L, 31L, 35L, 64L, 59L, 17L, 4L, 42L, 32L, 75L, 31L, 13L,
59L, 12L, 31L, 49L, 34L, 13L, 30L, 1L, 16L, 34L, 35L, 17L, 19L,
50L, 52L, 25L, 33L, 50L, 10L, 23L, 22L, 47L, 43L, 54L, 5L, 41L,
28L, 13L, 46L, 44L, 9L, 24L, 22L, 33L, 58L, 17L, 59L, 29L, 44L,
34L, 10L, 30L, 24L, 15L, 40L, 42L, 5L, 39L, 34L, 28L, 6L, 40L,
44L, 25L, 50L, 57L, 40L, 53L, 37L, 41L, 48L, 48L, 23L, 55L, 20L,
8L, 41L, 46L, 27L, 39L, 51L, 20L, 36L, 45L, 33L, 71L, 3L, 15L,
49L, 21L, 34L, 5L, 42L, 2L, 40L, 35L, 13L, 13L, 2L, 42L, 17L,
7L, 53L, 30L, 27L, 11L, 37L, 49L, 23L, 45L, 32L, 30L, 16L, 21L,
32L, 20L, 40L, 29L, 3L, 46L, 34L, 56L, 46L, 21L, 46L, 25L, 14L,
15L, 15L, 53L, 31L, 27L, 34L, 67L, 29L, 62L, 37L, 51L, 36L, 32L,
10L, 50L, 44L, 43L, 40L, 34L, 26L, 62L, 24L, 63L, 50L, 13L, 42L,
43L, 49L, 29L, 30L, 9L, 40L, 11L, 61L, 67L, 37L, 20L, 49L, 66L,
49L, 34L, 25L, 30L, 58L, 45L, 15L, 18L, 32L, 29L, 17L, 18L, 43L,
72L, 54L, 40L, 29L, 18L, 39L, 35L, 16L, 37L, 18L, 1L, 43L, 22L,
38L, 57L, 23L, 25L, 26L, 46L, 25L, 15L, 36L, 26L, 32L, 15L, 57L,
33L, 25L, 63L, 35L, 44L, 25L, 13L, 26L, 56L, 30L, 39L, 35L, 39L,
20L, 47L, 44L, 22L, 45L, 23L, 7L, 30L, 47L, 7L, 54L, 47L, 5L,
52L, 21L, 72L, 23L, 41L, 43L, 30L, 28L, 32L, 43L, 35L, 66L, 70L,
37L, 44L, 33L, 11L, 51L, 26L, 48L, 41L, 10L, 50L, 31L, 39L, 41L,
43L, 20L, 27L, 29L, 17L, 47L, 25L, 48L, 15L, 49L, 50L, 34L, 16L,
26L, 32L, 27L, 71L, 41L, 30L, 34L, 36L, 47L, 32L, 17L, 19L, 33L,
26L, 17L, 25L, 22L, 35L, 42L, 25L, 37L, 6L, 46L, 3L, 53L, 39L,
29L, 10L, 41L, 45L, 10L, 31L, 43L, 35L, 43L, 30L, 6L, 12L, 28L,
18L, 40L, 35L, 37L, 23L, 29L, 8L, 32L, 55L, 53L, 12L, 16L, 35L,
23L, 27L, 51L, 32L, 36L, 9L, 43L, 26L, 28L, 36L, 46L, 56L, 21L,
21L, 19L, 16L, 21L, 16L, 8L, 12L, 44L, 4L, 21L, 26L, 66L, 64L,
9L, 38L, 34L, 50L, 20L, 36L, 37L, 31L, 55L, 37L, 60L, 20L, 61L,
28L, 65L, 44L, 33L, 50L, 53L, 34L, 43L, 27L, 17L, 30L, 55L, 20L,
43L, 46L, 50L, 44L, 27L, 44L, 43L, 23L, 26L, 52L, 23L, 52L, 27L,
40L, 17L, 50L, 8L, 21L, 36L, 68L, 19L, 26L, 45L, 47L, 30L, 27L,
42L, 22L, 38L, 36L, 16L, 15L, 27L, 47L, 26L, 51L, 17L, 54L, 60L,
38L, 23L, 19L, 30L, 44L, 42L, 7L, 33L, 56L, 14L, 45L, 54L, 47L,
64L, 64L, 18L, 55L, 67L, 36L, 51L, 30L, 12L, 59L, 33L, 12L, 33L,
17L, 13L, 44L, 24L, 46L, 41L, 25L, 41L, 33L, 27L, 1L, 14L, 29L,
29L, 29L, 8L, 48L, 22L, 39L, 59L, 36L, 34L, 37L, 13L, 38L, 32L,
22L, 3L, 30L, 21L, 51L, 38L, 4L, 8L, 50L, 38L, 30L, 31L, 22L,
37L, 62L, 46L, 45L, 26L, 13L, 51L, 54L, 38L, 13L, 22L, 25L, 28L,
22L, 33L, 24L, 22L, 52L, 26L, 11L, 46L, 18L, 27L, 32L, 56L, 2L,
36L, 13L, 71L, 56L, 43L, 37L, 5L, 63L, 34L, 51L, 34L, 38L, 29L,
35L, 44L, 68L, 18L, 30L, 41L, 50L, 58L, 48L, 43L, 16L, 33L, 43L,
46L, 61L, 29L, 46L, 73L, 8L, 21L, 19L, 12L, 35L, 25L, 21L, 34L,
44L, 52L, 3L, 36L, 5L, 51L, 9L, 50L, 60L, 24L, 3L, 31L, 13L,
36L, 42L, 67L, 44L, 18L, 8L, 20L, 60L, 38L, 69L, 65L, 23L, 44L,
52L, 45L, 29L, 51L, 22L, 23L, 27L, 58L, 61L, 32L, 33L, 58L, 31L,
30L, 50L, 64L, 24L, 4L, 45L, 43L, 46L, 53L, 35L, 51L, 51L, 45L,
37L, 30L, 2L, 58L, 41L, 35L, 30L, 35L, 40L, 33L, 60L, 60L, 55L,
37L, 27L, 10L, 21L, 27L, 17L, 33L, 46L, 28L, 36L, 53L, 34L, 26L,
36L, 59L, 9L, 30L, 55L, 45L, 57L, 17L, 19L, 2L, 31L, 41L, 26L,
29L, 16L, 47L, 52L, 44L, 29L, 61L, 25L, 55L, 8L, 5L, 56L, 45L,
51L, 25L, 11L, 26L, 35L, 33L, 41L, 71L, 35L, 63L, 38L, 32L, 53L,
31L, 52L, 44L, 34L, 50L, 40L, 21L, 36L, 76L, 40L, 23L, 42L, 25L,
14L, 58L, 38L, 37L, 69L, 46L, 46L, 49L, 42L, 28L, 55L, 13L, 45L,
12L, 42L, 32L, 11L, 32L, 36L, 61L, 19L, 28L, 31L, 35L, 22L, 15L,
50L, 64L, 32L, 59L, 33L, 24L, 19L, 49L, 36L, 26L, 57L, 57L, 34L,
29L, 7L, 30L, 42L, 47L, 53L, 55L, 55L, 35L, 26L, 20L, 55L, 61L,
43L, 31L, 30L, 52L, 27L, 63L, 25L, 47L, 63L, 23L, 2L, 47L, 12L,
37L, 34L, 46L, 30L, 62L, 52L, 27L, 3L, 59L, 50L, 5L, 34L, 23L
), levels = c("16", "17", "18", "19", "20", "21", "22", "23",
"24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34",
"35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45",
"46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56",
"57", "58", "59", "60", "61", "62", "63", "64", "65", "66", "67",
"68", "69", "70", "71", "72", "73", "74", "75", "76", "77", "78",
"79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89",
"90", "91", "93", "N.C. "), class = "factor"), CIS.P3 = structure(c(3L,
3L, 3L, 3L, 3L, 2L, 1L, 3L, 2L, 3L, 3L, 3L, 3L, 1L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 1L, 2L,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 1L, 2L, 2L, 3L, 3L, 1L, 2L, 2L, 3L,
3L, 3L, 3L, 3L, 1L, 3L, 3L, 3L, 2L, 3L, 2L, 2L, 2L, 3L, 2L, 3L,
3L, 3L, 2L, 3L, 2L, 2L, 3L, 3L, 2L, 2L, 2L, 3L, 2L, 3L, 3L, 2L,
1L, 3L, 3L, 2L, 3L, 3L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 1L, 3L, 3L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L,
3L, 3L, 3L, 3L, 2L, 3L, 3L, 1L, 1L, 2L, 2L, 3L, 2L, 2L, 1L, 2L,
2L, 3L, 3L, 2L, 2L, 3L, 3L, 2L, 3L, 1L, 2L, 3L, 3L, 3L, 3L, 1L,
2L, 2L, 3L, 3L, 3L, 2L, 3L, 3L, 3L, 2L, 3L, 3L, 2L, 4L, 3L, 3L,
2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 3L, 3L, 1L, 3L,
3L, 3L, 2L, 2L, 1L, 2L, 2L, 3L, 1L, 3L, 2L, 2L, 3L, 1L, 3L, 2L,
2L, 3L, 3L, 3L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 3L, 1L, 3L, 2L, 3L,
3L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 2L, 1L, 3L, 1L, 2L, 2L, 2L, 2L,
2L, 1L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 1L,
1L, 1L), levels = c("Mejor", "(NO LEER) Igual", "Peor", "N.S.",
"N.C."), class = "factor")), class = "data.frame", row.names = c(NA,
-2787L))

Maybe you want something like this using facet_wrap:
set.seed(123)
CIS_data_5 <- data.frame(
CIS$CIS.P20,
CIS$CIS.P3
)
CIS$P3 <- factor(CIS$CIS.P3, labels = c("Mejor", "(NO LEER) Igual", "Peor", "N.S.", "N.C."))
n <- as.numeric(c(CIS$CIS.P20))
P20 <- sample(n, 2787, replace = TRUE, prob = NULL)
P20labs <- c("16-29", "30-44", "45-64", ">65", "N.C.")
cut_points <- c(16, 30, 45, 65, Inf)
i <- findInterval(P20, cut_points)
P20_fac <- P20labs[i]
P20_fac[is.na(P20)] <- P20labs[length(P20labs)]
P20_fac <- factor(P20_fac, levels = P20labs)
library(dplyr)
library(ggplot2)
data.frame(P3 = CIS$P3,
P20_fac = P20_fac) %>%
ggplot(aes(x = P20_fac)) +
geom_bar() +
coord_flip() +
facet_wrap(~P3)
Created on 2022-09-25 with reprex v2.0.2

Related

add correlation coefficient and CI values in a boxplot in R

I am trying to make a boxplot with the correlation coefficient and their CI values in a boxplot in R. However when I try to add the following line of code it does not work nor gives it a error code.
boxplot(AI~Q8,
data=df6,
main="The relation between Q8 and the A",
xlab="A",
ylab="B",
col="orange",
border="brown",
stat_cor()
)
or
boxplot(AI~Q8,
data=df6,
main="The relation between Q8 and the A",
xlab="A",
ylab="B",
col="orange",
border="brown",
) + stat_cor()
And is it also possible to get a different color for each boxplot?
This is the dataset I use
structure(list(AI = c(0.659967433444017, 0.941802575478176, 0.565824387077681,
0.733813835498287, 0.502486567259441, 0.581214986043292, 0.190601573198807,
0.61511194322592, 0.630316833066587, 0.513634604352834, 0.691766098799664,
0.443331648025045, 0.475498746385683, 0.253183014637901, 0.685340877692643,
1.07517098753888, 1.10219598244924, 0.469261733415629, 0.473732071653954,
1.11472099520751, 1.12140547685593, 0.844644528419478, 0.741480217894283,
0.664326042816726, 0.798610418245564, 0.184662871961999, 0.718894350907626,
0.773529667226157, 0.65593386028412, 0.958431317152659, 0.638301596431948,
0.844678953483002, 1.12048871720509, 1.21583405287684, 0.642731559824528,
0.720376072993178, 0.53551579775883, 0.612011376983417, 0.281431655977777,
1.25176573918925, 1.15328375538737, 1.20957695840318, 1.09278028083012,
0.859636858723266, 1.20869790596587, 1.12288309438874, 0.891306451574103,
0.552552119084953, 0.995259412720299, 1.17674596084747), Q8 = c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L,
3L, 3L, 3L, 2L, 2L, 2L, 4L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L)), 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, 33L, 34L, 36L, 37L, 38L,
39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L,
52L), class = "data.frame")
You can add correlation in a legend, colors may be specified as vectors.
clr <- hcl.colors(length(unique(df6$Q8)), alpha=.5)
blr <- hcl.colors(length(unique(df6$Q8)), alpha=1)
boxplot(AI ~ Q8,data=df6, main="The relation between Q8 and the A",
xlab="A", ylab="B", col=clr, border=blr)
corr <- with(df6, cor.test(AI, Q8))[c('estimate', 'conf.int')]
legend('topleft',
legend=bquote(rho ==.(signif(corr[[1]], 2))~'['*.(signif(corr[[2]][1], 2))*','~.(signif(corr[[2]][2], 2))*']'),
bty='n')

Creating a custom legend with a subset of data ggplot [duplicate]

This question already has an answer here:
Remove legend entries for some factors levels
(1 answer)
Closed 3 years ago.
I am creating some stacked barplots in which there are about 50 different factors. I would like a legend that just maps the colors for the 8 most abundant factors.
I first calculated the mean abundance. Then I have ordered the factor by this. And can also get the corresponding colors, but I don't know if I should 'independently' create a legend, or if I am missing another 'easy' way with ggplot.
Any help appreciated.
thanks
## calculating the OTUs mean abundance
## instead of labelling all 50 in the legend just label top 5 or 10 for label
meanAbund<-as.data.frame(colSums(dat.grouped[,grep("Otu",colnames(dat.grouped))]))
meanAbund$Name<-as.character(tax_tab1[match(rownames(meanAbund),rownames(tax_tab1)),ncol(tax_tab1)])
colnames(meanAbund)<-c("mean_abund","Name")
top8_meanAbund<-meanAbund[rev(order(meanAbund$mean_abund)),][1:8]
# use melted dataframe for plotting
## now ordered by mean abundance
dat.groupedM$Level<-factor(dat.groupedM$Level,meanAbund[rev(order(meanAbund$mean_abund)),"Name"])
#top8 (mean is greater than 0.1 %)
# top 8 colors
library(RColorBrewer)
n <- length(unique(dat.groupedM$Level))
# generate divergent color palete, cause so many colors
qual_col_pals = brewer.pal.info[brewer.pal.info$category == 'qual',]
col_vector = unlist(mapply(brewer.pal, qual_col_pals$maxcolors, rownames(qual_col_pals)))
set.seed(200)
mycols<-sample(col_vector, n)
top8cols<-mycols[1:8]
#pie(rep(1,n), mycols)
ggplot(dat.groupedM, aes(x = Time, y = value, fill = Level))+
geom_bar(stat = "identity") +
facet_wrap(~Diet)+
scale_fill_manual(values = mycols) +
theme_bw()+
theme(legend.position = "none")+
ggtitle(paste(level,"composition - Cohort 1"))
thanks getting closer now I have this, see below. But it isn't using my divergent palette "mycols" which I had specified with scale_fill_manual before now I changed to this as per suggestion. Okay now this is correct !!
scale_fill_manual(values = mycols,breaks = top10_meanAbund$Name)+
> dput(dat.groupedM)
structure(list(Diet = structure(c(2L, 2L, 3L, 3L, 2L, 2L, 3L,
3L, 2L, 2L, 3L, 3L, 2L, 2L, 3L, 3L, 2L, 2L, 3L, 3L, 2L, 2L, 3L,
3L, 2L, 2L, 3L, 3L, 2L, 2L, 3L, 3L, 2L, 2L, 3L, 3L, 2L, 2L, 3L,
3L, 2L, 2L, 3L, 3L, 2L, 2L, 3L, 3L, 2L, 2L, 3L, 3L, 2L, 2L, 3L,
3L, 2L, 2L, 3L, 3L, 2L, 2L, 3L, 3L, 2L, 2L, 3L, 3L, 2L, 2L, 3L,
3L, 2L, 2L, 3L, 3L, 2L, 2L, 3L, 3L, 2L, 2L, 3L, 3L, 2L, 2L, 3L,
3L, 2L, 2L, 3L, 3L, 2L, 2L, 3L, 3L, 2L, 2L, 3L, 3L, 2L, 2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L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L,
3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L,
3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L,
3L, 2L, 3L, 2L, 3L, 2L, 3L), .Label = c("Control", "ZT2", "ZT14"
), class = "factor"), variable = structure(c(1L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L,
6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L,
10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 13L,
13L, 13L, 13L, 14L, 14L, 14L, 14L, 15L, 15L, 15L, 15L, 16L, 16L,
16L, 16L, 17L, 17L, 17L, 17L, 18L, 18L, 18L, 18L, 19L, 19L, 19L,
19L, 20L, 20L, 20L, 20L, 21L, 21L, 21L, 21L, 22L, 22L, 22L, 22L,
23L, 23L, 23L, 23L, 24L, 24L, 24L, 24L, 25L, 25L, 25L, 25L, 26L,
26L, 26L, 26L, 27L, 27L, 27L, 27L, 28L, 28L, 28L, 28L, 29L, 29L,
29L, 29L, 30L, 30L, 30L, 30L, 31L, 31L, 31L, 31L, 32L, 32L, 32L,
32L, 33L, 33L, 33L, 33L, 34L, 34L, 34L, 34L, 35L, 35L, 35L, 35L,
36L, 36L, 36L, 36L, 37L, 37L, 37L, 37L, 38L, 38L, 38L, 38L, 39L,
39L, 39L, 39L, 40L, 40L, 40L, 40L, 41L, 41L, 41L, 41L, 42L, 42L,
42L, 42L, 43L, 43L, 43L, 43L, 44L, 44L, 44L, 44L, 45L, 45L, 45L,
45L, 46L, 46L, 46L, 46L, 47L, 47L, 47L, 47L, 48L, 48L, 48L, 48L,
49L, 49L, 49L, 49L, 50L, 50L, 50L, 50L), .Label = c("Otu1", "Otu108",
"Otu11", "Otu112", "Otu136", "Otu14", "Otu155", "Otu157", "Otu158",
"Otu161", "Otu182", "Otu186", "Otu197", "Otu21", "Otu23", "Otu242",
"Otu244", "Otu247", "Otu27", "Otu29", "Otu30", "Otu314", "Otu35",
"Otu36", "Otu378", "Otu4", "Otu40", "Otu42", "Otu427", "Otu44",
"Otu464", "Otu49", "Otu53", "Otu535", "Otu56", "Otu585", "Otu607",
"Otu623", "Otu629", "Otu645", "Otu648", "Otu664", "Otu69", "Otu715",
"Otu730", "Otu741", "Otu76", "Otu77", "Otu86", "Otu90"), class = "factor"),
value = c(0.125797907894198, 0.236708006778536, 0.577822547980787,
0.590703405992419, 0.010616635104027, 0.0101758392492056,
0.000833768625886666, 0.00075132991625995, 3.6675879491077e-05,
4.75000300636826e-05, 1.80354502712558e-05, 7.12272349413578e-06,
0.0923297961192989, 0.0516121598003722, 0.00943059557285678,
0.0071906839719107, 0.00539229454875785, 0.00506484304043506,
0.00338347504700533, 0.0025680210124108, 0.00175591025013762,
0.00395104856215105, 0.0561412864297836, 0.0389581641347799,
0.000170273382201598, 0.00010844375756416, 2.43702775724318e-05,
2.69013349522259e-05, 0.00832912099453767, 0.010385453199374,
0.000178806881780292, 0.000192961017676232, 0.000133411286157921,
0.000160277350764036, 3.61247330219488e-05, 2.18172717187001e-05,
0.0052992653709469, 0.0157284378186556, 1.02095850472487e-05,
6.26578426928999e-06, 1.60700750910741e-05, 1.5642472562587e-05,
6.62816031320301e-06, 0.00184183762620945, 0.000976314979621793,
0.000837378482800852, 0.00119405390180218, 0.000749043101546972,
2.53563183604468e-05, 6.40671433657518e-05, 2.15845735655314e-05,
2.23193546150843e-05, 0.021152108912735, 0.0154293683430873,
0.0179088127298003, 0.0243504748685249, 0.112274814726721,
0.117619628857172, 3.02064928526444e-05, 4.05741755377207e-05,
0.00181223384695308, 0.00207625195815772, 0.000943789542460111,
0.000206467245724716, 0.00154532777776536, 0.00226885210972427,
0.000119002475328241, 0.00022113732768583, 0.000235485770787934,
0.000458323057092208, 0.000652960109256795, 0.000724862646259261,
0.0727879614169974, 0.0343552622468531, 0.00966883214135748,
0.0121723948692721, 0.0502079899784375, 0.0642059286182179,
0.0744606748095006, 0.0279356500162451, 0.0165712179668264,
0.0155282557608783, 0.0161360297824822, 0.0176198016904319,
0.0025636247608102, 0.0011990003339988, 8.03074966867612e-05,
2.33672477744522e-05, 0.082133947866647, 0.000268818474649765,
9.9394282675768e-05, 5.50800067064597e-05, 0.0740561864383357,
0.0623723308152442, 0.0122016944534841, 0.00661733731518039,
0.000206774473028281, 0.000181997559545235, 0.000504582984323445,
0.0003864059666649, 0.260050134018791, 0.262172946886686,
0.18452199172589, 0.173786000484964, 0.0269969369566207,
0.0314904830827211, 0.00193643447967831, 0.00224489247051206,
3.93537896032742e-05, 1.28428312370815e-05, 0.0088546261393493,
0.0167354308377304, 1.21169704358824e-05, 2.12185497965253e-05,
6.62816031320301e-06, 1.43770979648094e-05, 1.55017664132774e-05,
4.91659313365037e-05, 0.000125001966670021, 6.22422609777641e-05,
2.67226640566305e-05, 3.7715397236691e-05, 0.000156595002030591,
0.000177342276671081, 9.9289511018095e-05, 3.26074531511788e-05,
0.0133283089983016, 0.016339336685079, 0.000374046362264717,
0.000314832095026693, 0.000101840989635774, 0.0499536313744285,
3.07770376000906e-05, 2.61927046816827e-05, 6.62816031320301e-06,
6.26578426928999e-06, 0.0190857963424119, 0.033561743200736,
0.00670074972877479, 0.00537788085712994, 1.18971385403729e-05,
1.11445994651961e-05, 6.62816031320301e-06, 6.26578426928999e-06,
1.14712208113288e-05, 2.05354865936513e-05, 7.60426873897666e-06,
6.26578426928999e-06, 1.20062319150955e-05, 1.36539983715138e-05,
6.62816031320301e-06, 7.2222843532018e-06, 9.9140609239925e-06,
9.94926827158573e-06, 7.53483093323745e-06, 8.69321376638133e-06,
1.21169704358824e-05, 1.19091870744721e-05, 6.62816031320301e-06,
7.23507396088365e-06, 2.46118496456816e-05, 2.17587837491135e-05,
6.62816031320301e-06, 6.26578426928999e-06, 1.82559375344064e-05,
1.89730194735652e-05, 6.62816031320301e-06, 6.26578426928999e-06,
0.00660850850197291, 0.0212222385456563, 0.00211118341459913,
0.00141420689575609, 9.26797200368037e-06, 8.90032999685065e-06,
7.54855398500617e-06, 6.26578426928999e-06, 2.23089983289096e-05,
2.17587837491135e-05, 6.62816031320301e-06, 6.26578426928999e-06,
5.45457596965578e-06, 5.45457596965578e-06, 6.62816031320301e-06,
5.98284905868666e-06, 3.2907497931271e-05, 2.32160410324433e-05,
6.62816031320301e-06, 1.51092819784857e-05, 1.21169704358824e-05,
1.19091870744721e-05, 7.91517227728171e-06, 7.23507396088365e-06,
4.18664545392701e-05, 4.14341191547126e-05, 0.000151073775168509,
0.000399628089283355, 9.9140609239925e-06, 1.4300121286468e-05,
7.53483093323745e-06, 6.26578426928999e-06), Level = structure(c(1L,
1L, 1L, 1L, 25L, 25L, 25L, 25L, 3L, 3L, 3L, 3L, 7L, 7L, 7L,
7L, 28L, 28L, 28L, 28L, 9L, 9L, 9L, 9L, 31L, 31L, 31L, 31L,
27L, 27L, 27L, 27L, 30L, 30L, 30L, 30L, 26L, 26L, 26L, 26L,
42L, 42L, 42L, 42L, 39L, 39L, 39L, 39L, 11L, 11L, 11L, 11L,
13L, 13L, 13L, 13L, 4L, 4L, 4L, 4L, 33L, 33L, 33L, 33L, 36L,
36L, 36L, 36L, 43L, 43L, 43L, 43L, 6L, 6L, 6L, 6L, 5L, 5L,
5L, 5L, 15L, 15L, 15L, 15L, 38L, 38L, 38L, 38L, 10L, 10L,
10L, 10L, 8L, 8L, 8L, 8L, 45L, 45L, 45L, 45L, 2L, 2L, 2L,
2L, 16L, 16L, 16L, 16L, 24L, 24L, 24L, 24L, 44L, 44L, 44L,
44L, 18L, 18L, 18L, 18L, 46L, 46L, 46L, 46L, 20L, 20L, 20L,
20L, 12L, 12L, 12L, 12L, 47L, 47L, 47L, 47L, 17L, 17L, 17L,
17L, 32L, 32L, 32L, 32L, 48L, 48L, 48L, 48L, 49L, 49L, 49L,
49L, 50L, 50L, 50L, 50L, 34L, 34L, 34L, 34L, 29L, 29L, 29L,
29L, 40L, 40L, 40L, 40L, 19L, 19L, 19L, 19L, 35L, 35L, 35L,
35L, 37L, 37L, 37L, 37L, 41L, 41L, 41L, 41L, 14L, 14L, 14L,
14L, 21L, 21L, 21L, 21L, 23L, 23L, 23L, 23L, 22L, 22L, 22L,
22L), .Label = c("Akkermansia", "Bacteroides", "Pseudomonas",
"Anaeroplasma", "Acetatifactor", "Lactobacillus", "Clostridium_XlVa",
"Oscillibacter", "Eubacterium", "Alistipes", "Acinetobacter",
"Parabacteroides", "Parasutterella", "Bacillus", "Clostridium_sensu_stricto",
"Turicibacter", "Clostridium_IV", "Staphylococcus", "Bifidobacterium",
"Romboutsia", "Escherichia/Shigella", "Listeria", "Enterococcus",
"Lactococcus", "Flavonifractor", "Ruminococcus", "Lachnospiracea_incertae_sedis",
"Clostridium_XlVb", "Stenotrophomonas", "Eisenbergiella",
"Hungatella", "Adhaeribacter", "Butyricicoccus", "Rhizobium",
"Prevotella", "Intestinimonas", "Sphingobium", "Dorea", "Enterorhabdus",
"Shewanella", "Bradyrhizobium", "Desulfovibrio", "Clostridium_XVIII",
"Anaerostipes", "Asaccharobacter", "Anaerotruncus", "Eggerthella",
"Butyricimonas", "Bilophila", "Anaerococcus"), class = "factor")), row.names = c(2L,
3L, 4L, 5L, 7L, 8L, 9L, 10L, 12L, 13L, 14L, 15L, 17L, 18L, 19L,
20L, 22L, 23L, 24L, 25L, 27L, 28L, 29L, 30L, 32L, 33L, 34L, 35L,
37L, 38L, 39L, 40L, 42L, 43L, 44L, 45L, 47L, 48L, 49L, 50L, 52L,
53L, 54L, 55L, 57L, 58L, 59L, 60L, 62L, 63L, 64L, 65L, 67L, 68L,
69L, 70L, 72L, 73L, 74L, 75L, 77L, 78L, 79L, 80L, 82L, 83L, 84L,
85L, 87L, 88L, 89L, 90L, 92L, 93L, 94L, 95L, 97L, 98L, 99L, 100L,
102L, 103L, 104L, 105L, 107L, 108L, 109L, 110L, 112L, 113L, 114L,
115L, 117L, 118L, 119L, 120L, 122L, 123L, 124L, 125L, 127L, 128L,
129L, 130L, 132L, 133L, 134L, 135L, 137L, 138L, 139L, 140L, 142L,
143L, 144L, 145L, 147L, 148L, 149L, 150L, 152L, 153L, 154L, 155L,
157L, 158L, 159L, 160L, 162L, 163L, 164L, 165L, 167L, 168L, 169L,
170L, 172L, 173L, 174L, 175L, 177L, 178L, 179L, 180L, 182L, 183L,
184L, 185L, 187L, 188L, 189L, 190L, 192L, 193L, 194L, 195L, 197L,
198L, 199L, 200L, 202L, 203L, 204L, 205L, 207L, 208L, 209L, 210L,
212L, 213L, 214L, 215L, 217L, 218L, 219L, 220L, 222L, 223L, 224L,
225L, 227L, 228L, 229L, 230L, 232L, 233L, 234L, 235L, 237L, 238L,
239L, 240L, 242L, 243L, 244L, 245L, 247L, 248L, 249L, 250L), class = "data.frame")
I couldn't get your code to run, so I'll answer your question with dummy data instead.
Let's generate some unequally distributed data, letters in this case.
set.seed(0)
df <- data.frame(x = LETTERS[c(rpois(100, 1) + 1, rpois(100, 8) + 1)])
We can determine the 8 most frequently occuring letters:
most_freq <- names(tail(sort(table(df$x)), 8))
Which we can then put into any of the discrete scales functions (including the manual one) as the breaks argument:
ggplot(df, aes(x, fill = x)) +
geom_bar() +
scale_fill_discrete(breaks = most_freq)
And as you can see, the categories not in the breaks still get a colour but don't get mentioned in the legend.

Nonlinear model convergence

I have a time series data set and each time series has datapoint of 30-year from different/same species. I am developing a forecasting model using the first 23 years of data from each time series data point and I am using the rest 7 years as test set to know the predictive ability of model but the nonlinear model (Model 6 and Model 7) don't give succinct result?
Data:
DD <- structure(list(Plot = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1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2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L), .Label = c("BD", "BG"), class = "factor"), Year = c(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, 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, 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, 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, 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, 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, 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), Count = c(81L, 45L, 96L, 44L, 24L, 8L, 28L, 32L, 39L, 29L,
40L, 17L, 4L, 12L, 18L, 11L, 63L, 98L, 78L, 76L, 67L, 36L, 56L,
43L, 81L, 8L, 14L, 20L, 25L, 19L, 135L, 91L, 171L, 88L, 59L,
1L, 1L, 1L, 2L, 1L, 11L, 9L, 34L, 15L, 32L, 21L, 33L, 43L, 39L,
20L, 6L, 3L, 9L, 9L, 28L, 16L, 15L, 2L, 1L, 1L, 34L, 16L, 19L,
35L, 32L, 7L, 2L, 30L, 29L, 25L, 28L, 11L, 31L, 31L, 28L, 27L,
34L, 110L, 87L, 103L, 72L, 19L, 46L, 43L, 107L, 32L, 26L, 31L,
12L, 29L, 23L, 40L, 50L, 23L, 34L, 11L, 9L, 4L, 24L, 55L, 14L,
16L, 51L, 43L, 2L, 13L, 8L, 96L, 52L, 118L, 32L, 1L, 8L, 17L,
34L, 29L, 38L, 15L, 4L, 38L, 2L, 1L, 1L, 1L, 1L, 1L, 3L, 3L,
4L, 6L, 4L, 4L, 10L, 6L, 7L, 9L, 15L, 30L, 25L, 36L, 13L, 17L,
43L, 36L, 60L, 50L, 26L, 13L, 13L, 27L, 18L, 56L, 96L, 16L, 54L,
2L, 2L, 9L, 5L, 5L, 6L, 2L, 6L, 2L, 3L, 4L, 3L, 136L, 71L, 116L,
28L, 23L, 76L, 64L, 98L, 58L, 26L, 13L, 13L, 13L, 18L, 19L, 24L,
18L, 17L, 3L, 23L, 19L, 9L, 11L, 13L, 20L, 29L, 29L, 17L, 20L,
26L, 71L, 63L, 53L, 54L, 20L, 22L, 18L, 93L, 50L, 18L, 12L, 12L,
31L), LogCount = c(1.908385019, 1.653212514, 1.982271233, 1.643462676,
1.380211242, 0.903089987, 1.447158031, 1.505109978, 1.591064607,
1.462397998, 1.602059991, 1.230448921, 0.602059991, 1.079181206,
1.255272505, 1.041392685, 1.799340549, 1.991226076, 1.892094603,
1.880813592, 1.826074803, 1.556302501, 1.748188027, 1.633468456,
1.908485019, 0.903089987, 1.146128035, 1.301029996, 1.397940009,
1.278753601, 2.130333768, 1.95904139, 2.2329961, 1.94448267,
1.770852012, 0, 0, 0, 0.30102999, 0, 1.0411392685, 0.954242509,
1.531478917, 1.176031259, 1.505149978, 1.322219295, 1.51851394,
1.6334684456, 1.591064607, 1.301029996, 0.77815125, 0.477121255,
0.954242509, 0.954242509, 1.447158031, 1.204119983, 1.176091259,
0.301029996, 0, 0, 1.531478917, 1.204119983, 1.278753501, 1.544068044,
1.505149978, 0.084509804, 0.301029996, 1.477121255, 1.462397998,
1.397940009, 1.447158031, 1.041392685, 1.491361694, 1.491361694,
1.447158031, 1.431363754, 1.531478917, 2.041392685, 1.939519253,
2.012837225, 1.857332495, 1.278753601, 1.662757382, 1.633468456,
2.029383778, 1.505149978, 1.414973348, 1.491361594, 1.079181245,
1.462397998, 1.361727835, 1.602059991, 1.698970004, 1.361727836,
1.531478917, 1.041392685, 0.954242509, 0.602059991, 1.380211242,
1.740362689, 1.146128036, 1.204119983, 1.707570176, 1.633468456,
0.301029996, 1.113943352, 0.903089987, 1.982271233, 1.716003344,
2.071882007, 1.50514997, 0, 0.903089987, 1.230448921, 1.53147891,
1.2397998, 1.57978359, 1.176091259, 0.602059991, 1.57978359,
0.301029996, 0, 0, 0, 0, 0, 0.477121255, 0.477121255, 0.602059991,
0.77815125, 0.602059991, 0.602059991, 1, 0.77815125, 0.84509804,
0.95424509, 1.176091259, 1.477121255, 1.39790009, 1.555302501,
1.113943352, 1.230448921, 1.633468456, 1.555302501, 1.77815125,
1.698970004, 1.414973348, 1.113943352, 1.113943352, 1.431353754,
1.255272505, 1.748188027, 1.982271233, 1.204119983, 1.73239376,
1.431363754, 1.361727835, 0.954242509, 0.698970004, 0.698970004,
0.77815125, 0.301029996, 0.77815125, 0.301029996, 0.477121255,
0.602059991, 0.477121255, 2.133538908, 1.851258349, 2.064457989,
1.447158031, 1.361727836, 1.880813592, 1.806179974, 1.991226076,
1.763427994, 1.414973348, 1.113943352, 1.113943352, 1.113943352,
1.255272505, 1.278753601, 1.380211242, 1.255272505, 1.230446921,
0.477121255, 1.361727835, 1.278753601, 0.954242509, 1.0411392685,
1.113943352, 1.301029996, 1.462397998, 1.462397998, 1.230448921,
1.301029995, 1.414973348, 1.851258349, 1.799340549, 1.72427587,
1.73239376, 1.301029996, 1.342422681, 1.255272505, 1.968482949,
1.698970004, 1.255272505, 1.079181246, 1.079181246, 1.491361694
), Diff = c(-0.255272505, 0.329058719, -0.338818557, -0.263241434,
-0.077121255, 0.544068044, 0.057991947, 0.085910629, -0.128666609,
0.139661993, -0.37161107, -0.62838893, 0.477121255, 0.176091259,
-0.21387982, 0.757947864, 0.191885527, -0.099131473, -0.011281011,
-0.054738789, -0.269772302, 0.191885526, -0.114719571, 0.275016563,
-1.005395032, 0.243038049, 0.15490196, 0.096910013, -0.119186408,
NA, -0.171292376, 0.273954718, -0.288513438, -0.17363066, -1.770852012,
0, 0, 0.301029996, -0.301029996, 1.041392685, -0.087150176, 0.577235408,
-0.355387658, 0.329058719, -0.182930683, 0.196294545, 0.110954516,
-0.042403849, -0.290034611, -0.522878746, -0.301029995, 0.477121254,
0, 0.492915522, -0.243038048, -0.028028724, -0.875061263, -0.301029996,
0, 1.531078917, -0.32735893, 0.070633618, 0.265310043, -0.038918066,
-0.660051938, -0.544068044, 1.176091259, -0.014723257, -0.064457989,
0.049218022, -0.405765346, 0.449969009, 0, -0.044203663, -0.015794267,
0.100115153, 0.509913768, -0.101873432, 0.073317972, -0.155504729,
-0.578578895, 0.384054231, -0.029289376, 0.395915322, -0.5202338,
-0.09017663, 0.076388346, -0.412180448, 0.383216752, -0.100670162,
0.240332155, 0.096910013, -0.337242168, 0.169751081, -0.490086232,
-0.087150176, -0.352182518, 0.778151251, 0.360151447, -0.594234653,
0.057991947, 0.503450193, -0.07410172, -1.33243846, 0.812913356,
-0.210853365, 1.079181246, -0.266267889, 0.355878663, -0.566732029,
-1.505149978, 0.903089987, 0.327358934, 0.301029996, -0.069080919,
0.117385599, -0.403692338, -0.574031268, 0.977723606, -1.278753601,
-0.301029996, 0, 0, 0, 0, 0.477121255, 0, 0.124938736, 0.176091259,
-0.176091259, 0, 0.397490009, -0.2218485, 0.06690679, 0.10914469,
0.22184875, 0.301029996, -0.079181206, 0.158362092, -0.442359149,
0.116505569, 0.403019535, -0.077165955, 0.221848749, -0.079181206,
-0.283996656, -0.301029996, 0, 0.317420412, -0.176091259, 0.492915522,
0.23483206, -0.77815125, 0.528273777, -0.301029996, -0.069635928,
-0.407485327, -0.255272505, 0, 0.079181246, -0.477121254, 0.477121254,
-0.477121254, 0.176091259, 0.124938736, -0.124938736, 1.656417653,
-0.282280559, 0.21319964, -0.617299958, -0.085430195, 0.5191085756,
-0.074533518, 0.185045102, -0.227798082, -0.348454546, -0.301029996,
0, 0, 0.141329153, 0.023481096, 0.101457641, -0.124938737, -0.024823584,
-0.753327666, 0.884606581, -0.082974235, -0.324511092, 0.087150176,
0.072550667, 0.187086644, 0.161368002, 0, -0.231949077, 0.070581075,
0.113903352, 0.436285001, -0.00519178, -0.075054679, 0.00811789,
-0.431363764, 0.041392685, -0.087150176, 0.713210444, -0.269512945,
-0.443697499, -0.176091259, 0, 0.412180448, -0.148939013)), class = "data.frame", row.names = c(NA,
-210L))
Code:
for(f in 1:11){
for(b in 1:5){
for (c in 1:5){
#To select test sets 1,2,3,4, and 5 years beyond the training set:
#Calculate the mean of abundance for the training set years.
Model1<-lm(mean~1, data=DD1)
#
Output2:
2 3 0.676209994477288 1.9365051784348e-09 4.44089209850063e-16
3 53 11.9236453578109 2.06371097988267e-09 1.13686837721616e-13
4 31 1.94583877614293 1.11022302462516e-15 1.99840144432528e-15
5 4 8.06660449042397 1.48071350736245e-08 3.19744231092045e-14
6 5 10.5321102149558 9.31706267692789e-10 1.4210854715202e-14
..
First, please see the time series graph of counts for different species and plots below.
library(ggplot2)
ggplot(DD, aes(Year, Count)) +
geom_point() +
geom_line() +
facet_grid(Plot ~ Species) +
scale_y_log10()
It is seen that there is no obvious trend which can be fitted by power or log-power function using nls.
Second, as I understand you are trying to use nls to predict outside the training data set. Usually it is not quite an effective to use least square models because of auto-correlated nature of time-series.
Third, the simplest prediction algorithm is Holt-Winters (see "dirty" implementation below). You can use as well a ton of other algorithms like ARIMA, exponential smoothing state space etc.
x <- ts(DD[DD$Species == "BG" & DD$Plot == "elq1a3", ]$LogCount)
m <- HoltWinters(x, gamma = FALSE)
library(forecast)
f <- forecast(m, 2)
plot(f, main = "elq1a3 at BG")
Fourth, about your algorithm in question, it throws
Error in qr.solve(QR.B, cc) : singular matrix 'a' in solve.
The reason is that in the first step of for-loop (at f = b = c = 1 DD2 data frame contains just one row. And executing
Model6<-nls(Diff~1+Count^T,start=list(T=1),trace=TRUE,algorithm ="plinear",data=DD2)
means that you are trying to fit a curve using only one data point, which is impossible.
However if you change f value in for-loop from 1:11 to 2:11 another error thrown:
Error in nls(Diff ~ 1 + Count^T, start = list(T = 1), trace = TRUE,
algorithm = "plinear", : step factor 0.000488281 reduced below
minFactor 0.000976562.
In this case you cannot use "naive" approach used by plinear algorithm with self-starting inital value and, e.g. nls.control(min.factor = 1e-5). You must feed all initial coefficients explicitely with default Gauss-Newton algorithm. Quite exausting, please try yourself :)

removing rows from dataframe in two different columns R

I have a dataframe of results. There are multiple comparisons for Cruise_Strata. I have two columns of cruise_strata (Cruise1_Strata1 and Cruise2_Strata2). The problem I found is that there are "duplicate" records in the dataframe. For example one row will have
Cruise_Strata1 Cruise_Strata2
201501.35 201502.35
and another row will have
Cruise_Strata1 Cruise_Strata2
201502.35 201501.35
The rows have the same results for the remaining columns. I would like to be able to identify rows where this happens and remove one row from the dataset, but do not know how to go about it. I cant use duplicate because they are not duplicates.
Any help would be appreciated.
Here is the dataframe.
dput(result5)
structure(list(Cruise_Strata1 = structure(c(1L, 1L, 2L, 2L, 3L,
3L, 4L, 4L, 5L, 5L, 6L, 6L, 7L, 7L, 8L, 8L, 9L, 9L, 10L, 10L,
11L, 11L, 12L, 12L, 13L, 13L, 14L, 14L, 15L, 15L, 16L, 16L, 17L,
17L, 18L, 18L, 19L, 19L, 20L, 20L, 21L, 21L, 22L, 22L, 23L, 23L,
24L, 24L, 25L, 25L, 26L, 26L, 27L, 27L, 28L, 28L, 29L, 29L, 30L,
30L, 31L, 31L, 32L, 32L, 33L, 33L, 34L, 34L, 35L, 35L, 36L, 36L,
37L, 37L, 38L, 38L, 39L, 39L, 40L, 40L, 41L, 41L, 42L, 42L, 43L,
43L, 44L, 44L, 45L, 45L, 46L, 46L, 47L, 47L, 48L, 48L, 49L, 49L,
50L, 50L, 51L, 51L, 52L, 52L, 53L, 53L, 54L, 54L, 55L, 55L, 56L,
56L, 57L, 57L, 58L, 58L, 59L, 59L, 60L, 60L, 61L, 61L, 62L, 62L,
63L, 63L, 64L, 64L, 65L, 65L, 66L, 66L), .Label = c("201501.10",
"201501.11", "201501.13", "201501.14", "201501.15", "201501.17",
"201501.18", "201501.19", "201501.21", "201501.22", "201501.23",
"201501.24", "201501.25", "201501.26", "201501.27", "201501.29",
"201501.30", "201501.31", "201501.33", "201501.34", "201501.35",
"201501.9", "201502.10", "201502.11", "201502.13", "201502.14",
"201502.15", "201502.17", "201502.18", "201502.19", "201502.21",
"201502.22", "201502.23", "201502.24", "201502.25", "201502.26",
"201502.27", "201502.29", "201502.30", "201502.31", "201502.33",
"201502.34", "201502.35", "201502.9", "201503.10", "201503.11",
"201503.13", "201503.14", "201503.15", "201503.17", "201503.18",
"201503.19", "201503.21", "201503.22", "201503.23", "201503.24",
"201503.25", "201503.26", "201503.27", "201503.29", "201503.30",
"201503.31", "201503.33", "201503.34", "201503.35", "201503.9"
), class = "factor"), Cruise_Strata2 = structure(c(23L, 45L,
24L, 46L, 25L, 47L, 26L, 48L, 27L, 49L, 28L, 50L, 29L, 51L, 30L,
52L, 31L, 53L, 32L, 54L, 33L, 55L, 34L, 56L, 35L, 57L, 36L, 58L,
37L, 59L, 38L, 60L, 39L, 61L, 40L, 62L, 41L, 63L, 42L, 64L, 43L,
65L, 44L, 66L, 1L, 45L, 2L, 46L, 3L, 47L, 4L, 48L, 5L, 49L, 6L,
50L, 7L, 51L, 8L, 52L, 9L, 53L, 10L, 54L, 11L, 55L, 12L, 56L,
13L, 57L, 14L, 58L, 15L, 59L, 16L, 60L, 17L, 61L, 18L, 62L, 19L,
63L, 20L, 64L, 21L, 65L, 22L, 66L, 1L, 23L, 2L, 24L, 3L, 25L,
4L, 26L, 5L, 27L, 6L, 28L, 7L, 29L, 8L, 30L, 9L, 31L, 10L, 32L,
11L, 33L, 12L, 34L, 13L, 35L, 14L, 36L, 15L, 37L, 16L, 38L, 17L,
39L, 18L, 40L, 19L, 41L, 20L, 42L, 21L, 43L, 22L, 44L), .Label = c("201501.10",
"201501.11", "201501.13", "201501.14", "201501.15", "201501.17",
"201501.18", "201501.19", "201501.21", "201501.22", "201501.23",
"201501.24", "201501.25", "201501.26", "201501.27", "201501.29",
"201501.30", "201501.31", "201501.33", "201501.34", "201501.35",
"201501.9", "201502.10", "201502.11", "201502.13", "201502.14",
"201502.15", "201502.17", "201502.18", "201502.19", "201502.21",
"201502.22", "201502.23", "201502.24", "201502.25", "201502.26",
"201502.27", "201502.29", "201502.30", "201502.31", "201502.33",
"201502.34", "201502.35", "201502.9", "201503.10", "201503.11",
"201503.13", "201503.14", "201503.15", "201503.17", "201503.18",
"201503.19", "201503.21", "201503.22", "201503.23", "201503.24",
"201503.25", "201503.26", "201503.27", "201503.29", "201503.30",
"201503.31", "201503.33", "201503.34", "201503.35", "201503.9"
), class = "factor"), P_value = c(0.63, 0.6793, 0.0319, 0.0289,
0.9516, 0.8128, 0.9967, 0.3071, 0.9641, 0.0246, 0.7967, 0.2551,
0.2329, 0.3725, 0.0269, 0.3796, 0.0245, 0.5562, 0.9952, 0.5176,
0.5596, 0.9966, 0.32, 0.6402, 0.7691, 0.9671, 0.9396, 0.9, 0.9024,
0.3624, 0.0433, 0.3402, 0.5302, 0.787, 0.0295, 0.3638, 0.006,
0.701, 0.6323, 0.0366, 2e-04, 0.0011, 0.8849, 0.3, 0.63, 0.9738,
0.0319, 0.5197, 0.9516, 0.7369, 0.9967, 0.2276, 0.9641, 0.0158,
0.7967, 0.6332, 0.2329, 0.0322, 0.0269, 0.3013, 0.0245, 0.0129,
0.9952, 0.795, 0.5596, 0.7277, 0.32, 0.747, 0.7691, 0.3817, 0.9396,
0.7961, 0.9024, 0.4164, 0.0433, 0.0028, 0.5302, 0.2864, 0.0295,
0.7036, 0.006, 0, 0.6323, 0.002, 2e-04, 0.9548, 0.8849, 0.0546,
0.6793, 0.9738, 0.0289, 0.5197, 0.8128, 0.7369, 0.3071, 0.2276,
0.0246, 0.0158, 0.2551, 0.6332, 0.3725, 0.0322, 0.3796, 0.3013,
0.5562, 0.0129, 0.5176, 0.795, 0.9966, 0.7277, 0.6402, 0.747,
0.9671, 0.3817, 0.9, 0.7961, 0.3624, 0.4164, 0.3402, 0.0028,
0.787, 0.2864, 0.3638, 0.7036, 0.701, 0, 0.0366, 0.002, 0.0011,
0.9548, 0.3, 0.0546), Cruise1 = structure(c(1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("201501",
"201502", "201503"), class = "factor"), Cruise1_Strata1 = structure(c(1L,
1L, 2L, 2L, 3L, 3L, 4L, 4L, 5L, 5L, 6L, 6L, 7L, 7L, 8L, 8L, 9L,
9L, 10L, 10L, 11L, 11L, 12L, 12L, 13L, 13L, 14L, 14L, 15L, 15L,
16L, 16L, 17L, 17L, 18L, 18L, 19L, 19L, 20L, 20L, 21L, 21L, 22L,
22L, 1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L, 5L, 5L, 6L, 6L, 7L, 7L,
8L, 8L, 9L, 9L, 10L, 10L, 11L, 11L, 12L, 12L, 13L, 13L, 14L,
14L, 15L, 15L, 16L, 16L, 17L, 17L, 18L, 18L, 19L, 19L, 20L, 20L,
21L, 21L, 22L, 22L, 1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L, 5L, 5L, 6L,
6L, 7L, 7L, 8L, 8L, 9L, 9L, 10L, 10L, 11L, 11L, 12L, 12L, 13L,
13L, 14L, 14L, 15L, 15L, 16L, 16L, 17L, 17L, 18L, 18L, 19L, 19L,
20L, 20L, 21L, 21L, 22L, 22L), .Label = c("10", "11", "13", "14",
"15", "17", "18", "19", "21", "22", "23", "24", "25", "26", "27",
"29", "30", "31", "33", "34", "35", "9"), class = "factor"),
Cruise2 = structure(c(2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L,
3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L,
2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L,
3L, 2L, 3L, 2L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L,
1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L,
3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L,
1L, 3L, 1L, 3L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L), .Label = c("201501", "201502", "201503"), class = "factor"),
Cruise2_Strata2 = structure(c(1L, 1L, 2L, 2L, 3L, 3L, 4L,
4L, 5L, 5L, 6L, 6L, 7L, 7L, 8L, 8L, 9L, 9L, 10L, 10L, 11L,
11L, 12L, 12L, 13L, 13L, 14L, 14L, 15L, 15L, 16L, 16L, 17L,
17L, 18L, 18L, 19L, 19L, 20L, 20L, 21L, 21L, 22L, 22L, 1L,
1L, 2L, 2L, 3L, 3L, 4L, 4L, 5L, 5L, 6L, 6L, 7L, 7L, 8L, 8L,
9L, 9L, 10L, 10L, 11L, 11L, 12L, 12L, 13L, 13L, 14L, 14L,
15L, 15L, 16L, 16L, 17L, 17L, 18L, 18L, 19L, 19L, 20L, 20L,
21L, 21L, 22L, 22L, 1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L, 5L, 5L,
6L, 6L, 7L, 7L, 8L, 8L, 9L, 9L, 10L, 10L, 11L, 11L, 12L,
12L, 13L, 13L, 14L, 14L, 15L, 15L, 16L, 16L, 17L, 17L, 18L,
18L, 19L, 19L, 20L, 20L, 21L, 21L, 22L, 22L), .Label = c("10",
"11", "13", "14", "15", "17", "18", "19", "21", "22", "23",
"24", "25", "26", "27", "29", "30", "31", "33", "34", "35",
"9"), class = "factor"), adjuste_p = c(1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0.792, 1, 1, 1, 0.0264,
0.1452, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0.3696, 1,
1, 1, 1, 0.792, 0, 1, 0.264, 0.0264, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 0.3696, 1, 1, 1, 1, 1, 0, 1, 0.264,
0.1452, 1, 1, 1)), .Names = c("Cruise_Strata1", "Cruise_Strata2",
"P_value", "Cruise1", "Cruise1_Strata1", "Cruise2", "Cruise2_Strata2",
"adjuste_p"), row.names = c(1453L, 2905L, 1520L, 2972L, 1587L,
3039L, 1654L, 3106L, 1721L, 3173L, 1788L, 3240L, 1855L, 3307L,
1922L, 3374L, 1989L, 3441L, 2056L, 3508L, 2123L, 3575L, 2190L,
3642L, 2257L, 3709L, 2324L, 3776L, 2391L, 3843L, 2458L, 3910L,
2525L, 3977L, 2592L, 4044L, 2659L, 4111L, 2726L, 4178L, 2793L,
4245L, 2860L, 4312L, 23L, 2927L, 90L, 2994L, 157L, 3061L, 224L,
3128L, 291L, 3195L, 358L, 3262L, 425L, 3329L, 492L, 3396L, 559L,
3463L, 626L, 3530L, 693L, 3597L, 760L, 3664L, 827L, 3731L, 894L,
3798L, 961L, 3865L, 1028L, 3932L, 1095L, 3999L, 1162L, 4066L,
1229L, 4133L, 1296L, 4200L, 1363L, 4267L, 1430L, 4334L, 45L,
1497L, 112L, 1564L, 179L, 1631L, 246L, 1698L, 313L, 1765L, 380L,
1832L, 447L, 1899L, 514L, 1966L, 581L, 2033L, 648L, 2100L, 715L,
2167L, 782L, 2234L, 849L, 2301L, 916L, 2368L, 983L, 2435L, 1050L,
2502L, 1117L, 2569L, 1184L, 2636L, 1251L, 2703L, 1318L, 2770L,
1385L, 2837L, 1452L, 2904L), class = "data.frame")
R Info
R version 3.2.1 (2015-06-18)
Platform: i386-w64-mingw32/i386 (32-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
Does this give you your desired result?
duplicated(apply(cbind(result5$Cruise_Strata1, df$Cruise_Strata2), 1,
function(x) paste(min(x), max(x))))
You can use the resulting logical vector to subset your data.
First you create a vector pasting the values in Cruise_Strata1 and Cruise_Strata2. Doing this you move the smaller of the two to the front and the larger one to the end (or you could do it vice versa). This is just a trick so that you can apply the duplicated function and recognize the duplicates.
Note: this approach will remove duplicates of the form:
Cruise_Strata1 Cruise_Strata2
x y
y x
As well as (if this is not desired let me know):
Cruise_Strata1 Cruise_Strata2
x y
x y
For a generic data frame df with duplicated values in Cruise_Strata1 and Cruise_Strata2:
df$dupe <- 0
for(i in 1:(length(df$Cruise_Strata1)-1))
{
for(j in (i+1):length(df$Cruise_Strata1))
if(df$Cruise_Strata1[i]==df$Cruise_Strata2[j])
{print(df[c(i,j),]); df$dupe[i] = 1;break}
}
df[df$dupe != 1,]

Ranking according to value across two variables - r

I have this dataframe:
df<-data.frame(
var1 = c(rep(c(rep(1,2), rep(2,3), rep(3,2), rep(4,1)),2), 1),
var2 = c(rep(1,8), rep(2,8),3)
)
df
var1 var2
#1 1 1
#2 1 1
#3 2 1
#4 2 1
#5 2 1
#6 3 1
#7 3 1
#8 4 1
#9 1 2
#10 1 2
#11 2 2
#12 2 2
#13 2 2
#14 3 2
#15 3 2
#16 4 2
#17 1 3
I would like to make a third variable that is a rank. Rows get the highest rank if 1) they have the lowest numbers in var2 - and then according to how low the numbers are in var1. e.g. Rows 1 and 2 with var2=1 and var1=1 should be ranked 1. Whereas, rows 9 and 10 with var2=2 and var1=1 would be ranked 5.
If my data are arranged in ascending order of var2 and then var1, I did the following using my favorite R function rle to achieve the ranking I'm after:
rle(df$var1)
N <- length(rle(df$var1)$lengths)
df$ranks <- rep(1:N, rle(df$var1)$lengths)
df
var1 var2 ranks
#1 1 1 1
#2 1 1 1
#3 2 1 2
#4 2 1 2
#5 2 1 2
#6 3 1 3
#7 3 1 3
#8 4 1 4
#9 1 2 5
#10 1 2 5
#11 2 2 6
#12 2 2 6
#13 2 2 6
#14 3 2 7
#15 3 2 7
#16 4 2 8
#17 1 3 9
This works, but it requires my df to be pre-sorted. I'd like a solution that does not need this. I feel like this should be a simple one-liner using rank and that I'm having a blind-spot. Any help appreciated - thanks.
EDIT 1:
- adding a larger example for testing of suggested answer
dput(df1)
df1 <- structure(list(var1 = c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L,
3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 7L,
7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 6L, 9L, 10L, 10L, 10L, 11L,
12L, 12L, 12L, 13L, 14L, 14L, 14L, 14L, 15L, 16L, 16L, 16L, 16L,
16L, 17L, 17L, 17L, 17L, 17L, 18L, 18L, 18L, 18L, 18L, 19L, 19L,
20L, 20L, 21L, 22L, 22L, 22L, 22L, 22L, 23L, 23L, 23L, 23L, 23L,
24L, 24L, 24L, 24L, 24L, 25L, 25L, 25L, 25L, 25L, 1L, 2L, 2L,
2L, 2L, 4L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L,
8L, 9L, 10L, 10L, 10L, 10L, 3L, 11L, 11L, 11L, 11L, 12L, 13L,
13L, 13L, 13L, 14L, 14L, 14L, 14L, 14L, 15L, 15L, 15L, 15L, 15L,
12L, 16L, 16L, 16L, 16L, 17L, 17L, 17L, 17L, 17L, 18L, 18L, 18L,
18L, 18L, 19L, 19L, 19L, 19L, 19L, 20L, 20L, 20L, 20L, 21L, 22L,
22L, 22L, 23L, 25L, 24L, 24L, 24L, 24L, 24L, 26L, 26L, 26L, 26L,
26L, 27L, 27L, 27L, 27L, 27L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L,
3L, 3L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L,
6L, 7L, 7L, 7L, 7L, 7L, 8L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L,
11L, 12L, 12L, 13L, 14L, 15L, 16L, 17L, 17L, 18L, 18L, 19L, 19L,
19L, 19L, 20L, 21L, 21L, 21L, 21L, 21L, 22L, 22L, 22L, 22L, 22L,
23L, 23L, 23L, 23L, 23L, 24L, 24L, 24L, 24L, 24L, 25L, 25L, 25L,
25L, 25L, 26L, 26L, 26L, 27L, 27L, 28L, 28L, 28L, 28L, 28L, 1L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L,
4L, 4L, 5L, 6L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L), var2 = c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,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, 1L, 1L, 12L, 12L, 12L, 12L, 12L, 19L, 19L, 19L, 19L, 19L,
20L, 20L, 20L, 20L, 20L, 21L, 21L, 21L, 21L, 21L, 23L, 23L, 23L,
23L, 23L, 24L, 24L, 24L, 24L, 24L, 22L, 25L, 2L, 2L, 2L, 3L,
4L, 4L, 4L, 5L, 6L, 6L, 6L, 6L, 7L, 8L, 8L, 8L, 8L, 8L, 9L, 9L,
9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 13L, 13L, 14L,
15L, 15L, 15L, 15L, 15L, 16L, 16L, 16L, 16L, 16L, 17L, 17L, 17L,
17L, 17L, 18L, 18L, 18L, 18L, 18L, 26L, 37L, 37L, 37L, 37L, 47L,
48L, 48L, 48L, 48L, 49L, 49L, 49L, 49L, 49L, 50L, 50L, 50L, 50L,
51L, 52L, 27L, 27L, 27L, 27L, 46L, 28L, 28L, 28L, 28L, 29L, 30L,
30L, 30L, 30L, 31L, 31L, 31L, 31L, 31L, 32L, 32L, 32L, 32L, 32L,
29L, 33L, 33L, 33L, 33L, 34L, 34L, 34L, 34L, 34L, 35L, 35L, 35L,
35L, 35L, 36L, 36L, 36L, 36L, 36L, 38L, 38L, 38L, 38L, 39L, 40L,
40L, 40L, 41L, 43L, 42L, 42L, 42L, 42L, 42L, 44L, 44L, 44L, 44L,
44L, 45L, 45L, 45L, 45L, 45L, 53L, 64L, 64L, 64L, 64L, 74L, 74L,
74L, 74L, 74L, 75L, 75L, 75L, 75L, 75L, 76L, 76L, 76L, 76L, 76L,
77L, 77L, 77L, 77L, 77L, 78L, 78L, 78L, 78L, 78L, 79L, 80L, 80L,
80L, 80L, 54L, 54L, 54L, 54L, 55L, 56L, 56L, 57L, 58L, 59L, 60L,
61L, 61L, 62L, 62L, 63L, 63L, 63L, 63L, 65L, 66L, 66L, 66L, 66L,
66L, 67L, 67L, 67L, 67L, 67L, 68L, 68L, 68L, 68L, 68L, 69L, 69L,
69L, 69L, 69L, 70L, 70L, 70L, 70L, 70L, 71L, 71L, 71L, 72L, 72L,
73L, 73L, 73L, 73L, 73L, 81L, 81L, 81L, 81L, 81L, 82L, 82L, 82L,
82L, 82L, 83L, 83L, 83L, 83L, 83L, 84L, 84L, 84L, 84L, 85L, 86L,
87L, 87L, 87L, 87L, 88L, 88L, 88L, 88L, 88L)), .Names = c("var1",
"var2", "ranks"), row.names = c(NA, -300L), class = "data.frame")
The ranks variable was got from this suggested answer:
df1$ranks1 <- dense_rank(paste(df1$var2, df1$var1))
Solutions involving paste[0] will only work if values within each vector are integer with a fixed number of digits. This is because paste converts to character and:
character (lexicographic) ordering differs from numeric: rank(c(1 , 2, 11)); rank(as.character(c(1 , 2, 11)))
concatenation introduces ambiguities: paste0(2,12); paste0(21,2)
Peter Dalgaard made a relevant post in 2011 http://r.789695.n4.nabble.com/Function-rank-for-data-frames-or-multiple-vectors-td3765685.html
For now ignore identical rows. Note that rank(x) == order(order(x)) and order accepts multiple ordering columns so if you don't mind how identical rows are split order(order(df$var2, df$var1)) does the job.
This splits identical rows according to their original ordering. There are a number of ways of ranking identical rows http://en.wikipedia.org/wiki/Ranking#Strategies_for_assigning_rankings.
In 2011 Peter Dalgaard suggested ave(order(order(df$var2, df$var1)), df$var2, df$var1) which gives what Wikipedia calls 'Fractional ranking' and in base::rank is the default ties.method="average".
Your example is what Wikipedia calls 'Dense ranking' which isn't available in base::rank but - as commented by David Arenburg - is provided by dplyr::dense_rank, so you can library(dyplr) and use:
dense_rank(ave(order(order(df$var2, df$var1)), df$var2, df$var1))
Looking at the code for dense_rank it is just
function (x)
{
r <- rank(x)
match(r, sort(unique(r)))
}
suggesting that if you don't want to load dplyr and are happy with a 2-statement solution creating another variable - e.g. r - you could use
r <- ave(order(order(df$var2, df$var1)), df$var2, df$var1); match(r, sort(unique(r)))
Edited to add ...
You can make things a little neater by realising that a data frame is really a list so to rank by columns in order of occurrence:
dense_rank(ave(order(do.call(order, df)), df))
You are ranking by columns in reverse order of occurrence so
dense_rank(ave(order(do.call(order, rev(df))), df))
or explicitly specifying columns and their order
dense_rank(ave(order(do.call(order, df[,2:1])), df[,2:1]))

Resources