Skip to content

Commit 5fc61fc

Browse files
authored
Updates to ggplot label access in testing ggplot labels (#24)
* Updates to ggplot label access Testing ggplot labels Fixes #23 * Other updates to pass test with ggplot4.0.0 * Revert coord_trans --> coord_transform pending ggplot release * Improve testing in changed code
1 parent f690f06 commit 5fc61fc

File tree

12 files changed

+125
-63
lines changed

12 files changed

+125
-63
lines changed

NAMESPACE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Generated by roxygen2: do not edit by hand
22

3-
S3method("-",gg)
3+
S3method("-",ggplot)
44
export(absorbance_to_transmittance)
55
export(add_band)
66
export(add_scalar_value)

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# PlotFTIR (development version)
22

3+
* Updates (mostly to testing) to comply with pending ggplot2 4.0.0 release (#23)
4+
35
# PlotFTIR 1.2.0
46

57
* Patch update to expose `check_ftir_data` for downstream packages. (related to #19)

R/io.R

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,11 +219,12 @@ read_ftir_directory <- function(path, files, sample_names = NA, ...) {
219219
}
220220
}
221221
},
222-
error = function(e)
222+
error = function(e) {
223223
cli::cli_warn(c(
224224
"{e}",
225225
i = "{.fn PlotFTIR::read_ftir_directory} will try to continue with the next file."
226226
))
227+
}
227228
)
228229
}
229230
if (nrow(ftir) > 0) {
@@ -504,7 +505,7 @@ save_plot <- function(ftir_spectra_plot, filename, ...) {
504505
))
505506
}
506507

507-
if (!ggplot2::is.ggplot(ftir_spectra_plot)) {
508+
if (!ggplot2::is_ggplot(ftir_spectra_plot)) {
508509
cli::cli_abort(
509510
"Error in {.fn PlotFTIR::save_plt}. {.arg ftir_spectra_plot} must be a ggplot object. You provided {.obj_type_friendly {ftir_spectra_plot}}."
510511
)

R/manipulations.R

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ zoom_in_on_range <- function(ftir_spectra_plot, zoom_range = c(1000, 1900)) {
4646
))
4747
}
4848

49-
if (!ggplot2::is.ggplot(ftir_spectra_plot)) {
49+
if (!ggplot2::is_ggplot(ftir_spectra_plot)) {
5050
cli::cli_abort(
5151
"Error in {.fn PlotFTIR::zoom_in_on_range}. {.arg ftir_spectra_plot} must be a ggplot object. You provided {.obj_type_friendly {ftir_spectra_plot}}."
5252
)
@@ -150,22 +150,22 @@ compress_trans <- function(intercept = 2000, ratio = 5) {
150150
#'
151151
#' @keywords internal
152152
#'
153-
#' @references From https://stackoverflow.com/a/64011534
153+
#' @references modified from https://stackoverflow.com/a/64011534
154154
#'
155155
#' @md
156-
`-.gg` <- function(plot, layer) {
156+
`-.ggplot` <- function(plot, layer = NULL) {
157157
if (is.null(layer) || missing(layer)) {
158158
cli::cli_abort(c(
159159
"Cannot use {.code -.gg()} with a single argument, it must be followed by a {.arg layer}.",
160160
i = "Did you accidentally put {.code -} on a new line?"
161161
))
162162
}
163-
if (!ggplot2::is.ggplot(plot)) {
164-
cli::cli_abort(
165-
"You need to have a ggplot on the left side. You provided {.obj_type_friendly { plot }}."
166-
)
167-
}
168-
plot$layers <- c(layer, plot$layers)
163+
164+
layers_count <- length(plot$layers)
165+
166+
plot <- plot + layer
167+
# reorganize plot layers to put the recently added one 'first' in the list - underneath everything else
168+
plot$layers <- c(plot$layers[[layers_count + 1]], plot$layers[1:layers_count])
169169
plot
170170
}
171171

@@ -228,7 +228,7 @@ compress_low_energy <- function(
228228
))
229229
}
230230

231-
if (!ggplot2::is.ggplot(ftir_spectra_plot)) {
231+
if (!ggplot2::is_ggplot(ftir_spectra_plot)) {
232232
cli::cli_abort(
233233
"Error in {.fn PlotFTIR::compress_low_energy}. {.arg ftir_spectra_plot} must be a ggplot object. You provided {.obj_type_friendly {ftir_spectra_plot}}."
234234
)
@@ -397,12 +397,32 @@ add_wavenumber_marker <- function(
397397
text <- as.character(as.integer(wavenumber))
398398
}
399399

400-
if (!ggplot2::is.ggplot(ftir_spectra_plot)) {
400+
if (!ggplot2::is_ggplot(ftir_spectra_plot)) {
401401
cli::cli_abort(
402402
"Error in {.fn PlotFTIR::add_wavenumber_marker}. {.arg ftir_spectra_plot} must be a ggplot object. You provided {.obj_type_friendly {ftir_spectra_plot}}."
403403
)
404404
}
405405

406+
if (is.null(line_aesthetics)) {
407+
line_aesthetics <- list()
408+
}
409+
410+
if (is.null(label_aesthetics)) {
411+
label_aesthetics <- list()
412+
}
413+
414+
if (!is.list(line_aesthetics)) {
415+
cli::cli_abort(
416+
"Error in {.fn PlotFTIR::add_wavenumber_marker}. {.arg line_aesthetics} must be a named list. You provided {.obj_type_friendly {line_aesthetics}}."
417+
)
418+
}
419+
420+
if (!is.list(label_aesthetics)) {
421+
cli::cli_abort(
422+
"Error in {.fn PlotFTIR::add_wavenumber_marker}. {.arg label_aesthetics} must be a named list. You provided {.obj_type_friendly {label_aesthetics}}."
423+
)
424+
}
425+
406426
data <- ftir_spectra_plot$data
407427
if (wavenumber < min(data$wavenumber) || wavenumber > max(data$wavenumber)) {
408428
cli::cli_abort(
@@ -484,7 +504,7 @@ rename_plot_sample_ids <- function(ftir_spectra_plot, sample_ids) {
484504
))
485505
}
486506

487-
if (!ggplot2::is.ggplot(ftir_spectra_plot)) {
507+
if (!ggplot2::is_ggplot(ftir_spectra_plot)) {
488508
cli::cli_abort(
489509
"Error in {.fn PlotFTIR::rename_plot_sample_ids}. {.arg ftir_spectra_plot} must be a ggplot object. You provided {.obj_type_friendly {ftir_spectra_plot}}."
490510
)
@@ -608,7 +628,7 @@ move_plot_legend <- function(
608628
i = "Install {.pkg ggplot2} with {.run install.packages('ggplot2')}"
609629
))
610630
}
611-
if (!ggplot2::is.ggplot(ftir_spectra_plot)) {
631+
if (!ggplot2::is_ggplot(ftir_spectra_plot)) {
612632
cli::cli_abort(
613633
"Error in {.fn PlotFTIR::move_plot_legend}. {.arg ftir_spectra_plot} must be a ggplot object. You provided {.obj_type_friendly {ftir_spectra_plot}}."
614634
)
@@ -708,7 +728,7 @@ highlight_sample <- function(ftir_spectra_plot, sample_ids, ...) {
708728
))
709729
}
710730

711-
if (!ggplot2::is.ggplot(ftir_spectra_plot)) {
731+
if (!ggplot2::is_ggplot(ftir_spectra_plot)) {
712732
cli::cli_abort(
713733
"Error in {.fn PlotFTIR::highlight_sample}. {.arg ftir_spectra_plot} must be a ggplot object. You provided {.obj_type_friendly {ftir_spectra_plot}}."
714734
)
@@ -803,7 +823,7 @@ add_band <- function(
803823
))
804824
}
805825

806-
if (!ggplot2::is.ggplot(ftir_spectra_plot)) {
826+
if (!ggplot2::is_ggplot(ftir_spectra_plot)) {
807827
cli::cli_abort(
808828
"Error in {.fn PlotFTIR::add_band}. {.arg ftir_spectra_plot} must be a ggplot object. You provided {.obj_type_friendly {ftir_spectra_plot}}."
809829
)

R/plot_ftir.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,9 +310,9 @@ plot_ftir_stacked <- function(
310310
suppressMessages(p <- p + ggplot2::coord_cartesian(ylim = c(0, NA)))
311311

312312
if (grepl("absorbance", mode)) {
313-
p$labels$y <- "Absorbance (a.u.)"
313+
p <- p + ggplot2::ylab("Absorbance (a.u.)")
314314
} else {
315-
p$labels$y <- "Transmittance (a.u.)"
315+
p <- p + ggplot2::ylab("Transmittance (a.u.)")
316316
}
317317

318318
attr(p, "spectra_style") <- "stacked"

R/utils.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ get_plot_sample_ids <- function(ftir_spectra_plot) {
3333
i = "Install {.pkg ggplot2} with {.run install.packages('ggplot2')}"
3434
))
3535
}
36-
if (!ggplot2::is.ggplot(ftir_spectra_plot)) {
36+
if (!ggplot2::is_ggplot(ftir_spectra_plot)) {
3737
cli::cli_abort(
3838
"Error in {.fn PlotFTIR::get_plot_sample_ids}. {.arg ftir_spectra_plot} must be a ggplot object. You provided {.obj_type_friendly {ftir_spectra_plot}}."
3939
)

R/zzz.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
options("PlotFTIR.lang" = "en")
1010
packageStartupMessage(
1111
'Plotting spectra with PlotFTIR. Please cite if plots are used in publishing (`citation("plotFTIR")`).\n',
12-
'PlotFTIR is set to English as default. Changer au fran\u00e7ais par la fonction `options("PlotFTIR.lang" = "en")`'
12+
'PlotFTIR is set to English as default. Changer au fran\u00e7ais par la fonction `options("PlotFTIR.lang" = "fr")`'
1313
)
1414
} else {
1515
if (

cran-comments.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,4 @@
22

33
0 errors | 0 warnings | 0 notes
44

5-
* This is a minor version release with improvements
6-
* Exposes a function for downstream package(s) use.
7-
* Adds import for .jdx filetype.
8-
5+
* This is a patch release with updates to test code to resolve upstream changes in ggplot2 4.0.0.

man/dot-gg.Rd renamed to man/dot-ggplot.Rd

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-io.R

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -307,10 +307,12 @@ test_that("reading .jdx works", {
307307
)
308308
# 2D NMR Data
309309
expect_error(
310-
read_ftir(
311-
path = system.file("extdata", "isasspc1.dx", package = "readJDX")
312-
),
313-
"Could not confirm `infrared` data file."
310+
suppressWarnings(
311+
read_ftir(
312+
path = system.file("extdata", "isasspc1.dx", package = "readJDX")
313+
),
314+
"Could not confirm `infrared` data file."
315+
)
314316
)
315317
})
316318

0 commit comments

Comments
 (0)