R/plot_obs.R

Defines functions plot_obs

Documented in plot_obs

# Generated by fusen: do not edit by hand


#' Plot simulated individuals

#' This function allows to plot the simulated individuals obs_obj on the map with density information. It is nessary to use the map_obj, a sf dataframe, contaning at least, a colums density_km. The title and the legend can be personnalized.
#'
#' @param obs_obj sf dataframe. Individuals simulated with their coordinates. 
#' @param map_obj sf dataframe. Map of the study area with a density column density_km.
#' @param title Character. Title.
#' @param legend Character. Legend.
#'
#' @importFrom ggplot2 ggplot geom_sf aes coord_sf scale_fill_gradientn geom_point scale_size labs theme element_text element_blank theme_set theme_bw unit
#' @importFrom ggspatial annotation_scale annotation_north_arrow north_arrow_fancy_orienteering
#' @importFrom viridisLite viridis
#' @importFrom sp bbox
#' @importFrom sf as_Spatial
#'
#' @return ggplot object. Study area with the simulates individuals and the density color gradient.
#' @export 

#' @examples
#' 
#' data(dataset_map)
#' 
#' ind <- simulate_ind(map_obj = dataset_map,
#'                     crs = 2154)
#' 
#' plot_obs(obs_obj = ind,
#'          map_obj = dataset_map)
#' 
plot_obs <- function(obs_obj, map_obj, legend = "Density (ind/km2)", title = ""){

  # Function checks
  
  assert_that(inherits(map_obj, "sf"))
  if (!all(c("density_km") %in% names(map_obj))) {stop("map_obj must contain `density_km` column. Verify your column names.")}
  assert_that(is.numeric(map_obj$density_km))
  if (!all(c("x", "y") %in% names(obs_obj))) {stop("obs_obj must contain `x` and `y` columns. Verify your column names.")}
  assert_that(is.numeric(obs_obj$x))
  assert_that(is.numeric(obs_obj$y))
  
  # Function
  theme_set(theme_bw())

  xlim <- bbox(as_Spatial(map_obj))[1, ]
  ylim <- bbox(as_Spatial(map_obj))[2, ]

  ggplot() +
    geom_sf(data = map_obj, aes(fill = density_km), color = NA) +
    geom_point(data = obs_obj, aes(x = x, y = y), size = 0.5) +
    scale_size(name = "Nb ind", breaks = 0:3) +
    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) +
    scale_fill_gradientn(name = legend,
                         colors = viridis(256)) +
    labs(title = title,
         caption = paste("Nb simulations = ", nrow(obs_obj), sep = " ")) +
    theme(legend.position = "right",
          legend.key.width = unit(0.5, "cm"),
          plot.title = element_text(lineheight = 0.8, face = "bold"),
          axis.text = element_text(size = 6),
          axis.title.x = element_blank(),
          axis.title.y = element_blank())
}
maudqueroue/intercali documentation built on Oct. 8, 2022, 2:09 p.m.