ggplot2 Add geom line for each facet in bland altman plot - r

I have the following data frame
structure(list(Lightbox = c(84L, 67L, 80L, 63L, 76L, 66L, 79L,
81L, 77L, 82L, 84L, 67L, 80L, 63L, 76L, 66L, 79L, 81L, 77L, 82L,
84L, 67L, 80L, 63L, 76L, 66L, 79L, 81L, 77L, 82L, 84L, 67L, 80L,
63L, 76L, 66L, 79L, 81L, 77L, 82L, 84L, 67L, 80L, 63L, 76L, 66L,
79L, 81L, 77L, 82L), variable = structure(c(1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), .Label = c("S1",
"S2", "S3", "S4", "S5"), class = "factor"), value = c(82L, 65L,
73L, 50L, 50L, 50L, 72L, 56L, 76L, 78L, 88L, 66L, 71L, 60L, 54L,
55L, 63L, 68L, 73L, 75L, 73L, 65L, 76L, 57L, 51L, 57L, 75L, 65L,
69L, 66L, 77L, 67L, 79L, 58L, 55L, 56L, 77L, 66L, 73L, 80L, 78L,
62L, 78L, 52L, 63L, 59L, 71L, 64L, 69L, 89L), mean = c(83, 66,
76.5, 56.5, 63, 58, 75.5, 68.5, 76.5, 80, 86, 66.5, 75.5, 61.5,
65, 60.5, 71, 74.5, 75, 78.5, 78.5, 66, 78, 60, 63.5, 61.5, 77,
73, 73, 74, 80.5, 67, 79.5, 60.5, 65.5, 61, 78, 73.5, 75, 81,
81, 64.5, 79, 57.5, 69.5, 62.5, 75, 72.5, 73, 85.5), diff = c(2L,
2L, 7L, 13L, 26L, 16L, 7L, 25L, 1L, 4L, -4L, 1L, 9L, 3L, 22L,
11L, 16L, 13L, 4L, 7L, 11L, 2L, 4L, 6L, 25L, 9L, 4L, 16L, 8L,
16L, 7L, 0L, 1L, 5L, 21L, 10L, 2L, 15L, 4L, 2L, 6L, 5L, 2L, 11L,
13L, 7L, 8L, 17L, 8L, -7L)), .Names = c("Lightbox", "variable",
"value", "mean", "diff"), row.names = c(NA, -50L), class = "data.frame")
I wish to plot a bland altman graph, difference against mean for 5 facet groups S1->S5 which is easy enough
p <- ggplot(df_melt, aes(mean, diff))+ geom_point(na.rm=TRUE)+ facet_wrap(~variable)
However, I would also like to add some geom_hline to each facet showing the mean for each group and the standard deviations. If I had only one group I would do the following:
yintercepts_mean <- c(mean(df_melt$diff, na.rm = TRUE))
yintercepts_mean_r <- round(yintercepts_mean,3)
yintercepts_sd_p <- c(mean(df_melt$diff, na.rm = TRUE) + c(2) * sd(df_melt$diff, na.rm = TRUE))
yintercepts_sd_n <- c(mean(df_melt$diff, na.rm = TRUE) + c(-2) * sd(df_melt$diff, na.rm = TRUE))
yintercepts_sd_p_r <- round(yintercepts_sd_p,3)
yintercepts_sd_n_r <- round(yintercepts_sd_n,3)
#ylabels <- c("- 2SD", "+ 2SD", "Mean")
ylabels <- c("mean")
ylabels2 <- c("+ 2SD")
ylabels3 <- c("- 2SD")
p + geom_hline(yintercept = yintercepts_mean_r, linetype=1, color='blue') +
geom_hline(yintercept = yintercepts_sd_p_r, linetype=2, color='blue') +
geom_hline(yintercept = yintercepts_sd_n_r, linetype=2, color='blue')
How can I incorporate the above when facetting my data?

library(plyr)
df2 <- ddply(df_melt,.(variable),summarise,mean=mean(diff, na.rm = TRUE),
sd=sd(diff, na.rm = TRUE))
library(ggplot2)
p <- ggplot(df_melt, aes(mean, diff)) +
geom_point(na.rm=TRUE) +
geom_hline(data=df2,aes(yintercept=c(round(mean,3),
round(mean+2*sd,3),
round(mean-2*sd,3))),
linetype=c(1,2,2), color='blue') +
facet_wrap(~variable)
print(p)

Related

Creating scatter plot class or group wise

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)

order geom_point by specific facet

I have a ggplot related question, which should be easy but I could not find the answer yet. I am trying to plot a faceted plot with the code below and this dataset (11 kB).
ggplot(plot.dat, aes(x = estimate, y = reorder(countryyear, estimate))) +
geom_point() +
geom_segment(aes(x=conf.low, xend=conf.high, yend=countryyear)) +
facet_grid(. ~ facet) +
xlab("Random Effect Estimate") +
ylab("") + scale_x_continuous(breaks=c(seq(0, 5, 1)), limits=c(0, 5)) +
ggtitle("Random Slopes in Country*Year Groups from Northwestern Europe") +
theme_minimal() + theme(plot.title = element_text(hjust = 0.5))
I would like countryyear to be organized by the values of estimate in the Extreme Right facet. Not quite sure how to order by values of a specific facet. Any ideas are welcome! Thanks.
Update: Here is the dput structure of a random subset of the dataset. It has some missing values, but it should work for the sake of the example. I also updated the download link above, that has the full version.
structure(list(estimate = c(1.41056902925372, 0.854859208455895,
1.16012834593894, 0.871339033194504, 0.803272289946221, 1.17540386134493,
0.996313357490551, 1.49940694539732, 1.33773365908762, 2.7318703090905,
1.19131935418045, 1.12765907711738, 0.746741192261761, 0.985847015192172,
0.912357310925342, 1.11582763712164, 1.21854572824977, 0.675712547978394,
0.566955524699616, 1.32611743759365, 0.519648352294682, 0.591013596394243,
1.30944973684044, 0.613722269599125, 1.13293279727271, 0.950788678552604,
1.1599446923567, 1.11493952112913, 0.95336321045095, 1.39002327097034,
0.794207546872633, 0.788545101449259, 1.01096883872495, 0.897407203907834,
1.38391605229103, 1.35754760293107, 1.0718508539761, 0.542191158958878,
0.757132752456427, 1.44172863221312, 1.04842251986171, 0.77260404885379,
0.879288027642055, 1.09372353598088, 0.745484830381145, 1.21211217249353,
0.628009608902132, 1.34864488674734), countryyear = structure(c(1L,
2L, 4L, 5L, 7L, 9L, 10L, 12L, 13L, 26L, 28L, 29L, 31L, 32L, 34L,
36L, 37L, 39L, 40L, 57L, 59L, 60L, 62L, 63L, 65L, 67L, 68L, 70L,
71L, 73L, 75L, 76L, 89L, 90L, 92L, 94L, 95L, 103L, 104L, 106L,
108L, 109L, 111L, 128L, 130L, 132L, 133L, 135L), .Label = c("AT02",
"AT04", "AT06", "AT14", "AT16", "BE02", "BE04", "BE06", "BE08",
"BE10", "BE12", "BE14", "BE16", "BG06", "BG08", "BG10", "BG12",
"CH14", "CZ02", "CZ04", "CZ08", "CZ10", "CZ12", "CZ14", "CZ16",
"DE02", "DE04", "DE06", "DE08", "DE10", "DE12", "DE14", "DE16",
"DK02", "DK04", "DK06", "DK08", "DK10", "DK12", "DK14", "EE04",
"EE06", "EE08", "EE10", "EE12", "EE14", "EE16", "ES02", "ES04",
"ES06", "ES08", "ES10", "ES12", "ES14", "ES16", "FI02", "FI04",
"FI06", "FI08", "FI10", "FI12", "FI14", "FI16", "FR06", "FR08",
"FR10", "FR12", "FR14", "FR16", "GB02", "GB04", "GB06", "GB08",
"GB10", "GB12", "GB14", "GB16", "GR02", "GR04", "GR08", "GR10",
"HU02", "HU06", "HU08", "HU10", "HU12", "HU14", "HU16", "IE02",
"IE04", "IE06", "IE08", "IE10", "IE12", "IE14", "IE16", "IT04",
"IT12", "IT16", "LT10", "LT12", "LT14", "NL02", "NL04", "NL06",
"NL08", "NL10", "NL12", "NL14", "NL16", "NO14", "PL02", "PL04",
"PL06", "PL08", "PL10", "PL12", "PL14", "PL16", "PT02", "PT04",
"PT06", "PT08", "PT10", "PT12", "PT14", "PT16", "SE02", "SE04",
"SE06", "SE08", "SE10", "SE12", "SE14", "SE16", "SI02", "SI04",
"SI06", "SI08", "SI10", "SI12", "SI14", "SI16", "SK04", "SK06",
"SK08", "SK10", "SK12"), class = "factor"), facet = structure(c(1L,
3L, 1L, 4L, 5L, 3L, 4L, 1L, 1L, 1L, 5L, 5L, 4L, 5L, 3L, 1L, 2L,
4L, 5L, 2L, 1L, 4L, 2L, 5L, 2L, 3L, 4L, 3L, 2L, 5L, 5L, 4L, 2L,
5L, 4L, 5L, 3L, 1L, 4L, 5L, 3L, 5L, 4L, 1L, 5L, 2L, 4L, 1L), .Label = c("Intercept",
"Extreme Left", "Center", "Right", "Extreme Right"), class = "factor"),
conf.low = c(1.16824810706745, 0.686215051613965, 0.910277310292764,
0.591705078386698, 0.37357342399703, 0.947951001435781, 0.663296044193037,
1.18794112232166, 1.06645119085865, 2.33578182814618, 0.580210898576738,
0.564235690522211, 0.530859530342114, 0.516191258265551,
0.730992343373883, 0.862424540370486, 0.827891784352444,
0.427638276259852, 0.275692447335368, 0.829763907986328,
0.370078643492081, 0.321852705445509, 0.83550621863293, 0.289836810427436,
0.847226120408727, 0.780056160572728, 0.873143885861924,
0.869757467125519, 0.615741777890997, 0.649483531741787,
0.349657606457465, 0.523294407847395, 0.670109418373736,
0.36656743494149, 0.952201390937053, 0.777207016700884, 0.888128473009524,
0.397085597526946, 0.479828726362257, 0.614533313431094,
0.813336887981082, 0.3129232351085, 0.61435321820328, 0.854801028643867,
0.346698059397102, 0.805414039007076, 0.434676644041643,
1.07780736338027), conf.high = c(1.70315275860739, 1.06494933995261,
1.47855797769819, 1.28312522319126, 1.7272277157504, 1.45743211956315,
1.49652679976667, 1.8925358720741, 1.67802460909168, 3.19512520208851,
2.44607918797515, 2.25369471581694, 1.05041423643869, 1.8828182806291,
1.13872035780431, 1.44368725318228, 1.79353596677755, 1.06769546329854,
1.16593171156554, 2.11938292490653, 0.729667639003753, 1.08526995489865,
2.05223919950836, 1.29954170985538, 1.51498719434776, 1.15888977865399,
1.54095070825389, 1.4292376699955, 1.47610807594453, 2.97492484321718,
1.80395225460704, 1.18824770090216, 1.52521060717706, 2.19697554354282,
2.01136404338166, 2.37122858469145, 1.29357889999432, 0.740322123703373,
1.19469713534712, 3.38237391450413, 1.35145693795059, 1.90755095606211,
1.25847381058047, 1.39942645489832, 1.60297301142912, 1.82417470710871,
0.907332092210651, 1.68753999308876)), row.names = c(1L,
9L, 17L, 25L, 33L, 41L, 49L, 57L, 65L, 128L, 136L, 144L, 152L,
160L, 168L, 176L, 184L, 192L, 200L, 283L, 291L, 299L, 307L, 315L,
323L, 331L, 339L, 347L, 355L, 363L, 371L, 379L, 442L, 450L, 458L,
466L, 474L, 512L, 520L, 528L, 536L, 544L, 552L, 640L, 648L, 656L,
664L, 672L), class = "data.frame")

Use select_helpers with dplyr::coalesce

I have a very wide dataframe (much larger than the data provided here for reprex).
Using the data provided below (assigned to my_wide_data), I would like to utilize dplyr::coalesce along with the select helpers from dplyr (e.g. dplyr::starts_with).
# dput output assigned to my_wide_data
structure(list(myvar1 = c(10L, 3L, 11L, 2L, 4L, 5L, 2L, 6L, 1L,
4L, 12L, 9L, 12L, 2L, 3L, 1L, 2L, 8L, 1L, 2L, 3L, 3L, 8L, 11L,
10L, 6L, 3L, 10L, 5L, 2L, 8L, 3L, 1L, 6L, 2L, 1L, 8L, 4L, 10L,
3L, 1L, 4L, 2L, 12L, 3L, 2L, 5L, 1L, 3L, 5L, 3L, 2L, 12L, 3L,
6L, 11L, 12L, 2L, 6L, 10L, 3L, 10L, 3L, 2L, 2L, 2L, 2L, 3L, 6L,
3L, 6L, 10L, 1L, 3L, 3L, 6L, 2L, 3L, 3L, 3L, 2L, 3L, 2L, 10L,
3L, 3L, 4L, 1L, 3L, 2L, 3L, 9L, 1L, 1L, NA, 5L, 1L, 8L, 3L, 10L,
3L, 3L, 4L, 7L, 10L, 2L, 2L, 11L, 6L, 11L, 6L, 4L, 4L, 12L, 6L,
6L, 1L, 2L, 11L, 2L, 2L, 11L, 3L, 2L, 3L, 2L, 2L, 3L, 3L, 9L,
2L, 1L, 1L, 4L, 2L, 8L, 2L, 10L, 6L, 3L, 1L, 6L, 2L, 10L, 3L,
5L, 6L, 3L, 4L, 10L, 9L, 3L, 4L, 3L, 2L, 3L, 9L, 3L, 3L, 1L,
10L, 4L, 4L, 6L, 2L, 7L, 3L, 2L, 3L, 1L, 3L, 3L, 3L, 7L, 2L,
2L, 6L, 2L, 4L, 3L, 3L, 4L, 2L, 4L, 2L, 5L, 5L, 3L, 6L, 5L, 4L,
5L, 4L, 4L, 10L, 1L, 9L, 4L, 4L, 4L, 4L, 8L, 6L, 5L), myvar2 = c(24L,
24L, 27L, 8L, 9L, 15L, 1L, 27L, 3L, 23L, 28L, 10L, 24L, 5L, 14L,
17L, 16L, 28L, 29L, 16L, 3L, 13L, 7L, 13L, 18L, 25L, 10L, 10L,
15L, 27L, 21L, 17L, 25L, 25L, 15L, 25L, 21L, 13L, 9L, 28L, 1L,
13L, 19L, 21L, 23L, 15L, NA, 29L, 12L, 25L, 1L, 5L, 12L, 7L,
15L, 25L, 4L, 8L, 30L, 25L, 8L, NA, 6L, 16L, 14L, 7L, 20L, 26L,
19L, 10L, 1L, 15L, 30L, 7L, 16L, 23L, 24L, 21L, 8L, 1L, 1L, 10L,
26L, 28L, 5L, 7L, 21L, 10L, 13L, 26L, 14L, 5L, 22L, 18L, NA,
NA, 9L, 20L, 17L, 23L, 3L, 13L, 7L, 5L, 6L, 9L, 8L, 15L, 9L,
10L, 15L, 13L, NA, 30L, 22L, 14L, 9L, 16L, 6L, 13L, 19L, 15L,
1L, 7L, 19L, 25L, 10L, NA, 8L, 25L, 5L, 2L, 16L, 8L, 19L, 18L,
27L, 2L, NA, 16L, 29L, 4L, 7L, 27L, 24L, 5L, 6L, 17L, 16L, 13L,
11L, NA, 12L, 9L, 8L, 1L, NA, 5L, 12L, 3L, 3L, 10L, 16L, 16L,
5L, 24L, 10L, 17L, 23L, 19L, 12L, 12L, 18L, 6L, 1L, 3L, 15L,
26L, 28L, 28L, 27L, 3L, 18L, 22L, 13L, 11L, 30L, 24L, 1L, 25L,
21L, 7L, 14L, 16L, 9L, 3L, 28L, 11L, 17L, 11L, 25L, 23L, 7L,
21L), myvar3 = c(78L, 79L, 78L, 78L, 79L, 78L, 79L, 77L, 79L,
79L, 76L, 78L, 78L, 79L, 79L, 79L, 79L, 78L, 79L, 79L, 79L, 79L,
78L, 78L, 78L, 79L, 79L, 78L, 78L, 79L, 78L, 79L, 79L, 78L, 79L,
79L, 78L, 78L, 78L, 79L, 79L, 79L, 79L, 78L, 79L, 79L, 73L, 79L,
79L, 79L, 79L, 79L, 72L, 79L, 78L, 78L, 78L, 79L, 78L, 78L, 79L,
78L, 79L, 79L, 79L, 79L, 79L, 78L, 78L, 79L, 78L, 78L, 79L, 79L,
79L, 76L, 79L, 78L, 79L, 79L, 79L, 79L, 79L, 75L, 79L, 79L, 79L,
79L, 79L, 79L, 79L, 78L, 79L, 79L, 77L, 78L, 79L, 78L, 79L, 78L,
79L, 79L, 79L, 78L, 78L, 79L, 79L, 78L, 78L, 78L, 78L, 79L, 79L,
78L, 78L, 76L, 79L, 76L, 77L, 79L, 79L, 78L, 79L, 79L, 79L, 79L,
79L, 79L, 79L, 78L, 78L, 79L, 78L, 79L, 79L, 78L, 79L, 78L, 79L,
79L, 79L, 79L, 79L, 78L, 79L, 79L, 77L, 79L, 79L, 78L, 78L, 79L,
78L, 79L, 79L, 79L, 78L, 79L, 79L, 79L, 78L, 79L, 79L, 78L, 79L,
78L, 79L, 79L, 78L, 79L, 79L, 79L, 79L, 79L, 79L, 79L, 78L, 79L,
78L, 79L, 79L, 79L, 79L, 79L, 78L, 79L, 79L, 79L, 79L, 79L, 79L,
79L, 78L, 79L, 78L, 79L, 78L, 79L, 79L, 79L, 79L, 76L, 78L, 79L
)), class = "data.frame", row.names = c(NA, -204L)) -> my_wide_data
In other words, instead of
my_wide_data %>%
mutate(coalesce_var <- coalesce(myvar1, myvar2, myvar3))
I would like to be able to do something like
my_wide_data %>%
mutate(coalesce_var <- coalesce(starts_with("my")))
QUESTION: Is it possible to accomplish something like this within dplyr or elsewhere in the tidyverse?
The following works by taking advantage that coalesce(...) can accept a list
vecs <- list(
c(1, 2, NA, NA, 5),
c(NA, NA, 3, 4, 5)
)
coalesce(!!! vecs)
Which you can combine with using a helper function in select and turning the resulting selected data frame into a list
my_wide_data %>%
mutate(coalesce_var = coalesce(!!! select(., starts_with("my"))))
# myvar1 myvar2 myvar3 coalesce_var
# 1 10 24 78 10
# 2 3 24 79 3
# 3 11 27 78 11
# 4 2 8 78 2
# 5 4 9 79 4
# etc
EDIT Here's an alternative construction - which I prefer
library(rlang)
library(tidyselect)
my_wide_data %>%
mutate(coalesce_var = coalesce(!!! syms(vars_select(names(.), starts_with("my")))))

ggplot2: Inconsistent color from alpha

I am making several plots that have different x-axis limits, and I want to highlight a region of interest by adding a grey box. Even though I use the same geom_rect() command with the same alpha value in ggplot2, I get results with very different grey colors. I have looked here and here but so far have not figured out how to make these boxes the same level of transparency. Below is a reproducible example (with fake data) and the figures that it produces. Notice the different color of the grey rectangles. I want the grey to be the same across plots.
Data<-structure(list(X = c(34L, 27L, 28L, 47L, 26L, 3L, 13L, 31L, 39L,
16L, 45L, 5L, 49L, 17L, 29L, 43L, 1L, 35L, 41L, 10L, 48L, 24L,
12L, 11L, 30L, 40L, 8L, 4L, 20L, 25L, 50L, 22L, 9L, 21L, 18L,
7L, 15L, 44L, 6L, 36L, 46L, 33L, 2L, 37L, 23L, 14L, 42L, 38L,
19L, 32L, 34L, 27L, 28L, 47L, 26L, 3L, 13L, 31L, 39L, 16L, 45L,
5L, 49L, 17L, 29L, 43L, 1L, 35L, 41L, 10L, 48L, 24L, 12L, 11L,
30L, 40L, 8L, 4L, 20L, 25L, 50L, 22L, 9L, 21L, 18L, 7L, 15L,
44L, 6L, 36L, 46L, 33L, 2L, 37L, 23L, 14L, 42L, 38L, 19L, 32L
), Y = c(130L, 146L, 58L, 110L, 117L, 135L, 133L, 108L, 97L,
61L, 71L, 64L, 103L, 142L, 125L, 104L, 100L, 147L, 111L, 78L,
56L, 145L, 62L, 69L, 70L, 116L, 137L, 79L, 150L, 94L, 91L, 81L,
65L, 118L, 129L, 83L, 98L, 84L, 85L, 148L, 93L, 73L, 59L, 87L,
134L, 88L, 136L, 90L, 140L, 55L, 89L, 115L, 123L, 51L, 132L,
126L, 66L, 80L, 60L, 120L, 109L, 76L, 74L, 57L, 149L, 121L, 138L,
128L, 114L, 127L, 68L, 107L, 67L, 112L, 144L, 119L, 53L, 52L,
54L, 96L, 131L, 106L, 113L, 72L, 95L, 63L, 92L, 86L, 75L, 105L,
82L, 101L, 139L, 143L, 122L, 77L, 99L, 141L, 124L, 102L), B = structure(c(2L,
2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L,
1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L,
1L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L,
1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L,
2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 2L,
2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L,
1L, 1L, 1L), class = "factor", .Label = c("no", "yes"))), .Names = c("X",
"Y", "B"), row.names = c(NA, -100L), class = "data.frame")
Data2<-structure(list(variable = c(2.49676547444708, 0.67359598601097,
0.674751772966082, 0.0317590441796792, 0.485143583939748, 1.08231639527806,
0.0732344181040914, 1.62357048819912, 0.146833215667032, 0.823157103468943,
0.240761579418538, 1.37540376416553), DOY_mid_month = c(15, 46,
75, 106, 136, 167, 197, 228, 259, 289, 320, 350)), .Names = c("variable",
"DOY_mid_month"), row.names = c(NA, -12L), class = "data.frame")
test<-ggplot(data=Data) +
geom_rect(aes(xmin=5, xmax=30, ymin=1, ymax=40), alpha = 0.02) +
geom_point(aes(x = X, y = X, colour= B), data =Data, size=2) +
theme_bw()
test2 <-ggplot(data=Data2) +
geom_rect(aes(xmin=5, xmax=30, ymin=-Inf, ymax=Inf), alpha = 0.02) +
geom_point(aes(x = DOY_mid_month, y = variable), color="black", size=4) +
scale_x_continuous("Day of Year", limits = c(0, 366)) + # Use this to add back X-axis label for the bottom plot in panel
scale_y_continuous(expression(paste("Variable", sep=""))) +
theme_bw()
Plot result from first example:
Plot result from second example:
You are currently drawing one rectangle for each row of the dataset. The more rectangles you overlap, the darker they get, which is why the longer dataset has a darker rectangle. Use annotate instead of geom_rect to draw a single rectangle.
annotate(geom = "rect", xmin=5, xmax=30, ymin=-Inf, ymax=Inf, alpha = 0.2)
If you want to stick with geom_rect you can give a one row data.frame to that layer so that each rectangle is only drawn one time. Here I use a fake dataset, although you could put your rectangle limits in the data.frame, as well.
geom_rect(data = data.frame(fake = 1),
aes(xmin = 5, xmax= 30, ymin = -Inf, ymax = Inf), alpha = 0.2)

Bubble Plot of Negative and Positive values in space ggplot2 R

I would like to make a bubble plot using ggplot2 in R. My code and data are found below.
Please leave the colors as they are. I am having difficulties in scaling positive and negative values equally. For example, -3 is scaled smaller than +3. I would like negatives and positives to be scaled proportionately irrespective of sign.
Identify negative from positive values using some kind of outline linetype for bubbles and include it in the legend.
Also remove the "Mean" part of the legend.
Thanks very much for your great help.
#=====================================================================
library(ggplot2)
if (dev.cur() == 1) x11(width=8,height=6)
par(mfcol=c(1,1))
p<-ggplot(site.resiudal, aes(x=Eastings, y=Northings, size=Mean,label=site.resiudal$Site,legend = FALSE))+
#theme(legend.position="none")+
geom_point(shape=21)+
geom_point(aes(colour = factor(Region)))+
scale_area(range=c(1,15))+
scale_alpha(guide = 'none')+
scale_x_continuous(name="Longitude", limits=c(-120,-95))+
scale_y_continuous(name="Latitude", limits=c(48,61))+
geom_text(size=4)+
scale_colour_manual(name="Region",labels = c("A", "B","C","D", "E"),values = c("1" = "firebrick3","2" = "palegreen4","3" = "sandybrown","4" = "red","5" = "gray0"))+
theme(legend.title = element_text(colour="black", size=16, face="plain"))+
theme(legend.text = element_text(colour="black", size = 16, face = "plain"))
p
#Data[["sign"]] = ifelse(Data[["Mean"]] >= 0, "positive", "negative")
#=================================================
structure(list(Site = structure(c(101L, 102L, 105L, 107L, 108L,
110L, 111L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L,
13L, 14L, 16L, 20L, 47L, 52L, 53L, 55L, 91L, 92L, 93L, 94L, 95L,
96L, 99L, 15L, 17L, 18L, 19L, 21L, 114L, 23L, 26L, 36L, 59L,
60L, 61L, 62L, 63L, 64L, 65L, 66L, 67L, 68L, 69L, 70L, 71L, 72L,
73L, 74L, 75L, 76L, 77L, 78L, 79L, 80L, 81L, 82L, 83L, 84L, 85L,
86L, 87L, 88L, 89L, 98L, 100L, 103L, 104L, 106L, 109L, 112L,
113L, 115L, 116L, 117L, 119L, 42L, 44L, 46L, 48L, 49L, 50L, 51L,
54L, 56L, 57L, 58L, 90L, 97L, 118L, 120L, 22L, 24L, 25L, 27L,
28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 37L, 38L, 39L, 40L, 41L,
43L, 45L), .Label = c("G100", "G101", "G102", "G103", "G104",
"G105", "G106", "G107", "G108", "G109", "G110", "G111", "G112",
"G113", "G114", "G115", "G116", "G117", "G118", "G119", "G120",
"GG10", "GG11", "GG12", "GG13", "GG14", "GG15", "GG16", "GG17",
"GG18", "GG19", "GG20", "GG21", "GG22", "GG23", "GG24", "GG25",
"GG26", "GG27", "GG28", "GG29", "GG30", "GG31", "GG32", "GG33",
"GG34", "GG35", "GG36", "GG37", "GG38", "GG39", "GG40", "GG41",
"GG42", "GG43", "GG44", "GG45", "GG46", "GG47", "GG48", "GG49",
"GG50", "GG51", "GG52", "GG53", "GG54", "GG55", "GG56", "GG57",
"GG58", "GG59", "GG60", "GG61", "GG62", "GG63", "GG64", "GG65",
"GG66", "GG67", "GG68", "GG69", "GG70", "GG71", "GG72", "GG73",
"GG74", "GG75", "GG76", "GG77", "GG78", "GG79", "GG80", "GG81",
"GG82", "GG83", "GG84", "GG85", "GG86", "GG87", "GG88", "GG89",
"GG90", "GG91", "GG92", "GG93", "GG94", "GG95", "GG96", "GG97",
"GG98", "GG99", "GGG1", "GGG2", "GGG3", "GGG4", "GGG5", "GGG6",
"GGG7", "GGG8", "GGG9"), class = "factor"), Name = structure(c(53L,
87L, 29L, 92L, 36L, 76L, 102L, 103L, 119L, 2L, 9L, 11L, 45L,
47L, 49L, 54L, 90L, 30L, 105L, 66L, 78L, 107L, 81L, 42L, 41L,
43L, 59L, 110L, 24L, 27L, 56L, 61L, 64L, 118L, 40L, 21L, 44L,
70L, 108L, 25L, 58L, 98L, 83L, 5L, 19L, 26L, 31L, 38L, 55L, 60L,
71L, 74L, 75L, 85L, 95L, 120L, 109L, 1L, 67L, 20L, 50L, 63L,
106L, 111L, 116L, 62L, 6L, 99L, 114L, 73L, 84L, 89L, 93L, 97L,
115L, 80L, 10L, 12L, 88L, 79L, 15L, 17L, 33L, 35L, 94L, 100L,
3L, 16L, 37L, 101L, 117L, 8L, 39L, 48L, 86L, 113L, 23L, 13L,
69L, 96L, 104L, 32L, 65L, 82L, 14L, 22L, 18L, 46L, 68L, 72L,
77L, 91L, 112L, 4L, 7L, 28L, 51L, 57L, 52L, 34L), .Label = c("ANEROID",
"ARBORG", "ATHABASCA", "BANFF", "BANGOR", "BATTLEFORD", "BEAVER MINES",
"BEAVERLODGE", "BERENS RIVER", "BIRTLE", "BISSETT", "BRANDON",
"BUFFALO NARROWS", "CALGARY", "CALMAR", "CAMPSIE", "CAMROSE",
"CARWAY", "CEYLON", "CHAPLIN", "CHURCHILL", "CLARESHOLM", "COLD LAKE",
"COLLINS BAY", "CORONATION", "COTE", "CREE LAKE", "CROWSNEST",
"CYPRESS RIVER", "DAUPHIN", "DAVIDSON", "DRUMHELLER", "EDMONTON",
"EDSON", "ELK POINT", "EMERSON AUT", "ENILDA-BERG", "ESTEVAN",
"FAIRVIEW", "FLIN FLON", "FORT CHIPEWYAN", "FORT MCMURRAY", "FORT VERMILION",
"GILLAM", "GIMLI", "GLEICHEN", "GRAND RAPIDS", "GRANDE PRAIRIE",
"GREAT FALLS", "HIGH POINT", "HIGHWOOD", "HINTON VALLEY", "HUDSON BAY",
"INDIAN BAY", "INDIAN HEAD", "ISLAND FALLS", "JASPER WARDEN",
"JENNER", "KEG RIVER RS", "KELLIHER", "KEY LAKE", "KINDERSLEY",
"KLINTONEL", "LA RONGE", "LACOMBE 2", "LANGRUTH WEST", "LEADER",
"LETHBRIDGE", "LOON LAKE", "LYNN LAKE", "MANOR", "MEDICINE HAT",
"MELFORT", "MOOSE JAW", "MOOSOMIN", "MORDEN", "MOUNTAIN VIEW",
"NEEPAWA MURRAY", "NINETTE", "NIPAWIN", "NORWAY HOUSE", "OLDS",
"ONEFOUR", "OUTLOOK", "PASWEGIN", "PEACE RIVER", "PELLY", "PIERSON",
"PILGER", "PINAWA WNRE", "PINCHER CREEK ", "PORTAGE PRAIRIE",
"PRINCE ALBERT", "RANFURLY", "REGINA", "ROCKY MT HOUSE ", "SASKATOON",
"SCOTFIELD", "SCOTT", "SION", "SLAVE LAKE", "SPRAGUE", "STEINBACH",
"STETTLER NORTH", "SWAN RIVER", "SWIFT CURRENT", "THE PAS", "THOMPSON",
"TONKIN", "URANIUM CITY ", "VAL-MARIE", "VAUXHALL", "WABASCA RS",
"WASECA", "WASKESIU LAKE", "WEST POPLAR", "WHITECOURT", "WHITESAND DAM",
"WINNIPEG", "YELLOW GRASS"), class = "factor"), Mean = c(-0.020525899,
0.333863493, 0.210353772, NA, NA, 0.093520458, 0.341295298, NA,
-0.175074657, 0.09834825, 0.075610648, NA, -0.117503802, 0.18309367,
0.25246942, 0.221329766, 0.072167004, -0.094766032, NA, NA, 0.19783711,
-0.166351357, -0.0996169, -0.038555432, -0.028092042, 0.297855371,
0.108263891, 0.002057761, 0.327731415, NA, 0.180100638, 0.193837736,
-0.003306948, 0.178881894, 0.3655509, -0.235975798, -0.176154056,
-0.080433735, -0.110955273, -0.228010105, 0.048103255, -0.116681527,
-0.073042421, NA, NA, 0.035356012, 0.297171565, -0.197834719,
0.036412958, 0.055218077, NA, -0.236229087, 0.265211081, 0.271625885,
-0.293179359, 0.113744571, -0.207770026, 0.100471248, -0.071569464,
NA, NA, NA, -0.052716493, 0.057385851, 0.090340517, -0.30456625,
-0.234420722, 0.082287977, 0.009973663, NA, -0.06405062, 0.074703356,
-0.208329196, -0.272401078, 0.217991554, -0.043619919, -0.208901155,
-0.020022401, 0.111495318, NA, 0.38239749, 0.199136959, -0.177740258,
NA, 0.147515615, 0.309306538, 0.298741467, 0.068170296, NA, -0.02102765,
0.001754313, -0.010196512, 0.108254156, -0.228183063, -0.196261239,
NA, -0.167054722, 0.039949534, 0.154337034, -0.020855461, 0.136010278,
NA, 0.096997744, NA, -0.241963754, 0.660176529, 0.423554314,
0.190305726, -0.210778787, -0.261148915, NA, 0.054264129, -0.098706619,
-0.138776994, NA, NA, NA, -0.113823745, 0.373292721, -0.047060083
), Eastings = c(-102.5800018, -101.8700027, -99.08000183, -98.26999664,
-97.23000336, -98.08000183, -95.59999847, -96.76999664, -97.23000336,
-97.08000183, -97.02999878, -95.69999695, -97.01999664, -99.27999878,
-96, -95.19999695, -96.06999969, -100.0500031, -101.2300034,
-98.80000305, -99.56999969, -101.0999985, -97.84999847, -111.2200012,
-111.1200027, -116.0299988, -117.6200027, -108.4800034, -103.6999969,
-107.1299973, -102.3499985, -105.6200027, -105.2699966, -103.1500015,
-101.8799973, -94.06999969, -94.72000122, -101.0800018, -97.87000275,
-111.4499969, -111.1999969, -111.3499985, -110.4700012, -102.2799988,
-104.6500015, -101.7799988, -105.9800034, -102.9700012, -103.6500015,
-103.75, -102.0999985, -105.5500031, -101.6699982, -103.9199982,
-104.6699982, -104.1800003, -102.2300034, -107.3000031, -109.5,
-106.6500015, -107.9300003, -108.9199982, -107.7300034, -107.8499985,
-106.3799973, -109.1800003, -108.25, -108.8300018, -109.4000015,
-104.5999985, -107.0500031, -105.1500015, -105.6699982, -106.7200012,
-106.0699997, -104, -101.0500031, -99.94999695, -101.2699966,
-99.65000153, -113.8499985, -112.8199997, -113.5800018, -111.0699997,
-111.7300034, -114.1200027, -113.2799988, -114.6800003, -116.3000031,
-114.7799988, -115.7799988, -119.4000015, -118.5299988, -118.8799973,
-117.4499969, -113.8300018, -110.2799988, -108.4300003, -109.0999985,
-114.9199982, -112.7200012, -112.8700027, -113.75, -114.0999985,
-114.0199966, -113.7300034, -113.3799973, -113.0500031, -112.8000031,
-110.7200012, -113.6299973, -113.9800034, -112.1299973, -115.5500031,
-114.1800003, -114.4800034, -114.3700027, -118.0299988, -117.5299988,
-116.4499969), Northings = c(52.88000107, 52.08000183, 49.54999924,
49.95000076, 49, 49.18000031, 49.02000046, 49.52999878, 49.91999817,
50.93000031, 52.34999847, 51.02999878, 50.63000107, 53.15000153,
50.47000122, 49.61999893, 50.18000031, 51.09999847, 52.11999893,
50.41999817, 50.15000153, 53.97000122, 53.97000122, 56.65000153,
58.77000046, 58.38000107, 57.75, 59.56999969, 58.18000031, 57.34999847,
55.52999878, 57.25, 55.15000153, 56.22999954, 54.77000046, 58.72999954,
56.34999847, 56.86999893, 55.79999924, 52.06999969, 50.72000122,
51.58000183, 49.11999893, 50.90000153, 49.38000107, 51.52000046,
51.27000046, 49.22000122, 50.54999924, 51.25, 49.61999893, 50.33000183,
50.13000107, 51.97999954, 50.43000031, 49.81999969, 51.20000076,
49.72000122, 50.90000153, 50.47000122, 50.97999954, 49.68000031,
50.27000046, 49.36999893, 49, 51.52000046, 52.77000046, 52.36999893,
53.13000107, 52.81999969, 51.47999954, 52.41999817, 53.22000122,
52.16999817, 53.91999817, 53.33000183, 50.43000031, 49.91999817,
49.18000031, 49.41999817, 53.27999878, 53.02999878, 53.31999969,
53.88000107, 53.41999817, 53.88000107, 54.72000122, 54.13000107,
55.41999817, 55.27999878, 54.15000153, 55.20000076, 56.08000183,
55.18000031, 56.22999954, 55.97000122, 54.41999817, 55.83000183,
54.04999924, 52.41999817, 52.33000183, 51.47000122, 52.45000076,
51.77999878, 51.11999893, 49.93000031, 49, 50.88000107, 49.63000107,
50.02000046, 49.13000107, 49.52000046, 50.04999924, 51.20000076,
49.47000122, 49.63000107, 50.54999924, 52.93000031, 53.40000153,
53.58000183), Region = c(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, 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, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L)), .Names = c("Site",
"Name", "Mean", "Eastings", "Northings", "Region"), class = "data.frame", row.names = c(NA,
-120L))
#============================================
use abs()
eg
instead of size=Mean use size=abs(Mean)
Then, you can track the sign using shape (or some other aesthetic, but color and size are already taken)
replace:
geom_point(shape=21)+
geom_point(aes(colour = factor(Region))) +
with the single line
geom_point(aes(shape=factor(sign(Mean)), colour = factor(Region))) +
If you'd like, you can also add lines such as
scale_shape_discrete(name="Mean Is", breaks=c(-1, 1), labels=c("Negative", "Positive"))
guides(size=FALSE)

Resources