-
Notifications
You must be signed in to change notification settings - Fork 2
/
plot_isospace.R
157 lines (109 loc) · 4.28 KB
/
plot_isospace.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#' Plot data in the isotope space
#'
#' @description
#' This function represents the data in the isotope space to overlay consumer
#' and sources isotope values. User can also use the [MixSIAR::plot_data()].
#'
#' @param filename a character of length 1. The name of the plot file to save.
#'
#' @param plot_save_pdf a logical value. If `TRUE`, the plot is saved as pdf.
#'
#' @param plot_save_png a logical value. If `TRUE`, the plot is saved as png.
#'
#' @inheritParams samplesim
#'
#' @return NULL
#'
#' @seealso [samplesim()], [get_output()], [plot_samplesim()]
#'
#' @export
#' @import ggplot2
#'
#' @examples
#' \dontrun{
#' ## Please visit the vignette available at:
#' ## https://ahasverus.github.io/samplesim/articles/samplesim.html
#' }
plot_isospace <- function(mix, source, discr, filename = "isospace",
plot_save_pdf = FALSE, plot_save_png = FALSE) {
if (length(mix$"FAC") > 0 || length(source$"S_factor1") > 0) {
stop("Your data contain at least one factor. ",
"Please use MixSIAR::plot_data() instead.")
}
spaces <- utils::combn(1:mix$"n.iso", 2)
nplots <- ncol(spaces)
for (i in 1:nplots) {
iso_names <- mix$"iso_names"[spaces[ , i]]
if (length(grep("C$", iso_names[1])) == 1) {
x_label <- expression(paste(delta^13, "C (\u2030)", sep = ""))
}
if (length(grep("C$", iso_names[2])) == 1) {
y_label <- expression(paste(delta^13, "C (\u2030)", sep = ""))
}
if (length(grep("N$", iso_names[1])) == 1) {
x_label <- expression(paste(delta^15, "N (\u2030)", sep = ""))
}
if (length(grep("N$", iso_names[2])) == 1) {
y_label <- expression(paste(delta^15, "N (\u2030)", sep = ""))
}
if (length(grep("S$", iso_names[1])) == 1) {
x_label <- expression(paste(delta^34, "S (\u2030)", sep = ""))
}
if (length(grep("S$", iso_names[2])) == 1) {
y_label <- expression(paste(delta^34, "S (\u2030)", sep = ""))
}
if (length(grep("O$", iso_names[1])) == 1) {
x_label <- expression(paste(delta^18, "O (\u2030)", sep = ""))
}
if (length(grep("O$", iso_names[2])) == 1) {
y_label <- expression(paste(delta^18, "O (\u2030)", sep = ""))
}
dat_mix <- data.frame(x = mix$"data_iso"[ , iso_names[1]],
y = mix$"data_iso"[ , iso_names[2]])
mu <- data.frame(rep(0, source$"n.sources"), rep(0, source$"n.sources"))
colnames(mu) <- iso_names
sig <- mu
for (iso in iso_names) {
mu[ , iso] <- source$"S_MU"[ , paste0("Mean", iso)] +
discr$"mu"[ , paste0("Mean", iso)]
sig[ , iso] <- sqrt(source$"S_SIG"[ , paste0("SD", iso)] ^ 2 +
discr$"sig2"[, paste0("SD", iso)])
}
dat_src <- data.frame("x" = mu[ , 1],
"y" = mu[ , 2],
"xmin" = mu[ , 1] - sig[ , 1],
"xmax" = mu[ , 1] + sig[ , 1],
"ymin" = mu[ , 2] - sig[ , 2],
"ymax" = mu[ , 2] + sig[ , 2],
"source" = source$"source_names")
if (i > 1) {
grDevices::dev.new()
}
gg <- ggplot(data = dat_src, aes(x = x, y = y)) +
geom_point(data = dat_mix, aes(x = x, y = y), colour = "#888888",
show.legend = TRUE) +
geom_errorbar(data = dat_src, aes(ymin = ymin, ymax = ymax,
colour = source), width = 0) +
geom_errorbarh(data = dat_src, aes(xmin = xmin, xmax = xmax,
colour = source), height = 0) +
geom_point(data = dat_src, aes(x = x, y = y, colour = source), shape = 24,
fill = "white", size = 3) +
labs(x = x_label, y = y_label) +
theme_light() +
theme(legend.position = "bottom", legend.title = element_blank())
print(gg)
if (plot_save_pdf) {
grDevices::cairo_pdf(
filename = paste0(filename, "-space", i, ".pdf"),
width = 6, height = 6)
print(gg)
grDevices::dev.off()
}
if (plot_save_png) {
grDevices::png(file = paste0(filename, "-space", i, ".png"))
print(gg)
grDevices::dev.off()
}
}
invisible(NULL)
}