Skip to content

Commit

Permalink
add ggplot2 to distribution plots, report, and adjust automatic year …
Browse files Browse the repository at this point in the history
…ordering for dist_map

close #18; close #29
  • Loading branch information
dpseidel committed May 30, 2019
1 parent 8878a62 commit 23ab1f8
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 33 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ importFrom(dplyr,summarise)
importFrom(dplyr,sym)
importFrom(ggplot2,aes)
importFrom(ggplot2,element_text)
importFrom(ggplot2,geom_histogram)
importFrom(ggplot2,geom_segment)
importFrom(ggplot2,geom_sf)
importFrom(ggplot2,ggplot)
importFrom(ggplot2,labs)
importFrom(ggplot2,theme)
importFrom(ggplot2,theme_classic)
importFrom(ggplot2,theme_minimal)
importFrom(graphics,hist)
importFrom(graphics,par)
importFrom(graphics,plot)
Expand Down
21 changes: 11 additions & 10 deletions R/distributions.r
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#' @param x a dataframe with columns: x, y, date, and id (optional)
#' @param plot a logical indicating whether or not to return a histogram of the distribution
#' @return a numeric vector and (optionally) a plot of the turning angle or step length distribution
#' @importFrom ggplot2 geom_histogram labs theme_minimal
#' @export
#' @examples
#' \donttest{
Expand All @@ -25,11 +26,11 @@ ss_dist <- function(x, plot = T) {
dist <- traj[[1]]$dist

if (plot == T) {
hist(
x = dist,
xlab = "Step Length",
main = paste0("Step Length Distribution \n dt = ", dt, " secs")
)
ggplot() + geom_histogram(aes(x = dist)) +
labs(
x = "Step Length",
title = paste0("Step Length Distribution \n dt = ", dt, " secs")
) + theme_minimal()
}

return(dist)
Expand All @@ -48,11 +49,11 @@ ta_dist <- function(x, plot = T) {


if (plot == T) {
hist(
x = ang,
xlab = "Turning Angle",
main = paste0("(Relative) Turning Angle Distribution \n dt = ", dt, " secs")
)
ggplot() + geom_histogram(aes(
x = ang)) + labs(
x = "Turning Angle",
title = paste0("(Relative) Turning Angle Distribution \n dt = ", dt, " secs")
) + theme_minimal()
}

return(ang)
Expand Down
7 changes: 4 additions & 3 deletions R/population.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
#' @param labels logical, indicating whether or not to label points with IDs, implemented using
#' \link[ggrepel]{geom_text_repel}
#' @export
#' @importFrom ggplot2 ggplot aes geom_sf labs
#' @importFrom ggplot2 ggplot aes geom_sf labs theme_minimal
dist_map <- function(df, proj4, labels = TRUE) {
mean_sf <- df %>%
dplyr::group_by(.data$id) %>%
dplyr::summarise(
meanX = mean(.data$x, na.rm = T),
meanY = mean(.data$y, na.rm = T),
meanyear = factor(round(mean(lubridate::year(.data$date)), 0))
meanyear = as.character(round(mean(lubridate::year(.data$date)), 0))
) %>%
sf::st_as_sf(coords = c("meanX", "meanY"), remove = FALSE, crs = proj4)

Expand All @@ -22,7 +22,7 @@ dist_map <- function(df, proj4, labels = TRUE) {
labs(
title = "General Spatial and Temporal Distribution of Individuals",
x = "Latitude", y = "Longitude", color = "Year", fill = "Year"
)
) + theme_minimal()

if (labels == TRUE) {
print(p + ggrepel::geom_text_repel(mapping = aes(
Expand All @@ -47,6 +47,7 @@ plot_timeline <- function(df) {
df %>%
group_by(.data$id) %>%
summarise(start_date = min(.data$date), end_date = max(.data$date)) %>%
mutate(id = forcats::fct_reorder(.data$id, .data$start_date, .desc = T)) %>%
ggplot() + geom_segment(aes(
x = .data$start_date, xend = .data$end_date,
y = .data$id, yend = .data$id
Expand Down
56 changes: 36 additions & 20 deletions inst/reports/report.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ params:

```{r setup, include = F}
knitr::opts_chunk$set(
root.dir = tempdir(),
collapse = TRUE,
echo = FALSE,
message = FALSE,
Expand All @@ -37,7 +38,8 @@ wavelet <- (!is.null(params$wavelet))
# Visualize the trajectory

```{r}
plot(params$df$x, params$df$y, type = "p", xlab = "Easting", ylab = "Northing")
ggplot(params$df, aes(x, y)) + geom_point() + labs(x = "Easting", y = "Northing") +
theme_minimal()
```

# Step Length & Turning Angle Distributions:
Expand All @@ -58,10 +60,10 @@ ta <- ta_dist(x = params$df)
roll <<- rolling_stats(params$df)
p1 <- ggplot(roll, aes(date, y = mean_dist)) +
geom_point(na.rm = T) + geom_smooth(na.rm = T)
geom_path(na.rm = T)
p2 <- ggplot(roll, aes(date, y = mean_ang)) +
geom_point(na.rm = T) + geom_smooth(na.rm = T)
geom_path(na.rm = T)
suppressMessages(p1 + p2 + plot_annotation("Mean Step Size & Turning Angle on a Rolling Interval"))
Expand Down Expand Up @@ -153,23 +155,37 @@ plots <- purrr::map(
```{r eval = seasonal}
seasonal_stats <<- interval_stats(params$df, type = "seasonal", seas = params$seas)
# p1 <- ggplot(seasonal_stats,
# aes(x = as.numeric(interval_start), color = seas)) +
# geom_pointrange(aes( y = mean_dist, ymin = mean_dist - sd_dist, ymax = mean_dist + sd_dist)) +
# # geom_pointrange(aes( y = mean_ang, ymin = mean_ang - sd_ang, ymax = mean_ang + sd_ang)) +
# scale_x_continuous(breaks = 1:nlevels(seasonal_stats$interval_start),
# labels = seasonal_stats$seas) +
# xlab(NULL) +
# theme(axis.text.x = element_text(angle = 25, vjust = .5), legend.position = "None")
seasonal_stats %>%
tidyr::gather("stat", "estimate", -1:-2) %>%
ggplot(., aes(x = as.numeric(interval_start), y = estimate, color = stat)) +
geom_point() + # to make sure single seasons displayed.
geom_step() +
scale_x_continuous("Season", breaks = 1:nlevels(seasonal_stats$interval_start)) +
ggtitle("Seasonal Interval Statistics")
p1 <- seasonal_stats %>%
# tidyr::gather("stat", "estimate", -1) %>%
ggplot(., aes(x = as.numeric(interval_start), y = mean_dist)) +
geom_col() +
geom_errorbar(aes(
ymin = mean_dist,
ymax = mean_dist + sd_dist
)) + labs(x = "", y = "Average Step Size (+ 1 SD)")
p2 <- seasonal_stats %>%
ggplot(., aes(x = as.numeric(interval_start), y = mean_ang)) +
geom_col() +
labs(x = "Season", y = "Mean Turning Angle")
p3 <- seasonal_stats %>%
select(-mean_dist, -sd_dist, -mean_ang, -sd_ang) %>%
tidyr::gather("stat", "estimate", -1) %>%
ggplot(., aes(
x = as.factor(as.numeric(interval_start)), y = estimate,
group = stat, color = stat
)) +
geom_point() +
geom_line() +
lims(y = c(-1, 1)) +
labs(x = "")
p1 + p2 + p3 +
plot_annotation(
title = "Interval Statistics Across Seasons",
theme = theme(plot.title = element_text(hjust = .5))
) &
theme_minimal()
```

`r if (wavelet) '# Visualizing Periodicity'`
Expand Down

0 comments on commit 23ab1f8

Please sign in to comment.