-
Notifications
You must be signed in to change notification settings - Fork 76
/
sits_plot.R
1725 lines (1676 loc) · 58.4 KB
/
sits_plot.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#' @title Plot time series
#' @method plot sits
#' @name plot
#' @author Gilberto Camara, \email{gilberto.camara@@inpe.br}
#' @description This is a generic function. Parameters depend on the specific
#' type of input. See each function description for the
#' required parameters.
#' \itemize{
#' \item sits tibble: see \code{\link{plot.sits}}
#' \item patterns: see \code{\link{plot.patterns}}
#' \item SOM map: see \code{\link{plot.som_map}}
#' \item SOM evaluate cluster: see \code{\link{plot.som_evaluate_cluster}}
#' \item classified time series: see \code{\link{plot.predicted}}
#' \item raster cube: see \code{\link{plot.raster_cube}}
#' \item vector cube: see \code{\link{plot.vector_cube}}
#' \item random forest model: see \code{\link{plot.rfor_model}}
#' \item xgboost model: see \code{\link{plot.xgb_model}}
#' \item torch ML model: see \code{\link{plot.torch_model}}
#' \item classification probabilities: see \code{\link{plot.probs_cube}}
#' \item model uncertainty: see \code{\link{plot.uncertainty_cube}}
#' \item classified cube: see \code{\link{plot.class_cube}}
#' \item classified vector cube: see \code{\link{plot.class_vector_cube}}
#' }
#'
#' @param x Object of class "sits".
#' @param y Ignored.
#' @param together A logical value indicating whether
#' the samples should be plotted together.
#' @param ... Further specifications for \link{plot}.
#'
#' @return A series of plot objects produced by ggplot2 showing all
#' time series associated to each combination of band and label,
#' and including the median, and first and third quartile ranges.
#'
#' @examples
#' if (sits_run_examples()) {
#' # plot sets of time series
#' plot(cerrado_2classes)
#' }
#'
#' @export
plot.sits <- function(x, y, ..., together = FALSE) {
.check_set_caller(".plot_sits")
stopifnot(missing(y))
# default value is set to empty char in case null
.check_lgl_parameter(together)
# Are there more than 30 samples? Plot them together!
if (together || nrow(x) > 30) {
p <- .plot_together(x)
} else {
# otherwise, take "allyears" as the default
p <- .plot_allyears(x)
}
# return the plot
return(invisible(p))
}
#' @title Plot patterns that describe classes
#' @name plot.patterns
#' @author Gilberto Camara, \email{gilberto.camara@@inpe.br}
#' @author Victor Maus, \email{vwmaus1@@gmail.com}
#' @description Plots the patterns to be used for classification
#'
#' @description Given a sits tibble with a set of patterns, plot them.
#'
#' @param x Object of class "patterns".
#' @param y Ignored.
#' @param ... Further specifications for \link{plot}.
#' @param bands Bands to be viewed (optional).
#' @param year_grid Plot a grid of panels using labels as columns and
#' years as rows. Default is FALSE.
#' @return A plot object produced by ggplot2
#' with one average pattern per label.
#'
#' @note
#' This code is reused from the dtwSat package by Victor Maus.
#' @examples
#' if (sits_run_examples()) {
#' # plot patterns
#' plot(sits_patterns(cerrado_2classes))
#' }
#' @export
#'
plot.patterns <- function(x, y, ..., bands = NULL, year_grid = FALSE) {
.check_set_caller(".plot_patterns")
stopifnot(missing(y))
# verifies if scales package is installed
.check_require_packages("scales")
# extract the patterns for each band
patterns_bands <- .ts_bands(.ts(x))
bands <- .default(bands, patterns_bands)
# pre-condition
.check_chr_within(bands,
within = patterns_bands
)
# extract only for the selected bands
.ts(x) <- .ts_select_bands(.ts(x), bands)
# put the time series in the data frame
plot_df <- purrr::pmap_dfr(
list(x[["label"]], x[["time_series"]]),
function(label, ts) {
lb <- as.character(label)
# extract the time series and convert
df <- tibble::tibble(Time = ts[["Index"]], ts[-1], Pattern = lb)
return(df)
}
)
# create a data.frame by melting the values per bands
plot_df <- tidyr::pivot_longer(plot_df, cols = sits_bands(x))
# Do we want a multi-year grid?
if (year_grid) {
plot_df <- plot_df |>
dplyr::mutate(year = format(.data[["Time"]], format = "%Y")) |>
dplyr::mutate(Time = as.Date(format(.data[["Time"]],
format = "2000-%m-%d"
)))
}
# Plot temporal patterns
gp <- ggplot2::ggplot(plot_df, ggplot2::aes(
x = .data[["Time"]],
y = .data[["value"]],
colour = .data[["name"]]
)) +
ggplot2::geom_line()
# Do we want a multi-year grid?
if (year_grid) {
gp <- gp + ggplot2::facet_grid(year ~ Pattern)
} else {
gp <- gp + ggplot2::facet_wrap(~Pattern)
}
# create a multi-frame plot
gp <- gp +
ggplot2::theme(legend.position = "bottom") +
ggplot2::scale_x_date(labels = scales::date_format("%b")) +
ggplot2::guides(colour = ggplot2::guide_legend(title = "Bands")) +
ggplot2::ylab("Value")
# plot the data
p <- graphics::plot(gp)
return(invisible(p))
}
#' @title Plot time series predictions
#' @name plot.predicted
#' @author Victor Maus, \email{vwmaus1@@gmail.com}
#' @author Gilberto Camara, \email{gilberto.camara@@inpe.br}
#' @description Given a sits tibble with a set of predictions, plot them
#'
#' @param x Object of class "predicted".
#' @param y Ignored.
#' @param ... Further specifications for \link{plot}.
#' @param bands Bands for visualization.
#' @param palette HCL palette used for visualization
#' in case classes are not in the default sits palette.
#' @return A plot object produced by ggplot2
#' showing the time series and its label.
#'
#' @note
#' This code is reused from the dtwSat package by Victor Maus.
#' @examples
#' if (sits_run_examples()) {
#' # Retrieve the samples for Mato Grosso
#' # train an svm model
#' ml_model <- sits_train(samples_modis_ndvi, ml_method = sits_svm)
#' # classify the point
#' point_ndvi <- sits_select(point_mt_6bands, bands = "NDVI")
#' point_class <- sits_classify(
#' data = point_ndvi, ml_model = ml_model
#' )
#' plot(point_class)
#' }
#' @export
#'
plot.predicted <- function(x, y, ...,
bands = "NDVI",
palette = "Harmonic") {
.check_set_caller(".plot_predicted")
stopifnot(missing(y))
.check_predicted(x)
# verifies if scales package is installed
.check_require_packages("scales")
# check for color_palette parameter (sits 1.4.1)
dots <- list(...)
if (missing(palette) && "color_palette" %in% names(dots)) {
warning(.conf("messages", ".plot_palette"))
palette <- dots[["color_palette"]]
}
# are bands specified?
if (!.has(bands)) {
bands <- sits_bands(x)
}
# are the chosen bands in the data?
if (!all(bands %in% sits_bands(x))) {
bands <- sits_bands(x)
}
# configure plot colors
# get labels from predicted tibble
labels <- unique(x[["predicted"]][[1]][["class"]])
colors <- .colors_get(
labels = labels,
legend = NULL,
palette = palette,
rev = FALSE
)
# put the time series in the data frame
p <- purrr::pmap(
list(
x[["latitude"]], x[["longitude"]], x[["label"]],
x[["time_series"]], x[["predicted"]]
),
function(row_lat, row_long, row_label,
row_time_series, row_predicted) {
lb <- .plot_title(row_lat, row_long, row_label)
# extract the time series
ts <- row_time_series
# convert to data frame
df_x <- data.frame(
Time = ts[["Index"]], ts[, bands],
Series = as.factor(lb)
)
# melt the time series data for plotting
df_x <- tidyr::pivot_longer(df_x,
cols = -c("Time", "Series"),
names_to = "variable"
)
# define a nice set of breaks for value plotting
y_labels <- scales::pretty_breaks()(range(df_x[["value"]],
na.rm = TRUE
))
y_breaks <- y_labels
# create a data frame with values and intervals
nrows_p <- nrow(row_predicted)
df_pol <- purrr::pmap_dfr(
list(
row_predicted[["from"]], row_predicted[["to"]],
row_predicted[["class"]], seq(1:nrows_p)
),
function(rp_from, rp_to, rp_class, i) {
best_class <- as.character(rp_class)
df_p <- data.frame(
Time = c(
lubridate::as_date(rp_from),
lubridate::as_date(rp_to),
lubridate::as_date(rp_to),
lubridate::as_date(rp_from)
),
Group = rep(i, 4),
Class = rep(best_class, 4),
value = rep(range(y_breaks,
na.rm = TRUE
), each = 2)
)
return(df_p)
}
)
# create a multi-year plot
df_pol[["Group"]] <- factor(df_pol[["Group"]])
df_pol[["Class"]] <- factor(df_pol[["Class"]])
df_pol[["Series"]] <- rep(lb, length(df_pol[["Time"]]))
# temporal adjustments - create a time index
idx <- min(df_pol[["Time"]], na.rm = TRUE) - 30 <= df_x[["Time"]] &
df_x[["Time"]] <= max(df_pol[["Time"]], na.rm = TRUE) + 30
df_x <- df_x[idx, , drop = FALSE]
# plot facets
gp <- ggplot2::ggplot() +
ggplot2::facet_wrap(~Series,
scales = "free_x", ncol = 1
) +
ggplot2::geom_polygon(
data = df_pol,
ggplot2::aes(
x = .data[["Time"]],
y = .data[["value"]],
group = .data[["Group"]],
fill = .data[["Class"]]
),
alpha = 0.7
) +
ggplot2::scale_fill_manual(values = colors) +
ggplot2::geom_line(
data = df_x,
ggplot2::aes(
x = .data[["Time"]],
y = .data[["value"]],
colour = .data[["variable"]]
)
) +
ggplot2::scale_color_brewer(palette = "Set1") +
ggplot2::scale_y_continuous(
expand = c(0, 0),
breaks = y_breaks,
labels = y_labels
) +
ggplot2::scale_x_date(
breaks = ggplot2::waiver(),
labels = ggplot2::waiver()
) +
ggplot2::theme(legend.position = "bottom") +
ggplot2::guides(
colour =
ggplot2::guide_legend(title = "Bands")
) +
ggplot2::ylab("Value") +
ggplot2::xlab("Time")
g <- graphics::plot(gp)
return(g)
}
)
return(invisible(p))
}
#' @title Plot RGB data cubes
#' @name plot.raster_cube
#' @author Gilberto Camara, \email{gilberto.camara@@inpe.br}
#'
#' @description Plot RGB raster cube
#'
#' @param x Object of class "raster_cube".
#' @param ... Further specifications for \link{plot}.
#' @param band Band for plotting grey images.
#' @param red Band for red color.
#' @param green Band for green color.
#' @param blue Band for blue color.
#' @param tile Tile to be plotted.
#' @param dates Dates to be plotted.
#' @param palette An RColorBrewer palette
#' @param rev Reverse the color order in the palette?
#' @param scale Scale to plot map (0.4 to 1.0)
#' @param style Style for plotting continuous objects
#'
#' @return A plot object with an RGB image
#' or a B/W image on a color scale
#'
#' @note
#' Use \code{scale} parameter for general output control.
#' The \code{dates} parameter indicates the date allows plotting of different dates when
#' a single band and three dates are provided, `sits` will plot a
#' multi-temporal RGB image for a single band (useful in the case of
#' SAR data). For RGB bands with multi-dates, multiple plots will be
#' produced.
#' @examples
#' if (sits_run_examples()) {
#' # create a data cube from local files
#' data_dir <- system.file("extdata/raster/mod13q1", package = "sits")
#' cube <- sits_cube(
#' source = "BDC",
#' collection = "MOD13Q1-6",
#' data_dir = data_dir
#' )
#' # plot NDVI band of the second date date of the data cube
#' plot(cube, band = "NDVI", dates = sits_timeline(cube)[1])
#' # plot NDVI band as an RGB composite for the three bands
#' }
#' @export
plot.raster_cube <- function(x, ...,
band = NULL,
red = NULL,
green = NULL,
blue = NULL,
tile = x[["tile"]][[1]],
dates = NULL,
palette = "RdYlGn",
rev = FALSE,
scale = 0.9,
style = "order") {
# check caller
.check_set_caller(".plot_raster_cube")
# retrieve dots
dots <- list(...)
# deal with wrong parameter "date"
if ("date" %in% names(dots) && missing(dates)) {
dates <- as.Date(dots[["date"]])
}
# is tile inside the cube?
.check_chr_contains(
x = x[["tile"]],
contains = tile,
case_sensitive = FALSE,
discriminator = "one_of",
can_repeat = FALSE,
msg = .conf("messages", ".plot_raster_cube_tile")
)
# verifies if stars package is installed
.check_require_packages("stars")
# verifies if tmap package is installed
.check_require_packages("tmap")
if (.has(band)) {
# check palette
.check_palette(palette)
# check rev
.check_lgl_parameter(rev)
}
# check scale parameter
.check_num_parameter(scale, min = 0.2)
# reverse the color palette?
if (rev || palette == "Greys")
palette <- paste0("-", palette)
# filter the tile to be processed
tile <- .cube_filter_tiles(cube = x, tiles = tile)
if (.has(dates)) {
# is this a valid date?
dates <- as.Date(dates)
.check_that(all(dates %in% .tile_timeline(tile)),
msg = .conf("messages", ".plot_raster_cube_date")
)
} else {
dates <- .tile_timeline(tile)[[1]]
}
# BW or color?
.check_bw_rgb_bands(band, red, green, blue)
.check_available_bands(x, band, red, green, blue)
if (.has(band) && length(dates) == 3) {
main_title <- paste0(.tile_collection(tile), " ", band, " ",
as.Date(dates[[1]]), "(R) ",
as.Date(dates[[2]]), "(G) ",
as.Date(dates[[3]]), "(B) "
)
p <- .plot_band_multidate(
tile = tile,
band = band,
dates = dates,
palette = palette,
main_title = main_title,
rev = rev,
scale = scale
)
return(p)
}
if (length(dates) > 1) {
warning(.conf("messages", ".plot_raster_cube_single_date"))
}
if (.has(band)) {
main_title <- paste0(.tile_collection(tile), " ", band,
" ", as.Date(dates[[1]]))
p <- .plot_false_color(
tile = tile,
band = band,
date = dates[[1]],
sf_seg = NULL,
seg_color = NULL,
line_width = NULL,
palette = palette,
main_title = main_title,
rev = rev,
scale = scale,
style = style
)
} else {
# plot RGB
main_title <- paste0(.tile_satellite(tile)," ",
tile[["tile"]], " ",
red, "(R) ",
green, "(G) ",
blue, "(B) ",
as.Date(dates[[1]])
)
p <- .plot_rgb(
tile = tile,
red = red,
green = green,
blue = blue,
date = dates[[1]],
main_title = main_title,
sf_seg = NULL,
seg_color = NULL,
line_width = NULL,
scale = scale
)
}
return(p)
}
#' @title Plot RGB vector data cubes
#' @name plot.vector_cube
#' @author Gilberto Camara, \email{gilberto.camara@@inpe.br}
#'
#' @description Plot RGB raster cube
#'
#' @param x Object of class "raster_cube".
#' @param ... Further specifications for \link{plot}.
#' @param band Band for plotting grey images.
#' @param red Band for red color.
#' @param green Band for green color.
#' @param blue Band for blue color.
#' @param tile Tile to be plotted.
#' @param dates Dates to be plotted.
#' @param seg_color Color to show the segment boundaries
#' @param line_width Line width to plot the segments boundary (in pixels)
#' @param palette An RColorBrewer palette
#' @param rev Reverse the color order in the palette?
#' @param scale Scale to plot map (0.4 to 1.5)
#' @param style Style for plotting continuous objects
#'
#' @return A plot object with an RGB image
#' or a B/W image on a color
#' scale using the pallete
#'
#' @note To see which color palettes are supported, please run
#' @examples
#' if (sits_run_examples()) {
#' # create a data cube from local files
#' data_dir <- system.file("extdata/raster/mod13q1", package = "sits")
#' cube <- sits_cube(
#' source = "BDC",
#' collection = "MOD13Q1-6",
#' data_dir = data_dir
#' )
#' # Segment the cube
#' segments <- sits_segment(
#' cube = cube,
#' output_dir = tempdir(),
#' multicores = 2,
#' memsize = 4
#' )
#' # plot NDVI band of the second date date of the data cube
#' plot(segments, band = "NDVI", date = sits_timeline(cube)[1])
#' }
#' @export
plot.vector_cube <- function(x, ...,
band = NULL,
red = NULL,
green = NULL,
blue = NULL,
tile = x[["tile"]][[1]],
dates = NULL,
seg_color = "black",
line_width = 0.3,
palette = "RdYlGn",
rev = FALSE,
scale = 1.0,
style = "order") {
.check_set_caller(".plot_vector_cube")
# retrieve dots
dots <- list(...)
# deal with wrong parameter "date"
if ("date" %in% names(dots) && missing(dates)) {
dates <- as.Date(dots[["date"]])
}
# is tile inside the cube?
.check_chr_contains(
x = x[["tile"]],
contains = tile,
case_sensitive = FALSE,
discriminator = "one_of",
can_repeat = FALSE,
msg = .conf("messages", ".plot_raster_cube_tile")
)
# filter the tile to be processed
tile <- .cube_filter_tiles(cube = x, tiles = tile)
if (.has(dates)) {
# is this a valid date?
dates <- as.Date(dates)[[1]]
.check_that(all(dates %in% .tile_timeline(tile)),
msg = .conf("messages", ".plot_raster_cube_date")
)
} else {
dates <- .tile_timeline(tile)[[1]]
}
# retrieve the segments for this tile
sf_seg <- .segments_read_vec(tile)
# BW or color?
.check_bw_rgb_bands(band, red, green, blue)
.check_available_bands(x, band, red, green, blue)
if (.has(band)) {
main_title <- paste0(
.tile_collection(tile), " ", band, " ", as.Date(dates[[1]])
)
# plot the band as false color
p <- .plot_false_color(
tile = tile,
band = band,
date = dates[[1]],
sf_seg = sf_seg,
seg_color = seg_color,
line_width = line_width,
palette = palette,
main_title = main_title,
rev = rev,
scale = scale,
style = style
)
} else {
main_title <- paste0(.tile_collection(tile)," ",
tile[["tile"]],
red, "(R) ",
green, "(G) ",
blue, "(B) ",
as.Date(dates[[1]])
)
# plot RGB
p <- .plot_rgb(
tile = tile,
red = red,
green = green,
blue = blue,
date = dates[[1]],
main_title = main_title,
sf_seg = sf_seg,
seg_color = seg_color,
line_width = line_width,
scale = scale
)
}
return(p)
}
#' @title Plot probability cubes
#' @name plot.probs_cube
#' @author Gilberto Camara, \email{gilberto.camara@@inpe.br}
#' @description plots a probability cube using stars
#'
#' @param x Object of class "probs_cube".
#' @param ... Further specifications for \link{plot}.
#' @param tile Tile to be plotted.
#' @param labels Labels to plot (optional).
#' @param palette RColorBrewer palette
#' @param rev Reverse order of colors in palette?
#' @param scale Scale to plot map (0.4 to 1.0)
#' @return A plot containing probabilities associated
#' to each class for each pixel.
#'
#'
#' @examples
#' if (sits_run_examples()) {
#' # create a random forest model
#' rfor_model <- sits_train(samples_modis_ndvi, sits_rfor())
#' # create a data cube from local files
#' data_dir <- system.file("extdata/raster/mod13q1", package = "sits")
#' cube <- sits_cube(
#' source = "BDC",
#' collection = "MOD13Q1-6",
#' data_dir = data_dir
#' )
#' # classify a data cube
#' probs_cube <- sits_classify(
#' data = cube, ml_model = rfor_model, output_dir = tempdir()
#' )
#' # plot the resulting probability cube
#' plot(probs_cube)
#' }
#'
#' @export
#'
plot.probs_cube <- function(x, ...,
tile = x[["tile"]][[1]],
labels = NULL,
palette = "YlGn",
rev = FALSE,
scale = 0.8) {
.check_set_caller(".plot_probs_cube")
# check for color_palette parameter (sits 1.4.1)
dots <- list(...)
if (missing(palette) && "color_palette" %in% names(dots)) {
warning(.conf("messages", ".plot_palette"))
palette <- dots[["color_palette"]]
}
# precondition
.check_chr_contains(
x = x[["tile"]],
contains = tile,
case_sensitive = FALSE,
discriminator = "one_of",
can_repeat = FALSE,
msg = .conf("messages", ".plot_raster_cube_tile")
)
# filter the cube
tile <- .cube_filter_tiles(cube = x, tiles = tile)
# plot the probs cube
p <- .plot_probs(tile = tile,
labels_plot = labels,
palette = palette,
rev = rev,
scale = scale)
return(p)
}
#' @title Plot probability vector cubes
#' @name plot.probs_vector_cube
#' @author Gilberto Camara, \email{gilberto.camara@@inpe.br}
#' @description plots a probability cube using stars
#'
#' @param x Object of class "probs_vector_cube".
#' @param ... Further specifications for \link{plot}.
#' @param tile Tile to be plotted.
#' @param labels Labels to plot (optional).
#' @param palette RColorBrewer palette
#' @param rev Reverse order of colors in palette?
#' @param scale Scale to plot map (0.4 to 1.0)
#' @return A plot containing probabilities associated
#' to each class for each pixel.
#'
#'
#' @examples
#' if (sits_run_examples()) {
#' # create a random forest model
#' rfor_model <- sits_train(samples_modis_ndvi, sits_rfor())
#' # create a data cube from local files
#' data_dir <- system.file("extdata/raster/mod13q1", package = "sits")
#' cube <- sits_cube(
#' source = "BDC",
#' collection = "MOD13Q1-6",
#' data_dir = data_dir
#' )
#' # segment the image
#' segments <- sits_segment(
#' cube = cube,
#' seg_fn = sits_slic(step = 5,
#' compactness = 1,
#' dist_fun = "euclidean",
#' avg_fun = "median",
#' iter = 20,
#' minarea = 10,
#' verbose = FALSE),
#' output_dir = tempdir()
#' )
#' # classify a data cube
#' probs_vector_cube <- sits_classify(
#' data = segments,
#' ml_model = rfor_model,
#' output_dir = tempdir()
#' )
#' # plot the resulting probability cube
#' plot(probs_vector_cube)
#' }
#'
#' @export
#'
plot.probs_vector_cube <- function(x, ...,
tile = x[["tile"]][[1]],
labels = NULL,
palette = "YlGn",
rev = FALSE,
scale = 0.8) {
.check_set_caller(".plot_probs_vector")
# check for color_palette parameter (sits 1.4.1)
dots <- list(...)
if (missing(palette) && "color_palette" %in% names(dots)) {
warning(.conf("messages", ".plot_palette"))
palette <- dots[["color_palette"]]
}
# precondition
.check_chr_contains(
x = x[["tile"]],
contains = tile,
case_sensitive = FALSE,
discriminator = "one_of",
can_repeat = FALSE,
msg = .conf("messages", ".plot_raster_cube_tile")
)
# filter the cube
tile <- .cube_filter_tiles(cube = x, tiles = tile)
# plot the probs vector cube
p <- .plot_probs_vector(tile = tile,
labels_plot = labels,
palette = palette,
rev = rev,
scale = scale)
return(p)
}
#' @title Plot variance cubes
#' @name plot.variance_cube
#' @author Gilberto Camara, \email{gilberto.camara@@inpe.br}
#' @description plots a probability cube using stars
#'
#' @param x Object of class "variance_cube".
#' @param ... Further specifications for \link{plot}.
#' @param tile Tile to be plotted.
#' @param labels Labels to plot (optional).
#' @param palette RColorBrewer palette
#' @param rev Reverse order of colors in palette?
#' @param type Type of plot ("map" or "hist")
#' @param scale Scale to plot map (0.4 to 1.0)
#' @return A plot containing probabilities associated
#' to each class for each pixel.
#'
#'
#' @examples
#' if (sits_run_examples()) {
#' # create a random forest model
#' rfor_model <- sits_train(samples_modis_ndvi, sits_rfor())
#' # create a data cube from local files
#' data_dir <- system.file("extdata/raster/mod13q1", package = "sits")
#' cube <- sits_cube(
#' source = "BDC",
#' collection = "MOD13Q1-6",
#' data_dir = data_dir
#' )
#' # classify a data cube
#' probs_cube <- sits_classify(
#' data = cube, ml_model = rfor_model, output_dir = tempdir()
#' )
#' # obtain a variance cube
#' var_cube <- sits_variance(probs_cube, output_dir = tempdir())
#' # plot the variance cube
#' plot(var_cube)
#' }
#'
#' @export
#'
plot.variance_cube <- function(x, ...,
tile = x[["tile"]][[1]],
labels = NULL,
palette = "YlGnBu",
rev = FALSE,
type = "map",
scale = 0.8) {
.check_set_caller(".plot_variance_cube")
# check for color_palette parameter (sits 1.4.1)
dots <- list(...)
if (missing(palette) && "color_palette" %in% names(dots)) {
warning(.conf("messages", ".plot_palette"))
palette <- dots[["color_palette"]]
}
# precondition
.check_chr_contains(
x = x[["tile"]],
contains = tile,
case_sensitive = FALSE,
discriminator = "one_of",
can_repeat = FALSE,
msg = .conf("messages", ".plot_raster_cube_tile")
)
# filter the cube
tile <- .cube_filter_tiles(cube = x, tiles = tile)
# check type
.check_that(type %in% c("map", "hist"))
# plot the variance cube
if (type == "map") {
p <- .plot_probs(tile = tile,
labels_plot = labels,
palette = palette,
rev = rev,
scale = scale)
} else {
p <- .plot_variance_hist(tile)
}
return(p)
}
#' @title Plot uncertainty cubes
#' @name plot.uncertainty_cube
#' @author Gilberto Camara, \email{gilberto.camara@@inpe.br}
#' @description plots a probability cube using stars
#'
#' @param x Object of class "probs_image".
#' @param ... Further specifications for \link{plot}.
#' @param tile Tiles to be plotted.
#' @param palette An RColorBrewer palette
#' @param rev Reverse the color order in the palette?
#' @param scale Scale to plot map (0.4 to 1.0)
#'
#' @return A plot object produced by the stars package
#' with a map showing the uncertainty associated
#' to each classified pixel.
#'
#' @examples
#' if (sits_run_examples()) {
#' # create a random forest model
#' rfor_model <- sits_train(samples_modis_ndvi, sits_rfor())
#' # create a data cube from local files
#' data_dir <- system.file("extdata/raster/mod13q1", package = "sits")
#' cube <- sits_cube(
#' source = "BDC",
#' collection = "MOD13Q1-6",
#' data_dir = data_dir
#' )
#' # classify a data cube
#' probs_cube <- sits_classify(
#' data = cube, ml_model = rfor_model, output_dir = tempdir()
#' )
#' # calculate uncertainty
#' uncert_cube <- sits_uncertainty(probs_cube, output_dir = tempdir())
#' # plot the resulting uncertainty cube
#' plot(uncert_cube)
#' }
#' @export
#'
plot.uncertainty_cube <- function(x, ...,
tile = x[["tile"]][[1]],
palette = "RdYlGn",
rev = TRUE,
scale = 1.0) {
.check_set_caller(".plot_uncertainty_cube")
# check for color_palette parameter (sits 1.4.1)
dots <- list(...)
if (missing(palette) && "color_palette" %in% names(dots)) {
warning(.conf("messages", ".plot_palette"))
palette <- dots[["color_palette"]]
}
# precondition
.check_chr_contains(
x = x[["tile"]],
contains = tile,
case_sensitive = FALSE,
discriminator = "one_of",
can_repeat = FALSE,
msg = .conf("messages", ".plot_raster_cube_tile")
)
# filter the cube
tile <- .cube_filter_tiles(cube = x, tiles = tile[[1]])
band <- sits_bands(tile)
main_title <- paste0(.tile_collection(tile), " uncertainty ", band)
# plot the data using tmap
p <- .plot_false_color(
tile = tile,
band = band,
date = NULL,
sf_seg = NULL,
seg_color = NULL,
line_width = NULL,
palette = palette,
main_title = main_title,
rev = rev,
scale = scale,
style = "order"
)
return(p)
}
#' @title Plot uncertainty vector cubes
#' @name plot.uncertainty_vector_cube
#' @author Gilberto Camara, \email{gilberto.camara@@inpe.br}
#' @description plots a probability cube using stars
#'
#' @param x Object of class "probs_vector_cube".
#' @param ... Further specifications for \link{plot}.
#' @param tile Tile to be plotted.
#' @param palette RColorBrewer palette
#' @param rev Reverse order of colors in palette?
#' @param scale Scale to plot map (0.4 to 1.0)
#' @return A plot containing probabilities associated
#' to each class for each pixel.
#'
#'
#' @examples
#' if (sits_run_examples()) {
#' # create a random forest model
#' rfor_model <- sits_train(samples_modis_ndvi, sits_rfor())
#' # create a data cube from local files
#' data_dir <- system.file("extdata/raster/mod13q1", package = "sits")
#' cube <- sits_cube(
#' source = "BDC",
#' collection = "MOD13Q1-6",
#' data_dir = data_dir
#' )
#' # segment the image
#' segments <- sits_segment(
#' cube = cube,
#' seg_fn = sits_slic(step = 5,
#' compactness = 1,
#' dist_fun = "euclidean",
#' avg_fun = "median",
#' iter = 20,
#' minarea = 10,
#' verbose = FALSE),
#' output_dir = tempdir()
#' )
#' # classify a data cube
#' probs_vector_cube <- sits_classify(
#' data = segments,
#' ml_model = rfor_model,
#' output_dir = tempdir()
#' )
#' # measure uncertainty
#' uncert_vector_cube <- sits_uncertainty(
#' cube = probs_vector_cube,
#' type = "margin",
#' output_dir = tempdir()
#' )
#' # plot the resulting uncertainty cube
#' plot(uncert_vector_cube)
#' }
#'
#' @export
#'
plot.uncertainty_vector_cube <- function(x, ...,
tile = x[["tile"]][[1]],
palette = "RdYlGn",
rev = TRUE,
scale = 0.8) {
.check_set_caller(".plot_uncertainty_vector_cube")
# check for color_palette parameter (sits 1.4.1)
dots <- list(...)
if (missing(palette) && "color_palette" %in% names(dots)) {
warning(.conf("messages", ".plot_palette"))
palette <- dots[["color_palette"]]
}
# precondition
.check_chr_contains(
x = x[["tile"]],
contains = tile,
case_sensitive = FALSE,
discriminator = "one_of",