diff --git a/R/func_mapmixture.R b/R/func_mapmixture.R index 70c6fe3..d03073f 100644 --- a/R/func_mapmixture.R +++ b/R/func_mapmixture.R @@ -18,7 +18,7 @@ #' @param basemap SpatRaster or sf object to use as the basemap. A SpatRaster object can be created from a file #' using the `terra::rast()` function. A sf object can be created from a file #' using the `sf::st_read()` function. If `NULL`, world country boundaries are used. -#' @param pie_size numeric value of zero or greater. +#' @param pie_size vector of numeric values of zero or greater. Can be a single value or a vector the same length as the number of sites. #' @param pie_border numeric value of zero or greater. #' @param pie_border_col string denoting colour of pie border. #' @param pie_opacity numeric value of zero to one. @@ -137,7 +137,12 @@ mapmixture <- function( # Check coordinate site IDs exactly match admixture site IDs if ( all(coords_df[[1]] == unique(admixture_df[[1]])) == FALSE ) { - stop("Site names in coordinates data frame do not match site names in admixture data frame. Check site names are not empty or NA and that they match.") + stop("Invalid input: site names in coordinates data frame do not match site names in admixture data frame. Check site names are not empty or NA and that they match.") + } + + # Check if pie_size vector is not 1 and if pie_size is not the same length as the number of sites + if ( length(pie_size) != 1 & length(pie_size) != dplyr::n_distinct(admixture_df[[1]]) ) { + stop("Invalid input: pie_size vector must be of length 1 or the same length as the number of sites") } # Transform admixture data into a plotting format ---- @@ -282,7 +287,7 @@ mapmixture <- function( ggplot2::geom_point( data = legend_data, ggplot2::aes(x = !!as.name("x"), y = !!as.name("y"), fill = !!as.name("cluster")), - shape = 22, colour = "black", stroke = 0.3, size = 5 * pie_size , alpha = 0 + shape = 22, colour = "black", stroke = 0.3, size = 5 * min(pie_size) , alpha = 0 )+ ggplot2::scale_fill_manual(values = cluster_cols, labels = stringr::str_to_title(cluster_names))+ ggplot2::theme( diff --git a/R/func_pie_charts.R b/R/func_pie_charts.R index 260b716..de24c50 100644 --- a/R/func_pie_charts.R +++ b/R/func_pie_charts.R @@ -12,7 +12,7 @@ #' @param border numeric value of zero or greater. #' @param border_col string denoting colour of pie border. #' @param opacity numeric value of zero to one. -#' @param pie_size numeric value of zero or greater. +#' @param pie_size vector of numeric values of zero or greater. Can be a single value or a vector the same length as the number of sites. #' #' @return A list of `annotation_custom()` objects. #' @export @@ -77,7 +77,7 @@ add_pie_charts <- function(df, admix_columns, lat_column, lon_column, pie_colour border_col = border_col )) - # Pie chart size formula + # Pie chart size formula (returns a single value or a vector of values depending on pie_size) radius <- dplyr::case_when( # If absolute number has less than or equal to 3 digits floor(log10(abs(coords$lat[1]))) + 1 <= 3 ~ 1 * pie_size, @@ -85,13 +85,21 @@ add_pie_charts <- function(df, admix_columns, lat_column, lon_column, pie_colour floor(log10(abs(coords$lat[1]))) + 1 > 3 && floor(log10(abs(coords$lat[1]))) ~ 80000 * pie_size, ) + # Prepare radius vector for input map() function (GitHub #25) + radius_list <- vector("numeric", length = length(radius)) + if (length(radius) == 1) { + radius_list <- rep(radius, length(pie_list)) + } else { + radius_list <- radius + } + # Convert pie chart ggplot objects to annotation custom geom objects pie_annotation <- purrr::map(1:length(pie_list), ~ ggplot2::annotation_custom( grob = ggplot2::ggplotGrob(pie_list[[.]]), - ymin = coord_list[[.]][1] - radius, - ymax = coord_list[[.]][1] + radius, - xmin = coord_list[[.]][2] - radius, - xmax = coord_list[[.]][2] + radius + ymin = coord_list[[.]][1] - radius_list[[.]], + ymax = coord_list[[.]][1] + radius_list[[.]], + xmin = coord_list[[.]][2] - radius_list[[.]], + xmax = coord_list[[.]][2] + radius_list[[.]] )) # Return list of annotation_custom objects diff --git a/man/add_pie_charts.Rd b/man/add_pie_charts.Rd index 5d1a06b..136279c 100644 --- a/man/add_pie_charts.Rd +++ b/man/add_pie_charts.Rd @@ -33,7 +33,7 @@ add_pie_charts( \item{opacity}{numeric value of zero to one.} -\item{pie_size}{numeric value of zero or greater.} +\item{pie_size}{vector of numeric values of zero or greater. Can be a single value or a vector the same length as the number of sites.} } \value{ A list of \code{annotation_custom()} objects. diff --git a/man/mapmixture.Rd b/man/mapmixture.Rd index 3f1ab6c..b239431 100644 --- a/man/mapmixture.Rd +++ b/man/mapmixture.Rd @@ -55,7 +55,7 @@ See \code{?sf::st_crs} for details.} using the \code{terra::rast()} function. A sf object can be created from a file using the \code{sf::st_read()} function. If \code{NULL}, world country boundaries are used.} -\item{pie_size}{numeric value of zero or greater.} +\item{pie_size}{vector of numeric values of zero or greater. Can be a single value or a vector the same length as the number of sites.} \item{pie_border}{numeric value of zero or greater.}