Skip to content

Commit

Permalink
Feature: different pie sizes for sites
Browse files Browse the repository at this point in the history
The `pie_size` parameter now accepts a single value or a vector of values the same length as the number of sites. This allows users to plot pie of different sizes for different sites.
  • Loading branch information
Tom-Jenkins committed Dec 11, 2024
1 parent 33e6b46 commit e5da2be
Showing 4 changed files with 24 additions and 11 deletions.
11 changes: 8 additions & 3 deletions R/func_mapmixture.R
Original file line number Diff line number Diff line change
@@ -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(
20 changes: 14 additions & 6 deletions R/func_pie_charts.R
Original file line number Diff line number Diff line change
@@ -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,21 +77,29 @@ 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,
# If absolute number has greater than 3 digits
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
2 changes: 1 addition & 1 deletion man/add_pie_charts.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/mapmixture.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e5da2be

Please sign in to comment.