-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from g-rppl/dev
Add visualisation methods
- Loading branch information
Showing
15 changed files
with
270 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,5 @@ | |
^inst/Stan/.*\.exe$ | ||
^test\.R$ | ||
^TODO\.md$ | ||
^doc$ | ||
^Meta$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,5 @@ inst/doc | |
docs | ||
TODO.md | ||
test.R | ||
/doc/ | ||
/Meta/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ Title: Estimate flight tracks from telemetry data | |
Description: `stantrackr` is a `R` package that provides simple functionality | ||
to estimate flight tracks from telemetry data using random walk models written | ||
in Stan. | ||
Version: 0.2.0 | ||
Version: 0.3.0 | ||
License: MIT + file LICENSE | ||
Authors@R: | ||
person("Georg", "Rüppel", , "[email protected]", role = c("aut", "cre"), | ||
|
@@ -16,7 +16,8 @@ Imports: | |
HDInterval, | ||
cmdstanr, | ||
dplyr, | ||
lubridate | ||
lubridate, | ||
ggplot2 | ||
Additional_repositories: | ||
https://mc-stan.org/r-packages/ | ||
SystemRequirements: CmdStan (https://mc-stan.org/users/interfaces/cmdstan) | ||
|
@@ -28,7 +29,6 @@ RoxygenNote: 7.2.3 | |
Suggests: | ||
knitr, | ||
rmarkdown, | ||
tidyverse, | ||
sf, | ||
leaflet, | ||
sfheaders, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
#' Plot model results | ||
#' | ||
#' @description | ||
#' Plot model results per individual and variable. | ||
#' | ||
#' @param x An object of class `stantrackr`. | ||
#' @param vars The variables to plot. Defaults to `c("lon", "lat")`. | ||
#' @param id The individuals to plot. Defaults to `NULL` which plots all | ||
#' individuals. | ||
#' @param ... Additional arguments passed to `stantrckr::summary()`. | ||
#' | ||
#' @return | ||
#' Returns one or multiple `ggplot` plots. | ||
#' | ||
#' @seealso `stantrackr::summary()` | ||
#' | ||
#' @examples | ||
#' \dontrun{ | ||
#' # Set ggplot theme | ||
#' theme_set(theme_bw(base_size = 20)) | ||
#' | ||
#' # Plot | ||
#' plot(fit) | ||
#' plot(fit, vars = "speed", prob = 0.89, ci = "ETI") | ||
#' } | ||
#' | ||
#' @importFrom dplyr filter | ||
#' @importFrom ggplot2 ggplot aes .data geom_segment geom_point ggtitle ylab | ||
#' | ||
#' @method plot stantrackr | ||
#' @export | ||
#' | ||
plot.stantrackr <- function(x, vars = c("lon", "lat"), id = NULL, ...) { | ||
if (is.null(id)) { | ||
id <- unique(x$ID) | ||
} | ||
for (i in id) { | ||
for (var in vars) { | ||
g <- summary(x, var, ...) |> | ||
filter(.data$ID == i) |> | ||
ggplot() + | ||
geom_segment(aes( | ||
x = .data$time, y = .data[[paste0(var, ".lower")]], | ||
xend = .data$time, yend = .data[[paste0(var, ".upper")]] | ||
), alpha = 0.2) + | ||
geom_point(aes(x = .data$time, y = .data[[paste0(var, ".mean")]])) + | ||
ggtitle(paste("ID:", i)) + | ||
ylab(var) | ||
plot(g) | ||
} | ||
} | ||
} | ||
|
||
#' Map model result | ||
#' | ||
#' @description | ||
#' Map individual flight trajectories and model uncertainty. | ||
#' | ||
#' @param fit An object of class `stantrackr`. | ||
#' @param id The individuals to plot. Defaults to `NULL` which plots all | ||
#' individuals. | ||
#' @param nsim The number of posterior draws to plot. Defaults to `50`. | ||
#' @param lwd The line width for the mean trajectory. Defaults to `2`. | ||
#' @param alpha The alpha value for the posterior draws. Defaults to `0.1`. | ||
#' | ||
#' @return | ||
#' Returns an overview map with the mean trajectories and `nsim` posterior draws | ||
#' per individual. | ||
#' | ||
#' @examples | ||
#' \dontrun{ | ||
#' mapTrack(fit) | ||
#' mapTrack(fit, nsim = 100, alpha = 0.05) | ||
#' } | ||
#' | ||
#' @importFrom ggplot2 ggplot aes .data geom_path labs | ||
#' | ||
#' @export | ||
#' | ||
mapTrack <- function(fit, id = NULL, nsim = 50, lwd = 2, alpha = 0.1) { | ||
if (is.null(id)) { | ||
id <- unique(fit$ID) | ||
} | ||
g <- fit |> | ||
as.data.frame() |> | ||
filter(.data$ID %in% id) |> | ||
ggplot() | ||
if (nsim > 0) { | ||
draws <- getDraws(fit, nsim = nsim) | ||
g <- g + | ||
geom_path( | ||
data = draws |> filter(.data$ID %in% id), | ||
aes(.data$lon, .data$lat, group = .data$tID), alpha = alpha | ||
) | ||
} | ||
g + | ||
geom_path(aes(.data$lon, .data$lat, colour = as.factor(.data$ID)), | ||
lwd = lwd | ||
) + | ||
labs(colour = "ID") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.