Open KilianGRIHAULTBARREIRO opened 7 months ago
New version :
splnr_plot_cost_4 <- function(df, col_names = NULL,
legendTitle = "Cost",
plotTitle = "",
paletteName = "YlGnBu") {
gg <- ggplot2::ggplot() +
ggplot2::coord_sf(xlim = sf::st_bbox(df)$xlim, ylim = sf::st_bbox(df)$ylim) +
ggplot2::labs(subtitle = plotTitle, fill = legendTitle)
# If col_names is NULL, use all columns in the data
if (is.null(col_names)) {
stop("Please specify the column(s) of data to display.")
}
# Replace NA with 0 in selected columns for binary data
df <- dplyr::mutate_all(df, ~replace_na(.,0))
# Create a new binary column indicating the presence of any non-missing value in selected columns for binary data
is_binary <- all(sapply(col_names, function(col_name) all(df[[col_name]] %in% c(0, 1))))
if (is_binary) {
df$UnionColumn <- apply(df[, col_names, drop = FALSE], 1, function(x) as.numeric(any(x == 1, na.rm = TRUE)))
gg <- gg +
ggplot2::geom_sf(data = df, ggplot2::aes(fill = UnionColumn),
colour = "grey80", size = 0.1, show.legend = TRUE) +
ggplot2::scale_fill_gradient(low = "#c6dbef", high = "#3182bd", limits = c(0, 1))
} else {
for (col_name in col_names) {
gg <- gg +
ggplot2::geom_sf(data = df, ggplot2::aes(fill = !!rlang::sym(col_name)),
colour = "grey80", size = 0.1, show.legend = TRUE) +
ggplot2::scale_fill_gradientn(
colors = rev(RColorBrewer::brewer.pal(100, paletteName)),
limits = NULL
)
}
}
return(gg)
}
Following discussion, let's try and make 1 general plotting function that will replace many of the plotting functions in spatialplanr. This generic function should take binary, factorial (many levels such as "epipelagic", "mesopelagic", "bathypelagic" etc:) and continuous data.
This function WILL NOT replace all plotting functions. Let's start with a few first.
Maybe
@KilianGRIHAULTBARREIRO Can you please provide an update of where you are with this and what functions you intend on working on next?
The function splnr_plot() can replace splnr_plot_cost
, splnr_plot_binFeature
, splnr_plot_MPAs
, and splnr_plot_featureNo
.
It works with splnr_plot_PUs
, and splnr_plot_solution
, but requires display (color/legend) adjustment
It can be generalised to all sf objects (or with a geometry
column), whether they contain discrete or continuous values.
I am working on the splnr_plot_costOverlay
integration
Ok. I'm not sure what you mean about requiring "display adjustment" but it would be good to get these two (splnr_plot_PUs
, and splnr_plot_solution
) working properly. If it's something you need help with, let me know.
Each time you have a new function working with splnr_plot
, update the vignettes to use the new function so we can all see how to do it. Then we are happy we can delete the old function from the package.
Every function I cited can be replaced by splnr_plot, it's just not the exact same display as the previous one done by the replaced function. I already replaced those in the vignette, that should be good.
I have added the ability for splnr_plot_PUs
to work with splnr_plot
Check if splnr_plot_solution
can be removed and replaced with this function instead
We want to make the function
splnr_plot_cost
more generic to handle multi-columns binary and continuous data.