R/plot_detect.R

Defines functions plot_detect

Documented in plot_detect

# Generated by fusen: do not edit by hand


#' Plot detection
#'
#' This function allows to highligth individuals detected according to the sample design and the detection function. The function represents on the study map, the different transects of the sample design and highligtht in dark blue the detected (simulated) individuals while other non detected (simulated) individuals are in grey. 
#' @param dist_obj sf dataframe. Distances between individuals and associated transect/segment and probability that the individual is detected or not.
#' @param transect_obj sf dataframe. Transect/segments data.
#' @param map_obj sf dataframe. Map of the study area with the density.
#' @param title character. Title for the graph.
#'
#' @importFrom ggplot2 ggplot geom_sf geom_point aes coord_sf scale_fill_gradientn labs theme element_text element_blank element_rect element_line theme_set theme_bw unit
#' @importFrom ggspatial annotation_scale annotation_north_arrow north_arrow_fancy_orienteering
#' @importFrom sp bbox
#' @importFrom sf as_Spatial st_sf st_union
#'
#' @return ggplot object. A map highligthing detected individuals.
#' @export

#' @examples
#' data("dataset_detected")
#' data("dataset_segs")
#' 
#' plot_detect(dist_obj = dataset_detected, 
#'             transect_obj = dataset_segs, 
#'             map_obj = dataset_map, 
#'             title = "Detected individuals")
plot_detect <- function(dist_obj, transect_obj, map_obj, title) {

  # Function checks
  
  assert_that(inherits(dist_obj, "data.frame"))
  assert_that(inherits(transect_obj, "sf"))
  assert_that(inherits(map_obj, "sf"))
    
  if (!all(c("distance_km", "distance_m") %in% names(dist_obj))) {stop("dist_obj must contain `distance_km` and `distance_m` columns. Verify your column names.")}
  assert_that(is.numeric(dist_obj$distance_m))
  assert_that(is.numeric(dist_obj$distance_km))

  # Function
  # on veut les contours
  contour_obj <- map_obj %>%
    st_union()

  # bounding box
  xlim <- bbox(as_Spatial(contour_obj))[1, ]
  ylim <- bbox(as_Spatial(contour_obj))[2, ]

  # Plot detection
  theme_set(theme_bw())
  ggplot() +
    geom_sf(data = transect_obj, color = "black") +
    geom_sf(data = contour_obj, aes(), color = "black", alpha = 0) +
    geom_point(data = dist_obj[dist_obj$detected == 0, ], aes(x = x, y = y), alpha = 0.3, shape = 20) +
    geom_point(data = dist_obj[dist_obj$detected == 1, ], aes(x = x, y = y), shape = 21, fill = "midnightblue") +
    coord_sf(xlim = xlim, ylim = ylim) +
    annotation_scale(location = "br", width_hint = 0.5) +
    annotation_north_arrow(location = "tr",
                           which_north = "true",
                           height = unit(0.8, "cm"),
                           width = unit(0.8, "cm"),
                           pad_x = unit(0.2, "cm"),
                           pad_y = unit(0.1, "cm"),
                           style = north_arrow_fancy_orienteering) +
    labs(title = title, caption = paste("Sightings = ", sum(dist_obj$detected), sep = " ")) +
    theme(legend.position = "bottom",
          legend.key.width = unit(0.5, "cm"),
          legend.text = element_text(size = 6),
          panel.grid = element_line(colour = "transparent"),
          plot.title = element_text(lineheight = 0.8, face = "bold"),
          axis.text = element_text(size = 6),
          strip.background = element_rect(fill = "white"),
          axis.title.x = element_blank(),
          axis.title.y = element_blank(),
          panel.background = element_rect(fill = "azure"),
          panel.border = element_rect(fill = NA))
}
maudqueroue/intercali documentation built on Oct. 8, 2022, 2:09 p.m.