Related
I've been trying to find a way to convert text files with pixels values into images (no matter the format) in R but I couldn't find a way to do it.
I found solutions for MatLab and Python, for example.
I have a file with 520 x 640 pixels with values from 0 to 255.
This is a small piece of it.
mid1al <- read.table("C:/Users/u015/Mid1_R_Al.txt", header = FALSE, sep = ";")
mid1al <- mid1al[1:20,1:20]
dput(mid1al)
structure(list(V1 = c(84L, 79L, 97L, 67L, 98L, 113L, 77L, 46L,
41L, 37L, 42L, 46L, 23L, 28L, 24L, 34L, 45L, 51L, 24L, 24L),
V2 = c(118L, 107L, 105L, 82L, 87L, 108L, 100L, 40L, 71L,
74L, 81L, 55L, 41L, 25L, 22L, 58L, 53L, 38L, 26L, 36L), V3 = c(103L,
116L, 128L, 82L, 77L, 104L, 97L, 50L, 65L, 78L, 98L, 111L,
86L, 59L, 35L, 51L, 43L, 46L, 33L, 47L), V4 = c(114L, 91L,
90L, 96L, 103L, 98L, 86L, 36L, 50L, 65L, 98L, 125L, 86L,
32L, 24L, 36L, 36L, 44L, 34L, 43L), V5 = c(68L, 70L, 85L,
85L, 100L, 111L, 61L, 12L, 42L, 70L, 103L, 103L, 45L, 27L,
18L, 27L, 32L, 43L, 51L, 41L), V6 = c(43L, 87L, 85L, 89L,
130L, 123L, 78L, 43L, 15L, 39L, 62L, 44L, 27L, 14L, 19L,
61L, 83L, 90L, 88L, 88L), V7 = c(20L, 72L, 116L, 124L, 133L,
133L, 103L, 56L, 21L, 9L, 19L, 26L, 18L, 32L, 67L, 92L, 100L,
105L, 94L, 79L), V8 = c(69L, 96L, 120L, 144L, 142L, 101L,
96L, 46L, 14L, 4L, 8L, 2L, 24L, 73L, 96L, 106L, 103L, 116L,
109L, 74L), V9 = c(118L, 122L, 134L, 135L, 133L, 98L, 57L,
20L, 5L, 5L, 2L, 14L, 51L, 89L, 117L, 95L, 103L, 93L, 104L,
77L), V10 = c(122L, 107L, 127L, 147L, 128L, 88L, 24L, 11L,
10L, 4L, 10L, 31L, 74L, 104L, 113L, 107L, 109L, 99L, 103L,
45L), V11 = c(105L, 120L, 114L, 132L, 125L, 112L, 51L, 6L,
3L, 9L, 18L, 49L, 82L, 111L, 111L, 96L, 92L, 81L, 75L, 18L
), V12 = c(98L, 104L, 103L, 126L, 147L, 128L, 61L, 26L, 2L,
9L, 18L, 50L, 105L, 103L, 101L, 98L, 74L, 53L, 18L, 1L),
V13 = c(107L, 91L, 108L, 109L, 138L, 114L, 88L, 33L, 2L,
4L, 9L, 61L, 71L, 77L, 78L, 83L, 43L, 38L, 8L, 5L), V14 = c(53L,
60L, 43L, 49L, 104L, 128L, 72L, 44L, 6L, 8L, 10L, 24L, 35L,
27L, 33L, 37L, 31L, 24L, 10L, 5L), V15 = c(13L, 16L, 11L,
27L, 62L, 78L, 73L, 30L, 8L, 7L, 31L, 66L, 66L, 33L, 13L,
27L, 16L, 18L, 12L, 7L), V16 = c(11L, 12L, 7L, 3L, 16L, 35L,
45L, 13L, 5L, 7L, 22L, 74L, 73L, 31L, 16L, 43L, 35L, 14L,
15L, 8L), V17 = c(15L, 16L, 7L, 8L, 1L, 5L, 15L, 13L, 31L,
33L, 22L, 34L, 38L, 17L, 18L, 41L, 39L, 26L, 19L, 12L), V18 = c(9L,
15L, 7L, 2L, 2L, 5L, 5L, 25L, 50L, 55L, 35L, 25L, 14L, 8L,
18L, 44L, 36L, 36L, 19L, 0L), V19 = c(15L, 16L, 4L, 6L, 4L,
6L, 22L, 45L, 59L, 48L, 56L, 58L, 52L, 30L, 22L, 46L, 41L,
50L, 23L, 7L), V20 = c(20L, 7L, 4L, 2L, 6L, 14L, 40L, 55L,
74L, 60L, 69L, 74L, 60L, 56L, 38L, 45L, 67L, 39L, 25L, 11L
)), row.names = c(NA, 20L), class = "data.frame")
Is there a way to create this image in Rstudio?
Im using ggstatsplot's ggscatterstats function to calculate correlation between various clinical parameters and then plotting them. For example
here my variables are age and WBC. This is taking all the data points irrespective of the class they belong. I would like to do the same with each FAB classification that is present in my data.
dat <- merge_clinical_class_TMB %>% select(FAB,AGE,Wbc,Platelet,HB,PB_Blasts,BM_Blasts,TMB_NONSYNONYMOUS)
df2 <- dat
library(ggstatsplot)
ggscatterstats(
df2,
x = AGE,
y = Wbc,
type = "np" # try the "robust" correlation too! It might be even better here
#, marginal.type = "boxplot"
)
My dataframe looks like this
head(df2)
FAB AGE Wbc Platelet HB PB_Blasts BM_Blasts TMB_NONSYNONYMOUS
1 M4 50 17 231 10 88 52 0.3000000
2 M3 61 1 90 10 44 0 0.4333333
3 M3 30 6 114 11 82 6 0.2333333
4 M0 77 92 105 9 67 56 0.4000000
5 M1 46 29 90 9 90 81 0.5666667
6 M1 68 3 63 8 91 55 0.9000000
My data
dput(df2)
structure(list(FAB = structure(c(5L, 4L, 4L, 1L, 2L, 2L, 3L,
3L, 3L, 5L, 3L, 5L, 1L, 5L, 5L, 3L, 3L, 3L, 1L, 2L, 1L, 4L, 6L,
6L, 5L, 3L, 5L, 7L, 5L, 1L, 6L, 5L, 5L, 6L, 5L, 6L, 3L, 3L, 4L,
4L, 5L, 7L, 3L, 3L, 5L, 2L, 5L, 1L, 3L, 6L, 2L, 5L, 2L, 5L, 7L,
3L, 3L, 8L, 6L, 4L, 2L, 2L, 2L, 2L, 3L, 8L, 3L, 2L, 2L, 4L, 6L,
3L, 3L, 3L, 2L, 3L, 2L, 2L, 2L, 3L, 6L, 2L, 1L, 3L, 2L, 5L, 5L,
1L, 2L, 5L, 6L, 6L, 2L, 6L, 4L, 2L, 5L, 2L, 2L, 2L, 1L, 4L, 4L,
1L, 3L, 9L, 6L, 5L, 5L, 1L, 3L, 3L, 5L, 1L, 2L, 2L, 3L, 5L, 1L,
5L, 5L, 6L, 2L, 2L, 2L, 1L, 3L, 3L, 6L, 5L, 2L, 5L, 1L, 2L, 8L,
2L, 3L, 9L, 5L, 2L, 1L, 5L, 3L, 5L, 5L, 1L, 3L, 2L, 5L, 3L, 6L,
5L, 1L, 2L, 2L, 5L, 3L, 5L, 5L, 6L, 5L, 5L, 3L, 5L, 6L, 3L, 2L,
3L, 3L, 2L, 4L, 6L, 4L, 1L, 2L, 6L, 3L, 6L, 2L, 3L, 2L, 4L, 2L,
2L, 4L, 3L, 3L, 4L, 4L, 4L, 3L, 4L, 3L, 6L, 2L, 4L, 2L, 5L, 2L,
4L), .Label = c("M0", "M1", "M2", "M3", "M4", "M5", "M6", "M7",
"nc"), class = "factor"), AGE = c(50L, 61L, 30L, 77L, 46L, 68L,
23L, 64L, 76L, 81L, 25L, 78L, 39L, 49L, 57L, 63L, 62L, 52L, 76L,
64L, 65L, 61L, 44L, 31L, 64L, 33L, 55L, 50L, 64L, 59L, 59L, 77L,
33L, 48L, 35L, 66L, 67L, 51L, 74L, 51L, 64L, 77L, 63L, 37L, 57L,
53L, 62L, 39L, 72L, 66L, 51L, 51L, 18L, 63L, 54L, 75L, 40L, 60L,
76L, 33L, 63L, 53L, 75L, 67L, 66L, 77L, 64L, 76L, 51L, 42L, 51L,
59L, 43L, 45L, 60L, 47L, 68L, 24L, 48L, 73L, 60L, 44L, 71L, 25L,
60L, 57L, 55L, 69L, 42L, 42L, 45L, 50L, 41L, 21L, 50L, 69L, 76L,
70L, 27L, 76L, 65L, 48L, 59L, 69L, 81L, 22L, 61L, 51L, 63L, 61L,
22L, 73L, 49L, 41L, 47L, 54L, 44L, 55L, 83L, 78L, 59L, 57L, 57L,
88L, 43L, 71L, 62L, 75L, 62L, 58L, 65L, 66L, 60L, 35L, 76L, 72L,
35L, 73L, 67L, 70L, 48L, 65L, 41L, 52L, 67L, 58L, 34L, 60L, 55L,
56L, 61L, 31L, 71L, 56L, 57L, 60L, 57L, 58L, 79L, 55L, 34L, 76L,
82L, 67L, 67L, 54L, 53L, 71L, 61L, 30L, 50L, 35L, 29L, 45L, 38L,
81L, 31L, 75L, 67L, 29L, 51L, 40L, 32L, 57L, 25L, 63L, 75L, 25L,
68L, 62L, 25L, 31L, 68L, 45L, 61L, 35L, 22L, 23L, 21L, 53L),
Wbc = c(17L, 1L, 6L, 92L, 29L, 3L, 32L, 117L, 62L, 91L, 34L,
10L, 2L, 57L, 88L, 77L, 75L, 4L, 15L, 1L, 3L, 86L, 9L, 137L,
132L, 3L, 22L, 6L, 3L, 1L, 12L, 40L, 26L, 116L, 53L, 112L,
2L, 42L, 32L, 4L, 2L, 3L, 17L, 19L, 14L, 3L, 119L, 5L, 3L,
79L, 104L, 3L, 35L, 77L, 2L, 8L, 8L, 1L, 4L, 1L, 46L, 2L,
6L, 31L, 3L, 2L, 3L, 34L, 2L, 2L, 15L, 12L, 4L, 29L, 12L,
12L, 60L, 224L, 33L, 2L, 7L, 14L, 5L, 11L, 47L, 5L, 31L,
6L, 11L, 38L, 5L, 7L, 134L, 93L, 3L, 10L, 3L, 48L, 90L, 297L,
1L, 1L, 1L, 2L, 2L, 115L, 35L, 50L, 18L, 62L, 52L, 15L, 12L,
48L, 81L, 13L, 35L, 28L, 78L, 17L, 30L, 99L, 20L, 3L, 172L,
6L, 28L, 98L, 59L, 101L, 68L, 2L, 2L, 43L, 4L, 38L, 34L,
59L, 37L, 1L, 111L, 49L, 43L, 298L, 26L, 47L, 14L, 16L, 114L,
203L, 8L, 133L, 1L, 31L, 3L, 68L, 3L, 20L, 19L, 73L, 20L,
5L, 1L, 15L, 45L, 68L, 88L, 36L, 10L, 23L, 1L, 72L, 1L, 2L,
40L, 12L, 13L, 7L, 46L, 2L, 64L, NA, 5L, 103L, 8L, 1L, 3L,
16L, 29L, 1L, 99L, 2L, 6L, 2L, 3L, 2L, 115L, 27L, 8L, 1L),
Platelet = c(231L, 90L, 114L, 105L, 90L, 63L, 38L, 100L,
32L, 32L, 23L, 98L, 215L, 14L, 56L, 19L, 110L, 22L, 85L,
42L, 16L, 22L, 50L, 42L, 15L, 61L, 65L, 50L, 134L, 102L,
57L, 29L, 111L, 50L, 44L, 34L, 28L, 232L, 42L, 58L, 27L,
86L, 23L, 38L, 76L, 108L, 52L, 175L, 52L, 132L, 23L, 143L,
30L, 41L, 9L, 21L, 95L, 59L, 79L, 38L, 11L, 68L, 22L, 141L,
168L, 70L, 41L, 21L, 25L, 35L, 14L, 20L, 67L, 116L, 45L,
57L, 8L, 34L, 32L, 60L, 93L, 145L, 48L, 33L, 50L, 129L, 9L,
61L, 176L, 12L, 53L, 136L, 40L, 73L, 27L, 12L, 166L, 30L,
87L, 40L, 94L, 52L, 23L, 127L, 39L, 57L, 35L, 21L, 148L,
25L, 149L, 64L, 351L, 71L, 53L, 22L, 35L, 31L, 46L, 85L,
18L, 80L, 62L, 156L, 32L, 50L, 69L, 31L, 20L, 57L, 142L,
37L, 79L, 66L, 21L, 31L, 88L, 11L, 15L, 82L, 53L, 76L, 51L,
68L, 64L, 55L, 40L, 90L, 37L, 45L, 36L, 52L, 86L, 88L, 35L,
174L, 28L, 121L, 131L, 17L, 152L, 52L, 30L, 79L, 79L, 87L,
30L, 44L, 140L, 59L, 58L, 19L, 29L, 156L, 19L, 61L, 36L,
11L, 71L, 13L, 45L, 34L, 39L, 82L, 18L, 43L, 118L, 32L, 73L,
15L, 60L, 208L, 96L, 257L, 61L, 12L, 32L, 23L, 52L, 46L),
HB = c(10L, 10L, 11L, 9L, 9L, 8L, 7L, 10L, 10L, 11L, 11L,
10L, 10L, 8L, 10L, 13L, 11L, 9L, 9L, 8L, 9L, 12L, 8L, 6L,
10L, 7L, 8L, 9L, 11L, 12L, 11L, 10L, 10L, 9L, 8L, 10L, 9L,
13L, 9L, 8L, 12L, 9L, 12L, 9L, 9L, 9L, 11L, 10L, 11L, 12L,
12L, 11L, 9L, 10L, 9L, 9L, 10L, 9L, 10L, 9L, 8L, 9L, 9L,
10L, 12L, 10L, 10L, 8L, 10L, 9L, 11L, 11L, 11L, 8L, 9L, 9L,
9L, 6L, 10L, 10L, 9L, 9L, 8L, 9L, 9L, 7L, 9L, 11L, 12L, 10L,
9L, 10L, 12L, NA, 10L, 7L, 11L, 10L, 9L, 11L, 10L, 9L, 8L,
8L, 10L, 9L, 12L, 11L, 8L, 13L, 11L, 9L, 9L, 12L, 10L, 9L,
10L, 8L, 9L, 9L, 9L, 10L, 9L, 10L, 10L, 9L, 10L, 8L, 7L,
9L, 9L, 8L, 9L, 9L, 8L, 10L, 8L, 9L, 9L, 8L, 9L, 9L, 9L,
9L, 9L, 10L, 9L, 8L, 9L, 10L, 7L, 11L, 11L, 10L, 6L, 8L,
9L, 9L, 10L, 8L, 11L, 10L, 11L, 8L, 9L, 8L, 9L, 8L, 10L,
10L, 10L, 9L, 9L, 12L, 9L, 9L, 11L, 9L, 13L, 9L, 10L, 8L,
9L, 10L, 10L, 11L, 9L, 9L, 10L, 9L, 9L, 11L, 7L, 13L, 14L,
12L, 8L, 12L, 8L, 9L), PB_Blasts = c(88L, 44L, 82L, 67L,
90L, 91L, 59L, 60L, 48L, 98L, 53L, 40L, 75L, 81L, 90L, 57L,
46L, 67L, 74L, 61L, 99L, 73L, 74L, 83L, 72L, 33L, 35L, 70L,
85L, 61L, 95L, 80L, 71L, 83L, 90L, 90L, 50L, 64L, 51L, 93L,
95L, 75L, 80L, 52L, 61L, 72L, 65L, 83L, 45L, 32L, 85L, 73L,
86L, 82L, 30L, 48L, 47L, 58L, 78L, 100L, 81L, 82L, 40L, 89L,
70L, 47L, 80L, 73L, 62L, 88L, 57L, 70L, 40L, 56L, 86L, 37L,
90L, 77L, 75L, 37L, 94L, 86L, 97L, 72L, 87L, 40L, 52L, 60L,
68L, 40L, 95L, 81L, 92L, 90L, 90L, 42L, 37L, 84L, 77L, 99L,
83L, 65L, 79L, 82L, 46L, 94L, 71L, 39L, 62L, 95L, 55L, 11L,
51L, 42L, 77L, 72L, 39L, 69L, 75L, 70L, 75L, 52L, 91L, 33L,
87L, 55L, 72L, 76L, 85L, 79L, 79L, 81L, 50L, 81L, 33L, 88L,
34L, 90L, 69L, 32L, 92L, 90L, 47L, 75L, 30L, 59L, 57L, 62L,
54L, 60L, 89L, 82L, 90L, 90L, 64L, 89L, 43L, 58L, 58L, 97L,
71L, 91L, 53L, 75L, 85L, 67L, 86L, 70L, 43L, 86L, 74L, 87L,
0L, 0L, 86L, 53L, 63L, 41L, 76L, 45L, 85L, 0L, 94L, 6L, 91L,
0L, 2L, 93L, 85L, 82L, 56L, 40L, 48L, 0L, 14L, 90L, 71L,
51L, 91L, 42L), BM_Blasts = c(52L, 0L, 6L, 56L, 81L, 55L,
0L, 0L, 88L, 37L, 87L, 6L, 4L, 48L, 84L, 70L, 53L, 18L, 82L,
5L, 34L, 68L, 5L, 6L, 90L, 0L, 67L, 0L, 22L, 12L, 0L, 2L,
14L, 3L, 18L, 7L, 17L, 79L, 0L, 40L, 0L, 8L, 71L, 33L, 17L,
41L, 65L, 53L, 0L, 11L, 85L, 2L, 90L, 39L, 0L, 54L, 23L,
0L, 0L, 0L, 97L, 42L, 48L, 61L, 6L, 0L, 46L, 55L, 10L, 2L,
0L, 48L, 39L, 37L, 43L, 0L, 91L, 76L, 41L, 16L, 30L, 17L,
54L, 50L, 65L, 0L, 59L, 22L, 51L, 16L, 6L, 10L, 90L, 72L,
0L, 32L, 0L, 49L, 88L, 98L, 0L, 0L, 15L, 0L, 0L, 94L, 55L,
39L, 9L, 86L, 70L, 11L, 5L, 74L, 79L, 90L, 83L, 57L, 74L,
28L, 17L, 4L, 91L, 0L, 91L, 50L, 49L, 80L, 22L, 64L, 84L,
12L, 14L, 86L, 6L, 18L, 40L, 0L, 61L, 6L, 87L, 0L, 62L, 51L,
6L, 72L, 59L, 29L, 24L, 96L, 0L, 53L, 13L, 45L, 61L, 56L,
35L, 10L, 0L, 8L, 58L, 16L, 25L, 10L, 3L, 71L, 52L, 67L,
32L, 88L, 10L, 8L, 0L, 0L, 97L, 7L, 45L, 0L, 49L, 9L, 85L,
0L, 70L, 91L, 7L, 0L, 2L, 0L, 32L, 11L, 71L, 0L, 48L, 0L,
14L, 7L, 90L, 63L, 83L, 29L), TMB_NONSYNONYMOUS = c(0.3,
0.433333333333, 0.233333333333, 0.4, 0.566666666667, 0.9,
0.3, 0.133333333333, 0.4, 0.3, 0.233333333333, 0.5, 0.266666666667,
0, 0.2, 0.4, 0.266666666667, 0.333333333333, 0.4, 0.4, 0.566666666667,
0.0333333333333, 0.166666666667, 0.1, 0.166666666667, 0.266666666667,
0.3, 0.3, 0.466666666667, 0.0666666666667, 0.266666666667,
0.266666666667, 0.0333333333333, 0.1, 0.133333333333, 0.0333333333333,
0.5, 0.6, 0.0333333333333, 0.1, 0.0333333333333, 0.333333333333,
0.433333333333, 0.2, 0.466666666667, 0.2, 0.0333333333333,
0.733333333333, 0.2, 0.233333333333, 0.233333333333, 0.3,
0.133333333333, 0, 0.3, 0.333333333333, 0.333333333333, 0.266666666667,
0.533333333333, 0.2, 0.533333333333, 0.466666666667, 0.533333333333,
0.0333333333333, 0.3, 0.5, 0.333333333333, 0.266666666667,
0.5, 0.333333333333, 0.0666666666667, 0.466666666667, 0.333333333333,
0.266666666667, 0.7, 0.433333333333, 0.166666666667, 0.0666666666667,
0.233333333333, 0.5, 0.0333333333333, 0.2, 0.433333333333,
0.433333333333, 0.4, 0.233333333333, 0.0666666666667, 0.233333333333,
0.466666666667, 0.0666666666667, 0, 0.1, 0.4, 0.1, 0.2, 0.4,
0.433333333333, 0.566666666667, 0.2, 0.0333333333333, 0.533333333333,
0.566666666667, 0.3, 0.466666666667, 0.566666666667, 0.0333333333333,
0.4, 0.0666666666667, 0.633333333333, 0.4, 0.466666666667,
0.466666666667, 0.3, 0.5, 0.0333333333333, 0.333333333333,
0.333333333333, 0.266666666667, 0.366666666667, 0.666666666667,
0.333333333333, 0.533333333333, 0.466666666667, 0.6, 0.333333333333,
0.4, 0.266666666667, 0.366666666667, 0.2, 0.0333333333333,
0.266666666667, 0.3, 0.166666666667, 0.4, 0.566666666667,
0.4, 0.1, 0.1, 0.0666666666667, 0.366666666667, 0, 0.4, 0.0333333333333,
0.1, 0.0666666666667, 0.5, 0.3, 0.466666666667, 0.0333333333333,
0.4, 0.1, 0.0666666666667, 0.766666666667, 0.5, 0.466666666667,
0.333333333333, 0.4, 0.333333333333, 0.4, 0.266666666667,
0.2, 0.3, 0.7, 0.166666666667, 0.2, 0, 0.5, 0.166666666667,
0.533333333333, 0.233333333333, 0.166666666667, 0.133333333333,
0.0666666666667, 0.4, 0.333333333333, 0.133333333333, 0.4,
0.233333333333, 0.466666666667, 0.366666666667, 0.266666666667,
0.266666666667, 0.266666666667, 0.4, 0.2, 0.166666666667,
0.4, 0.333333333333, 0.166666666667, 0.266666666667, 0.1,
0.333333333333, 0.733333333333, 0.466666666667, 0.466666666667,
0.2, 0.1, 1.13333333333, 0.2, 0.3)), class = "data.frame", row.names = c(NA,
-200L))
Objective I would like to do the same with various FABI have FAB label from M0 to M7 I would like to ignore nc
So for each FAB label I would like to see the correlation for example if I have to take the M0 class then I would like to see their Age vs Wbc correlation and similarly for other FAB class as well. Is it possible to do these in ggstataplot as I don't see for correlation any such functionality there .
Simple way is I can subset them and do the same like M0 ,M1, M2 etc etc but that is a long process can I split the FAB column and pass it to the library?
I would like to know other ways to do the above and plot the same
Any help or suggestion would be appreciated
Update: We could also use the built in function see comments:
Many thanks to #Indrajeet Patil: https://indrajeetpatil.github.io/ggstatsplot/articles/web_only/ggscatterstats.html#grouped-analysis-with-grouped_ggscatterstats
To subset FAB we use filter:
## for reproducibility
set.seed(123)
## plot
grouped_ggscatterstats(
## arguments relevant for ggscatterstats
data = df2 %>% filter(as.integer(FAB)<5),
x = AGE,
y = Wbc,
grouping.var = FAB,
type = "r",
# ggtheme = ggthemes::theme_tufte(),
## arguments relevant for combine_plots
annotation.args = list(
title = "Relationship between Wbc and Age",
caption = "Source: stackoverflow"
),
plotgrid.args = list(nrow = 2, ncol = 2)
)
First answer:
We could do something like this:
write a function and pass the data frame + the column FAB value:
library(ggstatsplot)
my_function <- function(df, x){
ggscatterstats(
df %>% filter(FAB == x),
x = AGE,
y = Wbc,
type = "np" # try the "robust" correlation too! It might be even better here
#, marginal.type = "boxplot"
)
}
M0 <- my_function(df2, "M0")
M1 <- my_function(df2, "M1")
M2 <- my_function(df2, "M2")
M3 <- my_function(df2, "M3")
.
.
.
library(patchwork)
(M0 / M1 | M2 / M3)
I have a data.frame fish.test0 for which I want to grep specific variables (in varlist) matching the group column to create a sub-data.frame that will undergo a statistical test. The results of the test is saved in tests.res.t. I want to loop the varlist so that I get one results for each input in varlist
Script:
varlist <- c("Abiotrophia","Alphatorquevirus")
for (i in varlist) {
fish.test <- fish.test0[grep("i",fish.test0$group),]
column <- c("ACDC")
tests <- list()
dat_test <- sapply( column, function(colx)
lapply( unique(fish.test$Merge), function(x)
fisher.test( data.frame(
a=c(( fish.test[ which(fish.test$Merge %in% x)[2],"Present"] -
fish.test[ which(fish.test$Merge %in% x)[2], colx] ),fish.test[ which(fish.test$Merge %in% x)[2], colx]
),
b=c(( fish.test[ which(fish.test$Merge %in% x)[1],"NotPresent"] -
fish.test[ which(fish.test$Merge %in% x)[1], colx] ), fish.test[ which(fish.test$Merge %in% x)[1], colx]))) #,alternative = "greater"
) )
rownames(dat_test) <- unique(fish.test$Merge )
colnames(dat_test) <- column
tests.res <- sapply(dat_test[1:dim(dat_test)[1],1], function(x) {
c(x$estimate[1],
x$estimate[2],
ci.lower = x$conf.int[1],
ci.upper = x$conf.int[2],
p.value = x$p.value)
})
tests.res.t <- as.data.frame(t(tests.res))
}
test-data:
fish.test0 <- structure(list(Present = c(4L, 4L, 9L, 9L, 57L, 57L, 146L, 146L,
91L, 91L, 26L, 26L, 6L, 6L, 12L, 12L, 33L, 33L, 10L, 10L, 66L,
66L, 4L, 4L, 4L, 4L, 9L, 9L, 18L, 18L, 19L, 19L, 51L, 51L, 50L,
50L, 12L, 12L, 7L, 7L, 14L, 14L, 27L, 27L, 9L, 9L, 5L, 5L, 6L,
6L, 22L, 22L, 3L, 3L, 14L, 14L, 4L, 4L, 15L, 15L, 6L, 6L, 8L,
8L, 4L, 4L), NotPresent = c(11L, 11L, 44L, 44L, 126L, 126L, 532L,
532L, 382L, 382L, 97L, 97L, 14L, 14L, 43L, 43L, 85L, 85L, 41L,
41L, 336L, 336L, 19L, 19L, 27L, 27L, 67L, 67L, 108L, 108L, 81L,
81L, 240L, 240L, 258L, 258L, 47L, 47L, 31L, 31L, 82L, 82L, 110L,
110L, 63L, 63L, 178L, 178L, 672L, 672L, 451L, 451L, 120L, 120L,
104L, 104L, 47L, 47L, 387L, 387L, 94L, 94L, 300L, 300L, 133L,
133L), group = c("G__Abiotrophia_NotPresent_Anus", "G__Abiotrophia_Present_Anus",
"G__Abiotrophia_NotPresent_Bile duct", "G__Abiotrophia_Present_Bile duct",
"G__Abiotrophia_NotPresent_Bone/Soft tissue", "G__Abiotrophia_Present_Bone/Soft tissue",
"G__Abiotrophia_NotPresent_Breast", "G__Abiotrophia_Present_Breast",
"G__Abiotrophia_NotPresent_Colorectum", "G__Abiotrophia_Present_Colorectum",
"G__Abiotrophia_NotPresent_Esophagus", "G__Abiotrophia_Present_Esophagus",
"G__Abiotrophia_NotPresent_Gallbladder", "G__Abiotrophia_Present_Gallbladder",
"G__Abiotrophia_NotPresent_Head and neck", "G__Abiotrophia_Present_Head and neck",
"G__Abiotrophia_NotPresent_Kidney", "G__Abiotrophia_Present_Kidney",
"G__Abiotrophia_NotPresent_Liver", "G__Abiotrophia_Present_Liver",
"G__Abiotrophia_NotPresent_Lung", "G__Abiotrophia_Present_Lung",
"G__Abiotrophia_NotPresent_Lymphoid tissue", "G__Abiotrophia_Present_Lymphoid tissue",
"G__Abiotrophia_NotPresent_Mesothelium", "G__Abiotrophia_Present_Mesothelium",
"G__Abiotrophia_NotPresent_Nervous system", "G__Abiotrophia_Present_Nervous system",
"G__Abiotrophia_NotPresent_Ovary", "G__Abiotrophia_Present_Ovary",
"G__Abiotrophia_NotPresent_Pancreas", "G__Abiotrophia_Present_Pancreas",
"G__Abiotrophia_NotPresent_Prostate", "G__Abiotrophia_Present_Prostate",
"G__Abiotrophia_NotPresent_Skin", "G__Abiotrophia_Present_Skin",
"G__Abiotrophia_NotPresent_Small intestine", "G__Abiotrophia_Present_Small intestine",
"G__Abiotrophia_NotPresent_Stomach", "G__Abiotrophia_Present_Stomach",
"G__Abiotrophia_NotPresent_Unknown", "G__Abiotrophia_Present_Unknown",
"G__Abiotrophia_NotPresent_Urothelial tract", "G__Abiotrophia_Present_Urothelial tract",
"G__Abiotrophia_NotPresent_Uterus", "G__Abiotrophia_Present_Uterus",
"G__Alphatorquevirus_NotPresent_Bone/Soft tissue", "G__Alphatorquevirus_Present_Bone/Soft tissue",
"G__Alphatorquevirus_NotPresent_Breast", "G__Alphatorquevirus_Present_Breast",
"G__Alphatorquevirus_NotPresent_Colorectum", "G__Alphatorquevirus_Present_Colorectum",
"G__Alphatorquevirus_NotPresent_Esophagus", "G__Alphatorquevirus_Present_Esophagus",
"G__Alphatorquevirus_NotPresent_Kidney", "G__Alphatorquevirus_Present_Kidney",
"G__Alphatorquevirus_NotPresent_Liver", "G__Alphatorquevirus_Present_Liver",
"G__Alphatorquevirus_NotPresent_Lung", "G__Alphatorquevirus_Present_Lung",
"G__Alphatorquevirus_NotPresent_Pancreas", "G__Alphatorquevirus_Present_Pancreas",
"G__Alphatorquevirus_NotPresent_Skin", "G__Alphatorquevirus_Present_Skin",
"G__Alphatorquevirus_NotPresent_Urothelial tract", "G__Alphatorquevirus_Present_Urothelial tract"
), ABCD = c(3L, 2L, 17L, 6L, 34L, 18L, 240L, 53L, 321L, 73L,
87L, 25L, 6L, 3L, 20L, 8L, 15L, 7L, 19L, 4L, 265L, 42L, 6L, 1L,
4L, 2L, 22L, 4L, 70L, 13L, 54L, 12L, 116L, 33L, 58L, 11L, 6L,
2L, 26L, 6L, 42L, 8L, 74L, 18L, 19L, 3L, 52L, 0L, 288L, 5L, 377L,
17L, 110L, 2L, 19L, 3L, 21L, 2L, 298L, 9L, 60L, 6L, 68L, 1L,
89L, 3L), Total = c(15L, 15L, 53L, 53L, 183L, 183L, 678L, 678L,
473L, 473L, 123L, 123L, 20L, 20L, 55L, 55L, 118L, 118L, 51L,
51L, 402L, 402L, 23L, 23L, 31L, 31L, 76L, 76L, 126L, 126L, 100L,
100L, 291L, 291L, 308L, 308L, 59L, 59L, 38L, 38L, 96L, 96L, 137L,
137L, 72L, 72L, 183L, 183L, 678L, 678L, 473L, 473L, 123L, 123L,
118L, 118L, 51L, 51L, 402L, 402L, 100L, 100L, 308L, 308L, 137L,
137L), Merge = c("Abiotrophia_Anus", "Abiotrophia_Anus", "Abiotrophia_Bile duct",
"Abiotrophia_Bile duct", "Abiotrophia_Bone/Soft tissue", "Abiotrophia_Bone/Soft tissue",
"Abiotrophia_Breast", "Abiotrophia_Breast", "Abiotrophia_Colorectum",
"Abiotrophia_Colorectum", "Abiotrophia_Esophagus", "Abiotrophia_Esophagus",
"Abiotrophia_Gallbladder", "Abiotrophia_Gallbladder", "Abiotrophia_Head and neck",
"Abiotrophia_Head and neck", "Abiotrophia_Kidney", "Abiotrophia_Kidney",
"Abiotrophia_Liver", "Abiotrophia_Liver", "Abiotrophia_Lung",
"Abiotrophia_Lung", "Abiotrophia_Lymphoid tissue", "Abiotrophia_Lymphoid tissue",
"Abiotrophia_Mesothelium", "Abiotrophia_Mesothelium", "Abiotrophia_Nervous system",
"Abiotrophia_Nervous system", "Abiotrophia_Ovary", "Abiotrophia_Ovary",
"Abiotrophia_Pancreas", "Abiotrophia_Pancreas", "Abiotrophia_Prostate",
"Abiotrophia_Prostate", "Abiotrophia_Skin", "Abiotrophia_Skin",
"Abiotrophia_Small intestine", "Abiotrophia_Small intestine",
"Abiotrophia_Stomach", "Abiotrophia_Stomach", "Abiotrophia_Unknown",
"Abiotrophia_Unknown", "Abiotrophia_Urothelial tract", "Abiotrophia_Urothelial tract",
"Abiotrophia_Uterus", "Abiotrophia_Uterus", "Alphatorquevirus_Bone/Soft tissue",
"Alphatorquevirus_Bone/Soft tissue", "Alphatorquevirus_Breast",
"Alphatorquevirus_Breast", "Alphatorquevirus_Colorectum", "Alphatorquevirus_Colorectum",
"Alphatorquevirus_Esophagus", "Alphatorquevirus_Esophagus", "Alphatorquevirus_Kidney",
"Alphatorquevirus_Kidney", "Alphatorquevirus_Liver", "Alphatorquevirus_Liver",
"Alphatorquevirus_Lung", "Alphatorquevirus_Lung", "Alphatorquevirus_Pancreas",
"Alphatorquevirus_Pancreas", "Alphatorquevirus_Skin", "Alphatorquevirus_Skin",
"Alphatorquevirus_Urothelial tract", "Alphatorquevirus_Urothelial tract"
)), row.names = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 10L, 9L, 12L,
11L, 13L, 14L, 16L, 15L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L,
25L, 26L, 28L, 27L, 29L, 30L, 31L, 32L, 34L, 33L, 35L, 36L, 38L,
37L, 40L, 39L, 42L, 43L, 45L, 44L, 47L, 46L, 1011L, 1012L, 1014L,
1013L, 1015L, 1016L, 1017L, 1018L, 1019L, 1020L, 1022L, 1021L,
1023L, 1024L, 1026L, 1025L, 1027L, 1028L, 1029L, 1030L), class = "data.frame")
This is probably not an answer but it should help to improve you code. If I'm terribly wrong, I'll remove my answer right away. I have loeft out the test business which I don't understand, but your problem seems to be extraction.
The first thing is that you need to remove the quotation marks in your grep command, try:
varlist <- c("Abiotrophia","Alphatorquevirus")
for( i in varlist )
{
# extract rows which contain the variable
fish.test <- fish.test0[ grep( i, fish.test0$group ), ]
print( head( fish.test ) )
}
From what I understand, you need to define column and tests outside your loop. Does that give you more of what you want:
varlist <- c("Abiotrophia","Alphatorquevirus")
column <- "ACDC"
tests <- list()
for( i in 1 : length( varlist ) ) # index can be used later to fill the list
{
# extract rows which contain the variable
fish.test <- fish.test0[ grep( varlist[ i ], fish.test0$group ), ]
# add a column with your name of choice
fish.test <- cbind( fish.test, c( 1: length( fish.test$group ) ) )
colnames( fish.test )[ length( fish.test ) ] <- column
# write each result into your defined list
tests[[ i ]] <- fish.test
}
I have a shinyscript prepared where i want to show a graph based on two widgets.
The first widget(selectInput) controls for the area i want to show a diagnostic plot for.
The second widget (checkboxGroupInput) controls for the amount of data i want to show for the area selected with the first widget. So, the options for the checkboxes depend on what is selected with the selectInput.
I solved this with a htmlOutput("") in the UI and a corresponding renderUI in the server.
Everything works fine, but when i proceed to the plotting, something weird happens.
I can use a reactive filter to control for the area as selected with selectInput, but when i extend the filter to also work work with the checkboxGroupInputi get the following error when i run the app:
Warning: Error in : Problem with filter() input ..2.
x Input ..2 must be of size 611 or 1, not size 0.
i Input ..2 is Code == input$code.
202:
This only shows when all the checkboxes are unchecked and no graph is visible. I Can plot the graph that corresponds with the checkboxes, but it only shows 5 barcharts (when for example ten are to be plotted) and the error is given.
Can someone tell me if there is something wrong with m code? And how i can resolve the error and work with these dependand widgets?
Below my code and data
Code
#libraries needed
library(shiny)
library(ggplot2)
library(dplyr)
#data needed
df <- "load in data"
# user interface ----
ui <- fluidPage(
tabsetPanel(
tabPanel("diagnostische tabellen",fluid = TRUE,
titlePanel("PQ analyse"),
sidebarLayout(
sidebarPanel(
helpText("selecteer terrein waar je de PQ-data van wil bekijken"),
#make first dropdownmenu for area
selectInput("terrein",
label = "Kies een terrein",
choices = sort((unique(df$Terrein))),
selected = 1),
htmlOutput("code")
),
mainPanel(plotOutput("map1"))))
)
)
# Server logic ----------------------------
server <- function(input, output){
# ceate a reactive list of PQ-codes based on previous selection for area
output$code <- renderUI({
data_available <- df[df$Terrein == input$terrein, "Code"]
checkboxGroupInput("code",
label = "PQ-code",
choices = sort(unique(data_available)),
selected = unique(data_available))
})
## filter the data for the graph
filtered_data <- reactive({
filter(df, Terrein == input$terrein, Code == input$code)
})
## GGplot graph
output$map1 <- renderPlot({
ggplot(filtered_data(), aes( x = Code, fill = as.character(Jaar))) +
geom_bar(position = position_stack(reverse = TRUE))+
theme(axis.text.x = element_text(angle = 45, size = 15))+
scale_fill_brewer()+
labs(fill='Jaar')+
ggtitle(paste("Aantal herhalingen PQ's op",input$terrein))
})
}
# Run app
shinyApp(ui, server)
df
structure(list(Terrein = structure(c(25L, 25L, 25L, 25L, 1L,
1L, 1L, 1L, 1L, 1L, 29L, 29L, 13L, 13L, 13L, 7L, 7L, 7L, 7L,
7L, 7L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 8L, 8L, 8L, 13L, 8L, 8L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L,
13L, 13L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 15L, 15L, 15L, 15L,
16L, 16L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 13L, 13L,
13L, 13L, 24L, 24L, 24L, 24L, 28L, 28L, 28L, 28L, 2L, 2L, 2L,
2L, 2L, 2L, 23L, 23L, 23L, 23L, 23L, 22L, 21L, 21L, 21L, 21L,
21L, 7L, 7L, 7L, 7L, 7L, 7L, 20L, 20L, 20L, 20L, 20L, 20L, 20L,
14L, 14L, 14L, 14L, 14L, 14L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
18L, 18L, 18L, 18L, 30L, 30L, 30L, 30L, 20L, 10L, 10L, 10L, 10L,
10L, 13L, 13L, 13L, 6L, 6L, 6L, 6L, 6L, 5L, 5L, 5L, 3L, 3L, 3L,
3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 25L, 1L, 1L, 17L, 17L, 17L, 17L,
13L, 13L, 13L, 13L, 13L, 13L, 23L, 23L, 23L, 23L, 23L, 3L, 3L,
3L, 13L, 3L, 10L, 10L, 25L, 25L, 25L, 25L, 14L, 14L, 14L, 14L,
14L, 14L, 23L, 23L, 23L, 23L, 23L, 15L, 15L, 15L, 15L, 16L, 16L,
16L, 5L, 5L, 5L, 5L, 5L, 12L, 12L, 12L, 12L, 12L, 19L, 15L, 15L,
15L, 15L, 9L, 16L, 16L, 16L, 8L, 19L, 16L, 19L, 8L, 8L, 16L,
16L, 16L, 8L, 8L, 8L, 8L, 8L, 19L, 16L, 19L, 8L, 16L, 16L, 16L,
8L, 16L, 25L, 15L, 15L, 15L, 15L, 15L, 15L, 25L, 21L, 21L, 21L,
7L, 7L, 7L, 12L, 12L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, 10L, 10L, 10L, 15L, 15L, 28L, 28L,
28L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 25L, 25L, 25L, 7L, 7L,
7L, 22L, 23L, 23L, 23L, 23L, 23L, 1L, 1L, 1L, 1L, 1L, 23L, 23L,
23L, 23L, 15L, 15L, 15L, 15L, 29L, 29L, 26L, 26L, 26L, 26L, 26L,
26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L,
26L, 7L, 7L, 7L, 5L, 5L, 5L, 5L, 5L, 20L, 12L, 12L, 8L, 20L,
20L, 20L, 20L, 7L, 7L, 7L, 12L, 25L, 25L, 25L, 24L, 24L, 24L,
20L, 20L, 15L, 15L, 15L, 15L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 15L, 15L,
15L, 15L, 14L, 14L, 14L, 14L, 14L, 14L, 12L, 8L, 8L, 8L, 8L,
21L, 21L, 21L, 12L, 10L, 2L, 1L, 1L, 1L, 1L, 1L, 10L, 10L, 15L,
15L, 15L, 15L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L,
26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 6L, 6L, 6L, 6L,
6L, 14L, 14L, 14L, 14L, 23L, 23L, 23L, 23L, 15L, 15L, 15L, 15L,
15L, 15L, 15L, 15L, 21L, 21L, 21L, 26L, 26L, 26L, 25L, 25L, 23L,
23L, 23L, 23L, 26L, 26L, 26L, 13L, 15L, 15L, 15L, 15L, 10L, 10L,
10L, 10L, 26L, 26L, 26L, 13L, 13L, 13L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 23L, 23L, 23L, 23L, 23L, 1L, 1L, 1L, 1L,
1L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 15L, 15L, 15L, 15L, 23L,
23L, 23L, 23L, 23L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L, 13L,
25L, 25L, 21L, 21L, 21L, 12L, 13L, 13L, 13L, 13L, 2L), .Label = c("Arnhemse Heide",
"ASK Doornspijkse Heide", "ASK Oldenbroekse Heide", "Balloërveld",
"Convooi AOCS Nieuw-Milligen", "De Dellen", "de Kom", "De Stompert & Vlasakkers",
"Deelen, VB", "Eder- en Ginkelse Heide", "Ermelosche Heide",
"Havelte", "ISK Harskamp", "Joost Dourleinkazerne", "Kruispeel en Achterbroek",
"Leusderheide", "Luitenant-Generaal Best Kazerne (vml. VB de Peel)",
"Olst-Welsum", "Oude Kamp", "Oude Molen", "Radiostation Noordwijk",
"Rucphense Heide", "Schinveldse Bossen", "Stroese Zand", "Uilenbosch (Waalsdorp)",
"Vliehors", "Vughtse Heide", "Weerter- en Bosoverheide", "Woensdrechtse Heide",
"Zwaluwenberg"), class = "factor"), Code = structure(c(230L,
228L, 228L, 231L, 4L, 5L, 6L, 1L, 2L, 3L, 239L, 240L, 100L, 101L,
102L, 116L, 117L, 118L, 119L, 120L, 121L, 10L, 7L, 8L, 9L, 10L,
11L, 12L, 13L, 26L, 27L, 28L, 30L, 29L, 14L, 15L, 16L, 23L, 24L,
25L, 17L, 18L, 19L, 20L, 21L, 22L, 44L, 45L, 46L, 47L, 48L, 49L,
216L, 217L, 218L, 102L, 214L, 215L, 31L, 42L, 35L, 36L, 37L,
38L, 43L, 32L, 33L, 34L, 39L, 40L, 41L, 71L, 71L, 72L, 59L, 60L,
61L, 62L, 57L, 65L, 63L, 64L, 58L, 55L, 56L, 67L, 68L, 68L, 69L,
70L, 70L, 91L, 92L, 78L, 79L, 80L, 73L, 74L, 75L, 76L, 77L, 103L,
100L, 105L, 108L, 102L, 101L, 104L, 109L, 107L, 106L, 94L, 95L,
93L, 96L, 99L, 97L, 98L, 122L, 123L, 124L, 125L, 135L, 136L,
225L, 222L, 219L, 220L, 221L, 223L, 226L, 224L, 227L, 106L, 105L,
107L, 104L, 188L, 189L, 186L, 187L, 236L, 235L, 237L, 238L, 55L,
56L, 57L, 58L, 59L, 60L, 176L, 177L, 178L, 179L, 180L, 175L,
143L, 144L, 145L, 146L, 147L, 116L, 119L, 117L, 118L, 121L, 120L,
163L, 165L, 160L, 161L, 162L, 164L, 166L, 111L, 110L, 112L, 113L,
114L, 115L, 84L, 85L, 86L, 87L, 88L, 89L, 90L, 167L, 168L, 169L,
170L, 241L, 242L, 242L, 242L, 160L, 73L, 74L, 77L, 80L, 75L,
103L, 105L, 107L, 50L, 51L, 52L, 53L, 54L, 138L, 139L, 140L,
151L, 152L, 153L, 158L, 159L, 1L, 2L, 3L, 4L, 5L, 6L, 231L, 1L,
2L, 171L, 172L, 173L, 174L, 100L, 102L, 108L, 101L, 109L, 104L,
176L, 177L, 178L, 179L, 180L, 154L, 155L, 156L, 106L, 157L, 79L,
78L, 230L, 229L, 228L, 230L, 115L, 114L, 113L, 112L, 110L, 111L,
176L, 177L, 178L, 179L, 180L, 122L, 123L, 124L, 125L, 137L, 135L,
136L, 141L, 142L, 138L, 139L, 140L, 97L, 95L, 96L, 99L, 98L,
150L, 126L, 127L, 128L, 129L, 190L, 133L, 134L, 132L, 213L, 148L,
131L, 149L, 211L, 212L, 133L, 134L, 132L, 210L, 213L, 210L, 212L,
211L, 148L, 131L, 149L, 210L, 134L, 133L, 132L, 213L, 130L, 231L,
125L, 128L, 129L, 127L, 126L, 124L, 231L, 145L, 144L, 143L, 118L,
120L, 117L, 93L, 94L, 160L, 161L, 166L, 165L, 164L, 163L, 162L,
89L, 88L, 85L, 84L, 90L, 86L, 87L, 79L, 78L, 91L, 123L, 122L,
238L, 237L, 235L, 92L, 80L, 75L, 74L, 76L, 77L, 73L, 232L, 233L,
234L, 119L, 121L, 116L, 175L, 176L, 177L, 179L, 180L, 178L, 2L,
3L, 5L, 4L, 1L, 176L, 178L, 179L, 180L, 126L, 127L, 128L, 129L,
239L, 240L, 191L, 192L, 193L, 194L, 195L, 196L, 197L, 198L, 199L,
200L, 201L, 202L, 203L, 204L, 205L, 206L, 207L, 208L, 209L, 116L,
121L, 119L, 138L, 142L, 141L, 139L, 140L, 161L, 94L, 95L, 183L,
166L, 165L, 160L, 163L, 117L, 120L, 118L, 93L, 233L, 234L, 232L,
189L, 187L, 186L, 162L, 164L, 128L, 126L, 129L, 127L, 74L, 75L,
80L, 76L, 77L, 73L, 79L, 78L, 91L, 92L, 100L, 103L, 108L, 101L,
109L, 106L, 105L, 104L, 123L, 124L, 125L, 122L, 115L, 114L, 113L,
112L, 111L, 110L, 97L, 182L, 184L, 185L, 181L, 145L, 144L, 143L,
96L, 82L, 66L, 2L, 3L, 4L, 5L, 1L, 83L, 81L, 128L, 129L, 126L,
127L, 209L, 206L, 207L, 208L, 191L, 192L, 193L, 194L, 203L, 204L,
205L, 198L, 197L, 196L, 195L, 202L, 201L, 199L, 200L, 52L, 51L,
53L, 50L, 54L, 112L, 115L, 114L, 110L, 180L, 179L, 176L, 178L,
122L, 124L, 126L, 127L, 128L, 129L, 123L, 125L, 145L, 144L, 143L,
192L, 195L, 195L, 233L, 234L, 178L, 176L, 180L, 179L, 191L, 194L,
197L, 103L, 128L, 129L, 126L, 127L, 80L, 76L, 79L, 78L, 193L,
198L, 200L, 101L, 100L, 108L, 81L, 83L, 82L, 73L, 74L, 75L, 77L,
91L, 92L, 176L, 177L, 178L, 180L, 179L, 1L, 2L, 3L, 4L, 5L, 93L,
94L, 95L, 96L, 99L, 98L, 97L, 128L, 129L, 126L, 127L, 176L, 178L,
177L, 179L, 180L, 94L, 97L, 95L, 96L, 105L, 107L, 106L, 109L,
104L, 233L, 234L, 143L, 144L, 145L, 93L, 108L, 101L, 100L, 103L,
58L), .Label = c("AhQ001", "AhQ002", "AhQ003", "AhQ004", "AhQ005",
"AhQ006", "BvB001", "BvB002", "BvB003", "BvB028", "BvB029", "BvB033",
"BvB034", "BvExA1", "BvExA2", "BvExA3", "BvExB1", "BvExB2", "BvExB3",
"BvExC1", "BvExC2", "BvExC3", "BvExD1", "BvExD2", "BvExD3", "BvQ004",
"BvQ005", "BvQ006", "BvQ008", "BvQ009", "BvQ028", "BvQ029", "BvQ030",
"BvQ031", "BvQ056", "BvQ057", "BvQ061", "BvQ062", "BvQ074", "BvQ075",
"BvQ076", "BvQ077", "BvQ078", "BvQ104", "BvQ105", "BvQ120", "BvQ121",
"BvQ182", "BvQ183", "DeQ001", "DeQ002", "DeQ003", "DeQ004", "DeQ005",
"DsQ001", "DsQ002", "DsQ003", "DsQ004", "DsQ005", "DsQ006", "DsQ007",
"DsQ008", "DsQ009", "DsQ010", "DsQ011", "DsQ023", "DsQB01", "DsQB02",
"DsQB03", "DsQB04", "DsQB05", "DsQB06", "EhQ001", "EhQ002", "EhQ003",
"EhQ004", "EhQ005", "EhQ006", "EhQ007", "EhQ008", "EhQJ01", "EhQJ02",
"EhQJ03", "ErQ001", "ErQ002", "ErQ003", "ErQ004", "ErQ005", "ErQ006",
"ErQ007", "GiQ001", "GiQ002", "HaQ001", "HaQ002", "HaQ003", "HaQ004",
"HaQ005", "HaQ006", "HaQ007", "HkQ001", "HkQ002", "HkQ003", "HkQ004",
"HkQ005", "HkQ006", "HkQ007", "HkQ008", "HkQ009", "HkQ010", "JdQ001",
"JdQ002", "JdQ003", "JdQ004", "JdQ005", "JdQ006", "KoQ001", "KoQ002",
"KoQ003", "KoQ004", "KoQ005", "KoQ006", "KrQ001", "KrQ002", "KrQ003",
"KrQ004", "KrQ005", "KrQ006", "KrQ007", "KrQ008", "LhH004", "LhPro1",
"LhPro2", "LhPro4", "LhPRro3", "LhQ001", "LhQ002", "LhX031",
"NmQ001", "NmQ002", "NmQ003", "NmQ004", "NmQ005", "NrQ001", "NrQ002",
"NrQ003", "NrQ004", "NrQ005", "OkPro1", "OkPro2", "OkQ001", "OlQ001",
"OlQ002", "OlQ003", "OlQ004", "OlQ005", "OlQ006", "OlQ007", "OlR001",
"OlR002", "OmQ001", "OmQ002", "OmQ003", "OmQ004", "OmQ005", "OmQ006",
"OmQ007", "OwQ001", "OwQ002", "OwQ003", "OwQ004", "PeH011", "PeH012",
"PeH013", "PeH014", "RhQ001", "SbQ001", "SbQ002", "SbQ003", "SbQ004",
"SbQ005", "StQ001", "StQ002", "StQ003", "StQ004", "StQ005", "SzQ001",
"SzQ002", "SzQ003", "SzQ004", "VdR070", "VhQ001", "VhQ002", "VhQ003",
"VhQ004", "VhQ005", "VhQ006", "VhQ007", "VhQ008", "VhQ009", "VhQ010",
"VhQ011", "VhQ012", "VhQ013", "VhQ014", "VhQ015", "VhQ016", "VhQ017",
"VhQ018", "VhQ019", "VlPro1", "VlPro2", "VlPro3", "VlPro4", "VlQ001",
"VlQ002", "VlQ003", "VlQ004", "VlQ005", "VuQ001", "VuQ002", "VuQ003",
"VuQ004", "VuQ005", "VuQ006", "VuT001", "VuT002", "VuT003", "WaQ001",
"WaQ002", "WaQ003", "WaQ004", "WaQ005", "WaQ006", "WaQ007", "WeQ001",
"WeQ002", "WeQ003", "WeQ004", "WhQ001", "WhQ002", "ZwQ001", "ZwQ002"
), class = "factor")), row.names = c(NA, -611L), class = "data.frame")
As you have multiple Codes for each Terrein, you should use %in%. Also, you need to define each bar count. Try this
# user interface ----
ui <- fluidPage(
tabsetPanel(
tabPanel("diagnostische tabellen",fluid = TRUE,
titlePanel("PQ analyse"),
sidebarLayout(
sidebarPanel(
helpText("selecteer terrein waar je de PQ-data van wil bekijken"),
#make first dropdownmenu for area
selectInput("terrein",
label = "Kies een terrein",
choices = sort((unique(df$Terrein))),
selected = 1),
uiOutput("mycode")
),
mainPanel(plotOutput("map1"))))
)
)
# Server logic ----------------------------
server <- function(input, output){
# ceate a reactive list of PQ-codes based on previous selection for area
output$mycode <- renderUI({
data_available <- df[df$Terrein == input$terrein, "Code"]
checkboxGroupInput("code",
label = "PQ-code",
choices = sort(unique(data_available)),
selected = unique(data_available))
})
## filter the data for the graph
filtered_data <- reactive({
dat <- filter(df, Terrein == input$terrein & Code %in% input$code)
data <- dat %>% group_by(Code) %>%
dplyr::summarise(n=n())
data
})
## GGplot graph
output$map1 <- renderPlot({
ggplot(filtered_data(), aes( x = Code, y=n, fill = Code )) +
geom_bar(position = position_stack(reverse = TRUE), stat = "identity")+
theme(axis.text.x = element_text(angle = 45, size = 15))+
scale_fill_brewer()+
labs(fill=NULL)+
ggtitle(paste("Aantal herhalingen PQ's op",input$terrein))
})
}
# Run app
shinyApp(ui, server)
You will get this output:
Please note that there is no Jaar defined, so you may need to define it.
I'm running a report, and have pulled out some data around something happening over time. I have then summarised that data into a dataframe of date/times and counts. When I try to plot it via ggplot2 I get an error
> ggplot(foo, aes(x=Date, y=Count))+
+ geom_line()
Error in cut.default(unclass(x), unclass(breaks), labels = labels, right = right, :
cannot allocate vector of length 1317423601
>
> ggplot(foo[2:349,], aes(x=Date, y=Count))+
+ geom_line()
Plot produced...
My data is using POSIXct dates, which do seem to cover the end of British Summer Time. I also note that if I excldue the first row of data it works !. Any ideas ?
Here's my data
> dput(foo)
structure(list(Date = structure(c(1317423600, 1317445200, 1317466800,
1317488400, 1317510000, 1317553200, 1317574800, 1317596400, 1317618000,
1317639600, 1317661200, 1317682800, 1317704400, 1317726000, 1317747600,
1317769200, 1317790800, 1317812400, 1317834000, 1317855600, 1317877200,
1317898800, 1317920400, 1317942000, 1317963600, 1317985200, 1318006800,
1318028400, 1318050000, 1318071600, 1318093200, 1318114800, 1318136400,
1318158000, 1318179600, 1318201200, 1318222800, 1318244400, 1318266000,
1318287600, 1318309200, 1318330800, 1318352400, 1318374000, 1318395600,
1318417200, 1318438800, 1318460400, 1318503600, 1318525200, 1318546800,
1318568400, 1318590000, 1318611600, 1318633200, 1318654800, 1318676400,
1318698000, 1318719600, 1318762800, 1318784400, 1318806000, 1318827600,
1318849200, 1318870800, 1318892400, 1318914000, 1318935600, 1318957200,
1318978800, 1319000400, 1319022000, 1319043600, 1319065200, 1319086800,
1319108400, 1319130000, 1319151600, 1319173200, 1319194800, 1319216400,
1319238000, 1319259600, 1319281200, 1319302800, 1319324400, 1319346000,
1319367600, 1319410800, 1319432400, 1319454000, 1319475600, 1319497200,
1319518800, 1319540400, 1319562000, 1319583600, 1319605200, 1319626800,
1319648400, 1319670000, 1319691600, 1319713200, 1319734800, 1319756400,
1319778000, 1319799600, 1319821200, 1319842800, 1319864400, 1319886000,
1319907600, 1319929200, 1319994000, 1320015600, 1320037200, 1320058800,
1320080400, 1320102000, 1320123600, 1320145200, 1320166800, 1320188400,
1320210000, 1320231600, 1320253200, 1320274800, 1320296400, 1320318000,
1320339600, 1320361200, 1320382800, 1320404400, 1320426000, 1320447600,
1320469200, 1320490800, 1320512400, 1320534000, 1320577200, 1320598800,
1320620400, 1320642000, 1320663600, 1320685200, 1320706800, 1320750000,
1320771600, 1320793200, 1320814800, 1320836400, 1320858000, 1320879600,
1320901200, 1320922800, 1320944400, 1320966000, 1320987600, 1321009200,
1321030800, 1321052400, 1321074000, 1321095600, 1321117200, 1321138800,
1321182000, 1321203600, 1321225200, 1321246800, 1321268400, 1321290000,
1321311600, 1321333200, 1321354800, 1321376400, 1321398000, 1321419600,
1321441200, 1321462800, 1321484400, 1321506000, 1321527600, 1321549200,
1321570800, 1321592400, 1321614000, 1321635600, 1321657200, 1321678800,
1321700400, 1321722000, 1321743600, 1321765200, 1321786800, 1321808400,
1321830000, 1321851600, 1321873200, 1321894800, 1321916400, 1321938000,
1321959600, 1321981200, 1322002800, 1322024400, 1322046000, 1322067600,
1322089200, 1322110800, 1322132400, 1322154000, 1322175600, 1322197200,
1322218800, 1322240400, 1322262000, 1322305200, 1322326800, 1322370000,
1322391600, 1322413200, 1322434800, 1322456400, 1322478000, 1322499600,
1322521200, 1322542800, 1322564400, 1322586000, 1322607600, 1322629200,
1322650800, 1322672400, 1322694000, 1322715600, 1322737200, 1322758800,
1322780400, 1322802000, 1322823600, 1322845200, 1322866800, 1322888400,
1322910000, 1322931600, 1322953200, 1322974800, 1322996400, 1323018000,
1323039600, 1323061200, 1323082800, 1323104400, 1323126000, 1323147600,
1323169200, 1323190800, 1323212400, 1323234000, 1323255600, 1323277200,
1323298800, 1323320400, 1323342000, 1323363600, 1323385200, 1323406800,
1323428400, 1323450000, 1323471600, 1323493200, 1323514800, 1323558000,
1323579600, 1323601200, 1323622800, 1323644400, 1323666000, 1323687600,
1323709200, 1323730800, 1323752400, 1323774000, 1323795600, 1323817200,
1323838800, 1323860400, 1323882000, 1323903600, 1323925200, 1323946800,
1323968400, 1323990000, 1324011600, 1324033200, 1324054800, 1324076400,
1324098000, 1324119600, 1324141200, 1324162800, 1324206000, 1324227600,
1324249200, 1324270800, 1324292400, 1324314000, 1324335600, 1324357200,
1324378800, 1324400400, 1324422000, 1324443600, 1324465200, 1324486800,
1324508400, 1324530000, 1324551600, 1324573200, 1324594800, 1324616400,
1324638000, 1324659600, 1324681200, 1324702800, 1324724400, 1324767600,
1324832400, 1324854000, 1324875600, 1324897200, 1324918800, 1324940400,
1324962000, 1324983600, 1325005200, 1325026800, 1325048400, 1325070000,
1325091600, 1325113200, 1325134800, 1325156400, 1325178000, 1325199600,
1325221200, 1325242800, 1325264400, 1325286000), class = c("POSIXct",
"POSIXt"), tzone = ""), Count = c(3L, 0L, 9L, 1L, 0L, 1L, 6L,
4L, 4L, 52L, 19L, 7L, 5L, 59L, 30L, 3L, 2L, 50L, 25L, 8L, 4L,
41L, 22L, 4L, 8L, 57L, 12L, 14L, 3L, 10L, 2L, 6L, 0L, 1L, 7L,
10L, 12L, 44L, 19L, 11L, 3L, 47L, 31L, 7L, 9L, 56L, 21L, 11L,
54L, 20L, 10L, 6L, 54L, 17L, 0L, 1L, 11L, 2L, 0L, 2L, 4L, 14L,
9L, 52L, 19L, 11L, 10L, 56L, 33L, 12L, 9L, 57L, 20L, 12L, 5L,
51L, 23L, 14L, 5L, 50L, 9L, 1L, 2L, 5L, 7L, 1L, 0L, 3L, 9L, 13L,
57L, 27L, 10L, 7L, 62L, 29L, 5L, 6L, 53L, 22L, 8L, 10L, 53L,
12L, 7L, 9L, 38L, 8L, 1L, 0L, 9L, 3L, 0L, 6L, 9L, 3L, 81L, 27L,
6L, 5L, 67L, 45L, 6L, 3L, 63L, 43L, 10L, 2L, 57L, 38L, 19L, 12L,
54L, 22L, 1L, 0L, 5L, 4L, 0L, 4L, 2L, 10L, 4L, 53L, 44L, 7L,
65L, 41L, 11L, 7L, 61L, 46L, 7L, 4L, 64L, 48L, 10L, 11L, 56L,
39L, 6L, 1L, 4L, 3L, 0L, 7L, 3L, 10L, 4L, 46L, 45L, 16L, 6L,
69L, 46L, 17L, 1L, 67L, 43L, 15L, 5L, 57L, 40L, 14L, 4L, 56L,
36L, 3L, 0L, 11L, 3L, 0L, 1L, 5L, 2L, 5L, 9L, 59L, 45L, 9L, 7L,
71L, 35L, 19L, 10L, 65L, 23L, 5L, 7L, 10L, 2L, 5L, 6L, 5L, 1L,
0L, 3L, 2L, 0L, 5L, 7L, 10L, 8L, 58L, 46L, 16L, 6L, 70L, 52L,
14L, 8L, 84L, 42L, 10L, 6L, 62L, 44L, 11L, 3L, 58L, 28L, 3L,
0L, 9L, 8L, 1L, 0L, 4L, 2L, 10L, 11L, 65L, 53L, 14L, 11L, 73L,
42L, 14L, 8L, 74L, 33L, 15L, 13L, 75L, 53L, 11L, 10L, 61L, 31L,
1L, 2L, 9L, 2L, 0L, 3L, 1L, 15L, 16L, 114L, 52L, 10L, 14L, 75L,
50L, 14L, 9L, 69L, 52L, 12L, 15L, 77L, 35L, 10L, 5L, 69L, 37L,
5L, 1L, 12L, 10L, 0L, 9L, 4L, 14L, 18L, 90L, 35L, 12L, 9L, 87L,
49L, 10L, 22L, 83L, 41L, 15L, 14L, 79L, 48L, 17L, 6L, 40L, 8L,
2L, 0L, 2L, 0L, 1L, 7L, 2L, 1L, 2L, 6L, 2L, 54L, 31L, 11L, 6L,
54L, 32L, 7L, 9L, 63L, 23L, 16L, 6L, 43L, 17L, 3L)), .Names = c("Date",
"Count"), row.names = c(1L, 2L, 3L, 4L, 5L, 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, 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, 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, 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, 147L, 148L, 149L, 150L,
151L, 152L, 153L, 155L, 156L, 157L, 158L, 159L, 160L, 161L, 162L,
163L, 164L, 165L, 166L, 167L, 168L, 169L, 170L, 171L, 172L, 173L,
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, 227L, 228L, 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, 285L, 286L, 287L,
288L, 289L, 290L, 291L, 292L, 293L, 294L, 295L, 296L, 297L, 298L,
299L, 300L, 301L, 302L, 303L, 304L, 305L, 306L, 307L, 308L, 309L,
310L, 311L, 312L, 313L, 315L, 316L, 317L, 318L, 319L, 320L, 321L,
322L, 323L, 324L, 325L, 326L, 327L, 328L, 329L, 330L, 331L, 332L,
333L, 334L, 335L, 336L, 337L, 338L, 339L, 341L, 344L, 345L, 346L,
347L, 348L, 349L, 350L, 351L, 352L, 353L, 354L, 355L, 356L, 357L,
358L, 359L, 360L, 361L, 362L, 363L, 364L, 365L), class = "data.frame")
and here's my original code
ggplot(foo, aes(x=Date, y=Count))+
geom_line()