@@ -3,7 +3,7 @@ context("cookbook axes")
33bp <- ggplot(PlantGrowth , aes(x = group , y = weight )) +
44 geom_boxplot()
55
6- expect_traces <- function (gg , n.traces , name ){
6+ expect_traces <- function (gg , n.traces , name ) {
77 stopifnot(is.ggplot(gg ))
88 stopifnot(is.numeric(n.traces ))
99 save_outputs(gg , paste0(" cookbook-axes-" , name ))
@@ -17,13 +17,13 @@ expect_traces <- function(gg, n.traces, name){
1717 list (traces = has.data , layout = L $ layout )
1818}
1919
20- get_legend <- function (L ){
21- if (! isTRUE(L $ kwargs $ layout $ showlegend )){
20+ get_legend <- function (L ) {
21+ if (! isTRUE(L $ kwargs $ layout $ showlegend )) {
2222 return (data.frame ())
2323 }
2424 legend.list <- list ()
25- for (tr in L $ traces ){
26- if (is.character(tr $ name )){
25+ for (tr in L $ traces ) {
26+ if (is.character(tr $ name )) {
2727 legend.list [[tr $ name ]] <-
2828 data.frame (name = tr $ name , showlegend = tr $ showlegend )
2929 }
@@ -32,12 +32,12 @@ get_legend <- function(L){
3232 subset(legend.df , showlegend )
3333}
3434
35- leg <- function (... ){
35+ leg <- function (... ) {
3636 name <- c(... )
3737 data.frame (name )
3838}
3939
40- expect_legend <- function (L , expected ){
40+ expect_legend <- function (L , expected ) {
4141 stopifnot(is.data.frame(expected ))
4242 shown <- get_legend(L )
4343 expect_identical(shown $ name , expected $ name )
@@ -59,7 +59,7 @@ test_that("factor levels determine tick order", {
5959 c(" trt2" , " trt1" , " ctrl" ))
6060 expect_legend(info , leg())
6161})
62-
62+
6363# # These two do the same thing; all data points outside the graphing
6464# # range are dropped, resulting in a misleading box plot.
6565bp.ylim.hide <- bp + ylim(5 , 7.5 )
@@ -74,7 +74,7 @@ test_that("scale_y(limits) hides points", {
7474 expect_legend(info , leg())
7575 expect_equal(info $ layout $ yaxis $ range , c(5 , 7.5 ))
7676})
77-
77+
7878bp.coord <- bp + coord_cartesian(ylim = c(5 , 7.5 ))
7979test_that(" Using coord_cartesian zooms into the area" , {
8080 info <- expect_traces(bp.coord , 3 , " coord-ylim" )
@@ -93,11 +93,12 @@ sp <- ggplot(dat, aes(xval, yval)) + geom_point()
9393
9494test_that(" A scatterplot with regular (linear) axis scaling" , {
9595 info <- expect_traces(sp , 1 , " linear-axes" )
96- # # TODO: why does this test take so long?
96+ # TODO: why does this test take so long?
9797 expect_legend(info , leg())
9898})
9999
100- library(scales ) # Need the scales package
100+ library(scales )
101+ # TODO: Add package "scales" to the list of dependencies?
101102sp.log2.scale <- sp + scale_y_continuous(trans = log2_trans())
102103
103104test_that(" log2 scaling of the y axis (with visually-equal spacing)" , {
@@ -214,12 +215,12 @@ test_that("In this particular case, x scale has no effect", {
214215
215216# Self-defined formatting function for times.
216217timeHMS_formatter <- function (x ) {
217- h <- floor(x / 60 )
218- m <- floor(x %% 60 )
219- s <- round(60 * (x %% 1 )) # Round to nearest second
220- lab <- sprintf(" %02d:%02d:%02d" , h , m , s ) # Format the strings as HH:MM:SS
221- lab <- gsub(" ^00:" , " " , lab ) # Remove leading 00: if present
222- lab <- gsub(" ^0" , " " , lab ) # Remove leading 0 if present
218+ h <- floor(x / 60 )
219+ m <- floor(x %% 60 )
220+ s <- round(60 * (x %% 1 )) # Round to nearest second
221+ lab <- sprintf(" %02d:%02d:%02d" , h , m , s ) # Format the strings as HH:MM:SS
222+ lab <- gsub(" ^00:" , " " , lab ) # Remove leading 00: if present
223+ lab <- gsub(" ^0" , " " , lab ) # Remove leading 0 if present
223224}
224225
225226custom.formatter <- bp + scale_y_continuous(label = timeHMS_formatter )
@@ -263,4 +264,3 @@ test_that("Hide all the vertical gridlines", {
263264 info <- expect_traces(blank.y , 3 , " blank-y" )
264265 expect_legend(info , leg())
265266})
266-
0 commit comments