R/plot_dsm.R

Defines functions plot_dsm

Documented in plot_dsm

# Generated by fusen: do not edit by hand

#' Plot dsm
#'
#' @param dsm_pred_obj dsm object. Le modele dsm créé. 
#' @param map_obj dataframe. La carte avec la densité associée
#' @param title character. Le titre souhaité pour le graphique
#' @param legend character. La légende souhiatée pour le graphique
#'
#' @importFrom dplyr mutate
#' @importFrom ggplot2 ggplot geom_sf aes coord_sf scale_fill_gradientn labs theme element_text 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 plot. La zone d'étude avec ce que le modèle a predit.
#' @export 

#' @examples
#' # TO DO
plot_dsm <- function(dsm_pred_obj, map_obj, title = "Detection", legend = "Marsouins\n(ind/km2)"){

  dsm_pred_plot  <- map_obj %>%
    as.data.frame() %>%
    mutate(density = dsm_pred_obj)

  theme_set(theme_bw())

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

  ggplot() +
    geom_sf(data = dsm_pred_plot$geometry, aes(fill = dsm_pred_plot$density), color = NA) +
    coord_sf(xlim = xlim, ylim = ylim) +
    annotation_scale(location = "br", width_hint = 0.5) +
    annotation_north_arrow(location = "tr",
                           which_north = "true",
                           height = unit(1, "cm"),
                           width = unit(1, "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) +
    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 = 8))
}
maudqueroue/intercali documentation built on Oct. 8, 2022, 2:09 p.m.