-
Notifications
You must be signed in to change notification settings - Fork 1
/
07-create-figures.r
210 lines (197 loc) · 6.99 KB
/
07-create-figures.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# MapBiomas Soil (beta): Script 07. Create figures
# Alessandro Samuel-Rosa & Taciara Zborowski Horst
# 2024 CC-BY
rm(list = ls())
# Install and load required packages
if (!require("data.table")) {
install.packages("data.table")
}
if (!require("sf")) {
install.packages("sf")
}
if (!require("geobr")) {
install.packages("geobr")
}
if (!require("rnaturalearth")) {
install.packages("rnaturalearth", dependencies = TRUE)
}
biomes <- geobr::read_biomes()[-7, ]
biomes <- sf::st_transform(biomes, crs = 4326)
brazil <- geobr::read_country()
brazil <- sf::st_transform(brazil, crs = 4326)
southamerica <- rnaturalearth::ne_countries(continent = c("south america", "europe"),
returnclass = "sf", scale = "medium")
southamerica <- southamerica[, "iso_a2"]
# Read data processed in the previous script
febr_data <- data.table::fread("data/06-febr-data.txt", dec = ",", sep = "\t")
colnames(febr_data)
nrow(unique(febr_data[, "id"])) # Result: 8904 events
nrow(febr_data) # Result: 8904 layers
# Avaliar distribuição de frequência dos dados de estoque de carbono
dev.off()
png("mapbiomas-soc/res/fig/carbon-stock-histogram.png",
width = 480 * 3, height = 480 * 3, res = 72 * 3
)
par(mar = c(5, 4, 2, 2) + 0.1)
y_max <- hist(febr_data[, cos_estoque_gm2] / 1000, plot = FALSE)$counts
y_max <- ceiling(max(y_max) / 1000) * 1000
hist(febr_data[, cos_estoque_gm2] / 1000,
panel.first = grid(nx = FALSE, ny = NULL),
# xlab = expression("Estoque de carbono orgânico do solo, kg m"^-2),
xlab = expression("Estoque de carbono orgânico do solo, t/ha"),
ylab = paste0("Frequência absoluta (n = ", nrow(febr_data), ")"),
ylim = c(0, y_max),
xlim = c(0, 120),
main = ""
)
rug(febr_data[, cos_estoque_gm2] / 1000)
dev.off()
# Avaliar distribuição de frequência da espessura da camada
dev.off()
png("mapbiomas-soc/res/fig/carbon-stock-depth-distribution.png",
width = 480 * 3, height = 480 * 3, res = 72 * 3
)
# x11()
par(mar = c(5, 4, 2, 2) + 0.1)
y_max <- hist(febr_data[, espessura], plot = FALSE)$counts
y_max <- ceiling(max(y_max) / 1000) * 1000
hist(febr_data[, espessura] / 100,
panel.first = grid(nx = FALSE, ny = NULL),
xlab = "Espessura da camada superficial, m",
ylab = paste0("Frequência absoluta (n = ", nrow(febr_data), ")"),
ylim = c(0, y_max),
xlim = c(0, 30) / 100,
main = ""
)
rug(febr_data[, espessura] / 100)
dev.off()
# Avaliar distribuição de frequência dos dados pontuais ao longo do tempo
dev.off()
png("mapbiomas-soc/res/fig/carbon-stock-temporal-distribution.png",
width = 480 * 3, height = 480 * 3, res = 72 * 3
)
# x11()
par(mar = c(5, 4, 2, 2) + 0.1)
y_max <- hist(febr_data[, data_coleta_ano], plot = FALSE)$counts
y_max <- ceiling(max(y_max) / 1000) * 1000
hist(febr_data[, data_coleta_ano],
panel.first = grid(nx = FALSE, ny = NULL),
xlab = "Ano de amostragem",
ylab = paste0("Frequência absoluta (n = ", nrow(febr_data), ")"),
ylim = c(0, y_max),
xlim = c(min(febr_data[, data_coleta_ano]),
as.integer(format(Sys.time(), "%Y"))),
main = ""
)
rug(febr_data[, data_coleta_ano])
dev.off()
# Criar objeto espacial e avaliar distribuição do estoque por bioma
febr_data_sf <- sf::st_as_sf(febr_data, coords = c("coord_x", "coord_y"), crs = 4326)
nrow(febr_data_sf) # 8904 events
dev.off()
png("mapbiomas-soc/res/fig/carbon-stock-spatial-distribution.png",
width = 480 * 3, height = 480 * 3, res = 72 * 3
)
# x11()
plot(biomes["name_biome"], reset = FALSE,
main = "", axes = TRUE, col = "transparent", lwd = 0.5, border = "darkgray",
key.pos = NULL, graticule = TRUE)
cex <- febr_data_sf[["cos_estoque_gm2"]] / (max(febr_data_sf[["cos_estoque_gm2"]]) * 0.4)
plot(febr_data_sf["cos_estoque_gm2"], add = TRUE, pch = 21, cex = cex, col = "black")
prob <- c(0, 0.5, 0.95, 0.975, 1)
quant <- round(quantile(febr_data_sf[["cos_estoque_gm2"]], prob) / 1000, 1)
leg_text <- paste(quant, "~ kg/m^2")
leg_text <- sapply(leg_text, function(x) parse(text = x))
legend(x = -40, y = 8, legend = leg_text, pch = 21, box.lwd = 0, pt.cex = quantile(cex, prob))
dev.off()
# Avaliar distribuição espacial dos pontos
# x11()
dev.off()
png("mapbiomas-soc/res/fig/points-spatial-distribution.png",
width = 480 * 3, height = 480 * 3, res = 72 * 3
)
par(mar = rep(1.9, 4))
plot(brazil, reset = FALSE, main = "", col = "transparent",
axes = TRUE, graticule = TRUE, lwd = 0.01)
plot(southamerica,
reset = FALSE,
col = "gray96",
add = TRUE, lwd = 0.5
)
plot(biomes["name_biome"], reset = FALSE,
main = "", axes = TRUE, col = "#eeece1", lwd = 0.5,
border = "gray69",
key.pos = NULL, graticule = TRUE, add = TRUE)
plot(febr_data_sf["cos_estoque_gm2"],
add = TRUE,
# pch = 21,
cex = 0.5, col = "firebrick"
)
dev.off()
# Avaliar distribuição temporal dos pontos
# Definir fatias temporais
# x11()
time_slices <- c(1900, 1985, 1995, 2005, 2015, 2021)
time_main <- c("<1985", "1985-1994", "1995-2004", "2005-2014", "2015-2021")
dev.off()
png("mapbiomas-soc/res/fig/points-spatial-temporal-distribution.png",
width = 480 * 5, height = 480, res = 72 * 2
)
par(mar = rep(1.9, 4), mfrow = c(1, 5))
for (i in 1:5) {
plot(brazil,
reset = FALSE, main = time_main[i], col = "transparent",
axes = TRUE, graticule = TRUE, lwd = 0.01
)
plot(southamerica,
reset = FALSE,
col = "gray96",
add = TRUE, lwd = 0.5
)
plot(biomes["name_biome"],
reset = FALSE,
main = "", axes = TRUE, col = "#eeece1", lwd = 0.5,
border = "gray69",
key.pos = NULL, add = TRUE
)
idx <- which(febr_data_sf[["data_coleta_ano"]] >= time_slices[i] &
febr_data_sf[["data_coleta_ano"]] < time_slices[i + 1])
plot(febr_data_sf[idx, "cos_estoque_gm2"], add = TRUE,
cex = 0.5, col = "firebrick")
}
dev.off()
# Avaliar distribuição por bioma
biomes$name_biome <- gsub(" ", "-", biomes$name_biome)
biomes$name_biome <- gsub("ô", "o", biomes$name_biome)
biomes$name_biome <- gsub("â", "a", biomes$name_biome)
biomes$name_biome <- tolower(biomes$name_biome)
febr_data_sf[["biome"]] <- NA_character_
dev.off()
for (i in biomes$name_biome) {
idx_bio <- which(biomes$name_biome == i)
idx_sf <- sf::st_intersects(biomes[idx_bio, ], febr_data_sf)[[1]]
febr_data_sf[["biome"]][idx_sf] <- i
file_name <- paste0(
"mapbiomas-soc/res/fig/carbon-stock-spatial-distribution-", i, ".png"
)
png(file_name, width = 480 * 3, height = 480 * 3, res = 72 * 3, bg = "transparent")
plot(
biomes[idx_bio, "name_biome"],
reset = FALSE, main = "", axes = FALSE, col = "#eeece1", border = "gray69", key.pos = NULL
)
plot(
febr_data_sf["cos_estoque_gm2"][idx_sf, ],
add = TRUE, pch = 21, col = "firebrick", cex = 1.0
)
dev.off()
}
# Number of samples per biome
table(febr_data_sf[["biome"]])
# Areal density of points per biome
round(table(febr_data_sf[["biome"]]) / (as.numeric(sf::st_area(biomes)) / (1000 * 1000 * 1000)), 1)
round(table(febr_data_sf[["biome"]]) / (2022 - 1985))
febr_data <- data.table::fread("res/tab/2023-05-19-pontos-estoque-cos.csv", sep = ",")
febr_data <- sf::st_as_sf(febr_data, coords = c("coord_x", "coord_y"), crs = 4326)
x11()
plot(febr_data["cos_estoque_gm2"])
mapview::mapview(febr_data["cos_estoque_gm2"])