Skip to content

Commit

Permalink
Updated plot_resp
Browse files Browse the repository at this point in the history
  • Loading branch information
SkylarMarvel committed Nov 1, 2024
1 parent 3e68d94 commit aa0c9a1
Show file tree
Hide file tree
Showing 3 changed files with 223 additions and 5 deletions.
54 changes: 49 additions & 5 deletions R/plot_resp.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,44 @@
#'
#' @return ggplot2 object.
#' @export
#'
#' @examples
#' # Use example boundary data from package
#' region_boundary <- geo_tox_data$boundaries$county
#' group_boundary <- geo_tox_data$boundaries$state
#' n <- nrow(region_boundary)
#'
#' # Single assay quantile
#' df <- data.frame(id = region_boundary$FIPS,
#' metric = "GCA.Eff",
#' assay_quantile = 0.5,
#' value = runif(n)^3)
#' # Default plot
#' plot_resp(df, region_boundary)
#' # Add group boundary, a state border in this case
#' plot_resp(df, region_boundary, group_boundary)
#' # Change quantile label
#' plot_resp(df, region_boundary, group_boundary,
#' assay_quantiles = c("Q50" = 0.5))
#'
#' # Multiple assay quantiles
#' df <- data.frame(id = rep(region_boundary$FIPS, 2),
#' metric = "GCA.Eff",
#' assay_quantile = rep(c(0.25, 0.75), each = n),
#' value = c(runif(n)^3, runif(n)^3 + 0.15))
#' plot_resp(df, region_boundary, group_boundary,
#' assay_quantiles = c("Q25" = 0.25, "Q75" = 0.75))
#'
#' # Summary quantiles
#' df <- data.frame(id = rep(region_boundary$FIPS, 4),
#' assay_quantile = rep(rep(c(0.25, 0.75), each = n), 2),
#' summary_quantile = rep(c(0.05, 0.95), each = n * 2),
#' metric = "GCA.Eff",
#' value = c(runif(n)^3, runif(n)^3 + 0.15,
#' runif(n)^3 + 0.7, runif(n)^3 + 0.85))
#' plot_resp(df, region_boundary, group_boundary,
#' assay_quantiles = c("A_Q25" = 0.25, "A_Q75" = 0.75),
#' summary_quantiles = c("S_Q05" = 0.05, "S_Q95" = 0.95))
plot_resp <- function(
df,
region_boundary,
Expand Down Expand Up @@ -45,9 +83,12 @@ plot_resp <- function(

metric <- df$metric[1]

fig <- ggplot2::ggplot(df, ggplot2::aes(fill = .data$value)) +
fig <- ggplot2::ggplot() +
# Plot county data using fill, hide county borders by setting color = NA
ggplot2::geom_sf(ggplot2::aes(geometry = .data$geometry), color = NA) +
ggplot2::geom_sf(data = df,
ggplot2::aes(fill = .data$value,
geometry = .data$geometry),
color = NA) +
# Add fill scale
ggplot2::scale_fill_viridis_c(
name = metric,
Expand Down Expand Up @@ -91,15 +132,18 @@ plot_resp <- function(
# Create separate plots for each stat
ggplot2::facet_wrap(
~assay_quantile,
ncol = length(assay_quantiles),
ncol = length(unique(df$assay_quantile)),
labeller = ggplot2::labeller(
assay_quantile = stats::setNames(names(assay_quantiles),
assay_quantiles)))
}

if (!is.null(group_boundary)) {
fig <- fig + ggplot2::geom_sf(data = group_boundary, fill = NA,
size = 0.15)
fig <- fig +
ggplot2::geom_sf(data = group_boundary,
ggplot2::aes(geometry = .data$geometry),
fill = NA,
size = 0.15)
}

fig
Expand Down
38 changes: 38 additions & 0 deletions man/plot_resp.Rd

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

136 changes: 136 additions & 0 deletions tests/testthat/test-plot_resp.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
test_that("warnings and errors", {

df <- data.frame(id = c("r1", "r2"),
metric = "GCA.Eff",
assay_quantile = 0.5,
value = 1:2)

unit_square <- matrix(c(0, 0, 1, 0, 1, 1, 0, 1, 0, 0), ncol = 2, byrow = TRUE)

region_boundary <- data.frame(
region = c("r1", "r2"),
geometry = sf::st_sfc(
sf::st_polygon(list(unit_square)),
sf::st_polygon(list(unit_square + 1))
)
)

expect_no_error(plot_resp(df, region_boundary))

df$value <- NA

expect_no_error(plot_resp(df, region_boundary))

expect_error(plot_resp(df, region_boundary, assay_quantiles = 0.5),
"Both assay_quantiles and summary_quantiles must be named")
expect_error(plot_resp(df, region_boundary, summary_quantiles = 0.1),
"Both assay_quantiles and summary_quantiles must be named")

region_boundary <- region_boundary[1, , drop = FALSE]

expect_warning(plot_resp(df, region_boundary),
"Some response data was removed due to missing spatial data")

region_boundary$region <- "r3"

expect_error(plot_resp(df, region_boundary),
"No spatial data for corresponding response data")
})

test_that("no summary data", {

assay_quantiles <- c("aq25" = 0.25, "aq75" = 0.75)

df <- data.frame(id = rep(c("r1", "r2"), each = 2),
metric = "GCA.Eff",
assay_quantile = rep(assay_quantiles, 2),
value = 1:4)

unit_square <- matrix(c(0, 0, 1, 0, 1, 1, 0, 1, 0, 0), ncol = 2, byrow = TRUE)

region_boundary <- data.frame(
region = c("r1", "r2"),
geometry = sf::st_sfc(
sf::st_polygon(list(unit_square)),
sf::st_polygon(list(unit_square + 1))
)
)

group_boundary <- data.frame(
geometry = sf::st_sfc(
sf::st_polygon(list(unit_square * 2))
)
)

expect_no_error(plot_resp(df, region_boundary))
expect_no_error(plot_resp(df, region_boundary, group_boundary))
expect_no_error(plot_resp(df, region_boundary, group_boundary,
assay_quantiles))
})

test_that("with summary data", {

assay_quantiles <- c("aq25" = 0.25, "aq75" = 0.75)
summary_quantiles = c("sq10" = 0.1, "sq90" = 0.9)

df <- data.frame(id = rep(c("r1", "r2"), each = 4),
assay_quantile = rep(rep(assay_quantiles, 2), each = 2),
metric = "GCA.Eff",
summary_quantile = rep(summary_quantiles, 4),
value = 1:8)

unit_square <- matrix(c(0, 0, 1, 0, 1, 1, 0, 1, 0, 0), ncol = 2, byrow = TRUE)

region_boundary <- data.frame(
region = c("r1", "r2"),
geometry = sf::st_sfc(
sf::st_polygon(list(unit_square)),
sf::st_polygon(list(unit_square + 1))
)
)

group_boundary <- data.frame(
geometry = sf::st_sfc(
sf::st_polygon(list(unit_square * 2))
)
)

expect_no_error(plot_resp(df, region_boundary))
expect_no_error(plot_resp(df, region_boundary, group_boundary))
expect_no_error(plot_resp(df, region_boundary, group_boundary,
assay_quantiles))
expect_no_error(plot_resp(df, region_boundary, group_boundary,
assay_quantiles, summary_quantiles))
})

test_that("with assay", {

assay_quantiles <- c("aq25" = 0.25, "aq75" = 0.75)

df <- data.frame(id = rep(c("r1", "r2"), each = 2),
assay = rep("a1", 4),
metric = "GCA.Eff",
assay_quantile = rep(assay_quantiles, 2),
value = 1:4)

unit_square <- matrix(c(0, 0, 1, 0, 1, 1, 0, 1, 0, 0), ncol = 2, byrow = TRUE)

region_boundary <- data.frame(
region = c("r1", "r2"),
geometry = sf::st_sfc(
sf::st_polygon(list(unit_square)),
sf::st_polygon(list(unit_square + 1))
)
)

group_boundary <- data.frame(
geometry = sf::st_sfc(
sf::st_polygon(list(unit_square * 2))
)
)

expect_no_error(plot_resp(df, region_boundary))
expect_no_error(plot_resp(df, region_boundary, group_boundary))
expect_no_error(plot_resp(df, region_boundary, group_boundary,
assay_quantiles))
})

0 comments on commit aa0c9a1

Please sign in to comment.