Skip to content

Commit

Permalink
Bring dependency and use of nmfspalette back into package
Browse files Browse the repository at this point in the history
  • Loading branch information
Schiano-NOAA committed Jan 15, 2025
1 parent 5edefc0 commit 42be843
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 86 deletions.
3 changes: 3 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,15 @@ Imports:
kableExtra,
knitr,
naniar,
nmfspalette,
officer,
stringr,
tibble,
tidyr,
tidyselect,
utils
Remotes:
nmfs-fish-tools/nmfspalette
Depends:
R (>= 2.10)
Suggests:
Expand Down
4 changes: 2 additions & 2 deletions R/add_theme.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ add_theme <- function(x) {
# add nmfs color palette (palette will be default)
# I believe including both functions is fine and will
# works regardless of the type of plot (one won't be used)
# nmfspalette::scale_color_nmfs() +
# nmfspalette::scale_fill_nmfs()
nmfspalette::scale_color_nmfs() +
nmfspalette::scale_fill_nmfs()
# Determining how to treat a legend if there is one
# check if one is present
# check_for_legend <- function(x) {
Expand Down
168 changes: 84 additions & 84 deletions tests/testthat/test-add_theme.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,87 +35,87 @@ test_that("add_theme applies NOAA formatting correctly", {
expect_error(add_theme(unsupported_obj2))
})

# test_that("nmfspalette returns correct scales", {
# # Test 1: Ensure nmfspalette integrates with ggplot color scales
# ggplot_obj <- ggplot2::ggplot(data = cars, ggplot2::aes(x = speed, y = dist, color = speed)) +
# ggplot2::geom_point() +
# nmfspalette::scale_color_nmfs()
#
# # Check if the correct scale is added
# expect_true("ScaleDiscrete" %in% class(ggplot_obj$scales$scales[[1]]))
# expect_true(ggplot_obj$scales$scales[[1]]$aesthetics == "colour")
#
# # Test 2: Ensure fill scale works with ggplot
# ggplot_fill_obj <- ggplot2::ggplot(data = cars, ggplot2::aes(x = factor(speed), fill = factor(speed))) +
# ggplot2::geom_bar() +
# nmfspalette::scale_fill_nmfs()
#
# # Check if the correct fill scale is applied
# expect_true("ScaleDiscrete" %in% class(ggplot_fill_obj$scales$scales[[1]]))
# expect_true(ggplot_fill_obj$scales$scales[[1]]$aesthetics == "fill")
# })
#
# test_that("nmfspalette scales are applied to ggplot object", {
# p <- ggplot2::ggplot(mtcars, ggplot2::aes(x = wt, y = mpg, color = as.factor(cyl))) +
# ggplot2::geom_point()
# formatted_plot <- add_theme(p)
#
# # Extract the color and fill scales from the plot
# scales <- formatted_plot$scales$scales
#
# # Check if the color scale is from nmfspalette
# color_scale <- scales[[1]]
# expect_true("ScaleDiscrete" %in% class(color_scale)) # It's a discrete scale
# expect_true(grepl("nmfs", deparse(color_scale$call))) # Check if nmfspalette scale is used
#
# # Ensure the color scale is the default "oceans" nmfspalette scale
# test_plot <- ggplot2::ggplot(
# Orange,
# ggplot2::aes(
# x = age,
# y = circumference,
# color = Tree
# )
# ) +
# ggplot2::geom_smooth(
# se = F,
# method = "loess",
# formula = "y ~ x"
# )
#
# ## use default color palette
# plot_default <- test_plot +
# nmfspalette::scale_color_nmfs()
#
# ## use oceans color palette
# plot_oceans <- test_plot +
# nmfspalette::scale_color_nmfs("oceans", 5)
#
# ## extract colors used for both plots
# plot_default_build <- ggplot2::ggplot_build(plot_default)
# colors_default <- unique(plot_default_build$data[[1]]["colour"])
#
# plot_oceans_build <- ggplot2::ggplot_build(plot_oceans)
# colors_oceans <- unique(plot_oceans_build$data[[1]]["colour"])
#
# ## test if plots' colors are identical
# expect_equal(colors_default, colors_oceans) # Check if the colors match
# })
#
# # Test for both color and fill scales
# test_that("nmfspalette scales apply both color and fill scales", {
# p <- ggplot2::ggplot(mtcars, ggplot2::aes(x = wt, y = mpg, color = as.factor(cyl), fill = as.factor(cyl))) +
# ggplot2::geom_point()
# formatted_plot <- add_theme(p)
#
# # Extract the scales from the plot
# scales <- formatted_plot$scales$scales
#
# # Check color scale
# color_scale <- scales[[1]]
# expect_true(grepl("nmfs", deparse(color_scale$call))) # Color scale from nmfspalette
#
# # Check fill scale
# fill_scale <- scales[[2]]
# expect_true(grepl("nmfs", deparse(fill_scale$call))) # Fill scale from nmfspalette
# })
test_that("nmfspalette returns correct scales", {
# Test 1: Ensure nmfspalette integrates with ggplot color scales
ggplot_obj <- ggplot2::ggplot(data = cars, ggplot2::aes(x = speed, y = dist, color = speed)) +
ggplot2::geom_point() +
nmfspalette::scale_color_nmfs()

# Check if the correct scale is added
expect_true("ScaleDiscrete" %in% class(ggplot_obj$scales$scales[[1]]))
expect_true(ggplot_obj$scales$scales[[1]]$aesthetics == "colour")

# Test 2: Ensure fill scale works with ggplot
ggplot_fill_obj <- ggplot2::ggplot(data = cars, ggplot2::aes(x = factor(speed), fill = factor(speed))) +
ggplot2::geom_bar() +
nmfspalette::scale_fill_nmfs()

# Check if the correct fill scale is applied
expect_true("ScaleDiscrete" %in% class(ggplot_fill_obj$scales$scales[[1]]))
expect_true(ggplot_fill_obj$scales$scales[[1]]$aesthetics == "fill")
})

test_that("nmfspalette scales are applied to ggplot object", {
p <- ggplot2::ggplot(mtcars, ggplot2::aes(x = wt, y = mpg, color = as.factor(cyl))) +
ggplot2::geom_point()
formatted_plot <- add_theme(p)

# Extract the color and fill scales from the plot
scales <- formatted_plot$scales$scales

# Check if the color scale is from nmfspalette
color_scale <- scales[[1]]
expect_true("ScaleDiscrete" %in% class(color_scale)) # It's a discrete scale
expect_true(grepl("nmfs", deparse(color_scale$call))) # Check if nmfspalette scale is used

# Ensure the color scale is the default "oceans" nmfspalette scale
test_plot <- ggplot2::ggplot(
Orange,
ggplot2::aes(
x = age,
y = circumference,
color = Tree
)
) +
ggplot2::geom_smooth(
se = F,
method = "loess",
formula = "y ~ x"
)

## use default color palette
plot_default <- test_plot +
nmfspalette::scale_color_nmfs()

## use oceans color palette
plot_oceans <- test_plot +
nmfspalette::scale_color_nmfs("oceans", 5)

## extract colors used for both plots
plot_default_build <- ggplot2::ggplot_build(plot_default)
colors_default <- unique(plot_default_build$data[[1]]["colour"])

plot_oceans_build <- ggplot2::ggplot_build(plot_oceans)
colors_oceans <- unique(plot_oceans_build$data[[1]]["colour"])

## test if plots' colors are identical
expect_equal(colors_default, colors_oceans) # Check if the colors match
})

# Test for both color and fill scales
test_that("nmfspalette scales apply both color and fill scales", {
p <- ggplot2::ggplot(mtcars, ggplot2::aes(x = wt, y = mpg, color = as.factor(cyl), fill = as.factor(cyl))) +
ggplot2::geom_point()
formatted_plot <- add_theme(p)

# Extract the scales from the plot
scales <- formatted_plot$scales$scales

# Check color scale
color_scale <- scales[[1]]
expect_true(grepl("nmfs", deparse(color_scale$call))) # Color scale from nmfspalette

# Check fill scale
fill_scale <- scales[[2]]
expect_true(grepl("nmfs", deparse(fill_scale$call))) # Fill scale from nmfspalette
})

0 comments on commit 42be843

Please sign in to comment.