Skip to content

Commit

Permalink
Merge pull request #72 from MattCowgill/vdiffr
Browse files Browse the repository at this point in the history
added unit tests, including with vdiffr
  • Loading branch information
MattCowgill authored Jul 28, 2019
2 parents 7669353 + c26341e commit 3384dcf
Show file tree
Hide file tree
Showing 13 changed files with 219 additions and 35 deletions.
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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 = "[email protected]",
role = c("aut", "cre")),
Expand All @@ -28,4 +28,5 @@ LazyData: true
RoxygenNote: 6.1.1
Suggests:
testthat (>= 2.1.0),
covr
covr,
vdiffr
8 changes: 4 additions & 4 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Binary file modified R/sysdata.rda
Binary file not shown.
6 changes: 6 additions & 0 deletions R/theme_grattan.R
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
10 changes: 7 additions & 3 deletions R/wrap_labs.R
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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) {
Expand All @@ -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 ")

}
Expand Down
20 changes: 10 additions & 10 deletions data-raw/create sysdata.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
1 change: 1 addition & 0 deletions man/stitch_pdfs.Rd

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

3 changes: 3 additions & 0 deletions tests/figs/deps.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- vdiffr-svg-engine: 1.0
- vdiffr: 0.3.1
- freetypeharfbuzz: 0.2.5
84 changes: 84 additions & 0 deletions tests/figs/vdiffr-tests/normal-plot.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 13 additions & 13 deletions tests/testthat/test-grattan_save.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})

Expand Down
19 changes: 16 additions & 3 deletions tests/testthat/test-theme_grattan.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Loading

0 comments on commit 3384dcf

Please sign in to comment.