diff --git a/DESCRIPTION b/DESCRIPTION index ac665c71..a078fcc3 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: grattantheme Type: Package Title: Create Graphs in the Grattan Institute Style -Version: 0.4.0 +Version: 0.4.0.900 Authors@R: c( person("Matt", "Cowgill", email = "matthew.cowgill@grattaninstitute.edu.au", role = c("aut", "cre")), @@ -28,4 +28,5 @@ LazyData: true RoxygenNote: 6.1.1 Suggests: testthat (>= 2.1.0), - covr + covr, + vdiffr diff --git a/NEWS.md b/NEWS.md index 41b51957..eb1e6d62 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,17 +1,17 @@ +# grattantheme 0.4.0.900 +* Minor visual fixes to fullslide charts +* More testing of functions to ensure style guide-compatibility + # grattantheme 0.4.0 * New "blog" type available with grattan_save() * Users can now manually specify height of figures, overriding defaults * New palettes options added * Left and white plot margins reduced * stitch_pdfs() function is now deprecated - -# grattantheme 0.3.1.901 * grattan_save() now accepts additional arguments ... to pass to ggsave() * Warnings about labels are no longer displayed when using grattan_save(type = "all") * fullslide charts are now quicker to save, and don't print chart elements * Vertical alignment of subtitles in fullslide charts fixed, to ensure appropriate whitespace between subtitle and plot - -# grattantheme 0.3.1.900 * make_presentation() now includes the chart title, subtitle, and caption in .pptx notes * Grattan-ish Word template now included for use with redoc/Rmarkdown * fixes bug in create_fullslide() introduced with ggplot 3.2.0 diff --git a/R/sysdata.rda b/R/sysdata.rda index a06ada9c..fe51e24d 100644 Binary files a/R/sysdata.rda and b/R/sysdata.rda differ diff --git a/R/theme_grattan.R b/R/theme_grattan.R index 548ff338..6b7d56f9 100644 --- a/R/theme_grattan.R +++ b/R/theme_grattan.R @@ -166,6 +166,12 @@ theme_grattan <- function(base_size = 18, colour = grattantheme::grattan_grey_title, face = "bold"), plot.subtitle = element_text(colour = grattantheme::grattan_grey_title, + vjust = 1, + margin = margin(t = 0, + r = 0, + b = base_size * .75, + l = 0, + unit = "pt"), hjust = 0), plot.caption = element_text(family = base_family, size = rel(0.555), diff --git a/R/wrap_labs.R b/R/wrap_labs.R index 86de630d..27f6da46 100644 --- a/R/wrap_labs.R +++ b/R/wrap_labs.R @@ -4,6 +4,8 @@ wrap_labs <- function(object, type){ p <- object + chart_class <- chart_types$class[chart_types$type == type] + # widths in characters char_width_grattan_title <- chart_types$title[chart_types$type == type] char_width_grattan_subtitle <- chart_types$subtitle[chart_types$type == type] @@ -20,9 +22,10 @@ wrap_labs <- function(object, type){ # add line break to title where necessary if(nchar(stored_title) <= char_width_grattan_title & - type %in% c("fullslide", "fullslide169")){ + chart_class == "fullslide" & + !grepl("\n", stored_title)){ - stored_title <- paste0("\n ", stored_title) + stored_title <- paste0("\n", stored_title) } if(nchar(stored_title) > 2 * char_width_grattan_title) { @@ -49,7 +52,8 @@ wrap_labs <- function(object, type){ # add line break to subtitle where necessary if(nchar(stored_subtitle) <= char_width_grattan_subtitle & - type %in% c("fullslide", "fullslide_169")){ + chart_class == "fullslide" & + !grepl("\n", stored_subtitle)){ stored_subtitle <- paste0(stored_subtitle, "\n ") } diff --git a/data-raw/create sysdata.R b/data-raw/create sysdata.R index 88d3b482..d42eb0bc 100644 --- a/data-raw/create sysdata.R +++ b/data-raw/create sysdata.R @@ -3,16 +3,16 @@ library(devtools) load("data-raw/logogrob.Rda") chart_types <- tibble::tribble( - ~type, ~width, ~height, ~caption, ~title, ~subtitle, - "normal", 22.16, 14.5, 120, 70, 75, - "normal_169", 30.00, 14.5, 180, 95, 100, - "tiny", 22.16, 11.08, 120, 70, 75, - "wholecolumn", 22.16, 22.16, 120, 70, 75, - "fullpage", 44.32, 22.16, 240, 140, 150, - "fullslide", 25.4, 19.05, 140, 55, 70, - "fullslide_169", 33.87, 19.05, 180, 55, 95, - "fullslide_44", 25.4, 25.4, 140, 50, 95, - "blog", 25.4, 19.05, 140, 50, 70) + ~type, ~width, ~height, ~caption, ~title, ~subtitle, ~class, + "normal", 22.16, 14.5, 120, 70, 75, "normal", + "normal_169", 30.00, 14.5, 180, 95, 100, "normal", + "tiny", 22.16, 11.08, 120, 70, 75, "normal", + "wholecolumn", 22.16, 22.16, 120, 70, 75, "normal", + "fullpage", 44.32, 22.16, 240, 140, 150, "normal", + "fullslide", 25.4, 19.05, 140, 55, 70, "fullslide", + "fullslide_169", 33.87, 19.05, 180, 55, 95, "fullslide", + "fullslide_44", 25.4, 25.4, 140, 50, 95, "fullslide", + "blog", 25.4, 19.05, 140, 50, 70, "fullslide") use_data(logogrob, chart_types, internal = TRUE, overwrite = TRUE) diff --git a/man/stitch_pdfs.Rd b/man/stitch_pdfs.Rd index 37feaeef..743e95d1 100644 --- a/man/stitch_pdfs.Rd +++ b/man/stitch_pdfs.Rd @@ -17,6 +17,7 @@ to be created, containing the stiched-together PDFs} Combine multiple PDFs into a single PDF } \examples{ +# Deprecated function, will be removed in future versions. # stitch_pdfs() is useful when you have created multiple charts using # grattan_save() and wish to combine them into one document diff --git a/tests/figs/deps.txt b/tests/figs/deps.txt new file mode 100644 index 00000000..0f64e23e --- /dev/null +++ b/tests/figs/deps.txt @@ -0,0 +1,3 @@ +- vdiffr-svg-engine: 1.0 +- vdiffr: 0.3.1 +- freetypeharfbuzz: 0.2.5 diff --git a/tests/figs/vdiffr-tests/normal-plot.svg b/tests/figs/vdiffr-tests/normal-plot.svg new file mode 100644 index 00000000..14a76120 --- /dev/null +++ b/tests/figs/vdiffr-tests/normal-plot.svg @@ -0,0 +1,84 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +10 +15 +20 +25 +30 +35 + + + + + +2 +3 +4 +5 +wt +Either put units here or jam an elaborate thing here that describes both axes, whatevs +Here goes a Grattan title, blah blah lots of words go here extremely orange +Notes: Blah Source: somewhere + diff --git a/tests/testthat/test-grattan_save.R b/tests/testthat/test-grattan_save.R index 961db74f..684e1206 100644 --- a/tests/testthat/test-grattan_save.R +++ b/tests/testthat/test-grattan_save.R @@ -18,22 +18,22 @@ test_plot_nolabs <- ggplot(mtcars, aes(x = wt, y = mpg, col = factor(cyl))) + test_that("grattan_save() saves charts", { - grattan_save(filename = "../figs/test/test_plot.png", + grattan_save(filename = "../figs/grattan_save/test_plot.png", object = test_plot, type = "all") - expect_true(file.exists("../figs/test/test_plot/test_plot_fullslide_44.png")) - expect_true(file.exists("../figs/test/test_plot/test_plot_fullslide_169.png")) - expect_true(file.exists("../figs/test/test_plot/test_plot_fullslide.png")) - expect_true(file.exists("../figs/test/test_plot/test_plot_fullpage.png")) - expect_true(file.exists("../figs/test/test_plot/test_plot_normal_169.png")) - expect_true(file.exists("../figs/test/test_plot/test_plot_normal.png")) - expect_true(file.exists("../figs/test/test_plot/test_plot_tiny.png")) - expect_true(file.exists("../figs/test/test_plot/test_plot_wholecolumn.png")) - expect_true(file.exists("../figs/test/test_plot/test_plot_blog.png")) - expect_true(file.exists("../figs/test/test_plot/test_plot.csv")) - - unlink("../figs", recursive = TRUE) + expect_true(file.exists("../figs/grattan_save/test_plot/test_plot_fullslide_44.png")) + expect_true(file.exists("../figs/grattan_save/test_plot/test_plot_fullslide_169.png")) + expect_true(file.exists("../figs/grattan_save/test_plot/test_plot_fullslide.png")) + expect_true(file.exists("../figs/grattan_save/test_plot/test_plot_fullpage.png")) + expect_true(file.exists("../figs/grattan_save/test_plot/test_plot_normal_169.png")) + expect_true(file.exists("../figs/grattan_save/test_plot/test_plot_normal.png")) + expect_true(file.exists("../figs/grattan_save/test_plot/test_plot_tiny.png")) + expect_true(file.exists("../figs/grattan_save/test_plot/test_plot_wholecolumn.png")) + expect_true(file.exists("../figs/grattan_save/test_plot/test_plot_blog.png")) + expect_true(file.exists("../figs/grattan_save/test_plot/test_plot.csv")) + + unlink("../figs/grattan_save", recursive = TRUE) unlink("../testthat/Rplots.pdf") }) diff --git a/tests/testthat/test-theme_grattan.R b/tests/testthat/test-theme_grattan.R index 5b4291ac..aae6dc6c 100644 --- a/tests/testthat/test-theme_grattan.R +++ b/tests/testthat/test-theme_grattan.R @@ -44,15 +44,28 @@ test_that("grattan colour functions work as expected", { expect_length(grattan_pal(), 5) + expect_length(grattan_pal(n = 1), 1) expect_length(grattan_pal(n = 2), 2) - - expect_length(grattan_pal(n = 10), 10) + expect_length(grattan_pal(n = 3), 3) + expect_length(grattan_pal(n = 4), 4) + expect_length(grattan_pal(n = 5), 5) + expect_length(grattan_pal(n = 6), 6) + expect_length(suppressWarnings(grattan_pal(n = 7)), 7) + expect_length(suppressWarnings(grattan_pal(n = 8)), 8) + expect_length(suppressWarnings(grattan_pal(n = 9)), 9) + expect_length(suppressWarnings(grattan_pal(n = 10)), 10) + expect_length(grattan_pal(n = 3, reverse = TRUE), 3) expect_warning(grattan_pal(n = 10)) expect_error(grattan_pal(n = 11)) - plot_w_col <- base_plot + + expect_equal(grattan_pal(n = 5), + c(grattantheme::grattan_yellow, grattantheme::grattan_lightorange, + grattantheme::grattan_darkorange, grattantheme::grattan_red, + grattantheme::grattan_darkred)) + + plot_w_col <- base_plot + geom_point(aes(col = factor(cyl))) + grattan_colour_manual(n = 3) diff --git a/tests/testthat/test-with-vdiffr.R b/tests/testthat/test-with-vdiffr.R new file mode 100644 index 00000000..1830e32c --- /dev/null +++ b/tests/testthat/test-with-vdiffr.R @@ -0,0 +1,31 @@ + +context("vdiffr-tests") + +normal_plot <- mtcars %>% + ggplot(aes(x = wt, + y = mpg)) + + geom_point() + + grattantheme::theme_grattan() + + labs(title = "Here goes a Grattan title, blah blah lots of words go here extremely orange", + subtitle = "Either put units here or jam an elaborate thing here that describes both axes, whatevs", + caption = "Notes: Blah Source: somewhere") + +test_that("normal plot looks correct", { + + vdiffr::expect_doppelganger("normal plot", normal_plot) + +}) + +# test_that("fullslide plot looks correct", { +# +# fullslide_plot <- grattantheme:::create_fullslide(normal_plot, +# type = "fullslide", +# height = NULL, +# warn_labs = FALSE) %>% +# gridExtra::grid.arrange() %>% +# ggplotify::as.ggplot() +# +# +# vdiffr::expect_doppelganger("fullslide plot", fullslide_plot) +# }) +# diff --git a/tests/testthat/test-wrap_labs.R b/tests/testthat/test-wrap_labs.R new file mode 100644 index 00000000..2c71aef5 --- /dev/null +++ b/tests/testthat/test-wrap_labs.R @@ -0,0 +1,41 @@ + +base_plot <- ggplot(mtcars, aes(x = mpg, y = wt)) + + geom_point() + +test_that("long title fails", { + p <- base_plot + + labs(title = "This is a really long title that should fail wrap_labs with an error because it flows onto more than two lines, so it should definitely fail blah blah blah") + + expect_error(wrap_labs(p, type = "fullslide")) + + p <- base_plot + + labs(title = "Regular title", + subtitle = "Extremely long subtitle that should fail wrap_labs with an error, it'll take up too many lines even for a monster chart etc etc etc etc lorem ipsum lorem ipsum") + + expect_error(wrap_labs(p, type = "fullslide")) + +}) + + +test_that("two line title wraps", { + + p <- base_plot + + labs(title = "This is a slightly long title that should wrap onto two lines but work blah blah blah") + + p <- wrap_labs(p, type = "fullslide") + + expect_true(grepl("\n", p$labels$title)) + +}) + +test_that("caption wraps onto two lines", { + + p <- base_plot + + labs(caption = "Notes: notes go here. Source: source goes here.") + + p <- wrap_labs(p, type = "fullslide") + + expect_true(grepl("\n", p$labels$caption)) + +}) +