@@ -11,7 +11,7 @@ test_that("default position is translated to barmode=stack", {
1111 expect_identical(L $ kwargs $ layout $ xaxis $ type , " category" )
1212 expect_identical(L [[1 ]]$ type , " histogram" )
1313 expect_true(L [[1 ]]$ x [1 ] %in% c(" CDN" , " MEX" , " USA" ))
14-
14+
1515 save_outputs(hist , " histogram-barmodestack" )
1616})
1717
@@ -27,7 +27,7 @@ test_that("binwidth is translated into xbins.size", {
2727 L <- gg2list(hist )
2828 expect_equal(length(L ), 2 )
2929 expect_equal(L [[1 ]]$ xbins $ size , bw )
30-
30+
3131 save_outputs(hist , " histogram-binwidth" )
3232})
3333
@@ -44,6 +44,135 @@ test_that("dates work well with histograms", {
4444 expect_identical(L $ kwargs $ layout $ xaxis $ type , " date" )
4545 expect_identical(L [[1 ]]$ x [1 ], " 2012-01-01 00:00:00" )
4646 expect_identical(L [[1 ]]$ x [2 ], " 2012-02-01 00:00:00" )
47-
47+
4848 save_outputs(hist , " histogram-dates" )
4949})
50+
51+ # Non-numeric (date) data, specifying binwidth
52+ killed <- data.frame (date = c(" 2014-12-24" ,
53+ " 2014-12-23" ,
54+ " 2014-12-22" ,
55+ " 2014-12-22" ,
56+ " 2014-12-22" ,
57+ " 2014-12-18" ,
58+ " 2014-12-22" ,
59+ " 2014-12-21" ,
60+ " 2014-12-21" ,
61+ " 2014-12-21" ,
62+ " 2014-12-20" ,
63+ " 2014-12-19" ,
64+ " 2014-12-18" ,
65+ " 2014-12-18" ,
66+ " 2014-12-17" ,
67+ " 2014-12-17" ,
68+ " 2013-12-20" ,
69+ " 2014-04-25" ,
70+ " 2014-12-01" ,
71+ " 2014-12-17" ,
72+ " 2014-12-17" ,
73+ " 2014-12-17" ,
74+ " 2014-12-17" ,
75+ " 2014-12-17" ,
76+ " 2014-12-17" ,
77+ " 2014-12-15" ,
78+ " 2014-12-15" ,
79+ " 2014-12-15" ,
80+ " 2014-12-14" ,
81+ " 2014-12-14" ,
82+ " 2014-12-14" ,
83+ " 2014-12-13" ,
84+ " 2014-12-13" ,
85+ " 2013-05-18" ,
86+ " 2014-12-13" ,
87+ " 2014-12-12" ,
88+ " 2014-12-12" ,
89+ " 2014-12-11" ,
90+ " 2014-12-10" ,
91+ " 2014-12-10" ,
92+ " 2014-12-10" ,
93+ " 2014-12-10" ,
94+ " 2014-12-09" ,
95+ " 2014-12-09" ,
96+ " 2014-12-09" ,
97+ " 2014-12-09" ,
98+ " 2014-12-08" ,
99+ " 2014-12-08" ,
100+ " 2014-12-08" ,
101+ " 2014-12-07" ,
102+ " 2014-12-08" ,
103+ " 2014-12-07" ,
104+ " 2014-05-01" ,
105+ " 2014-12-05" ,
106+ " 2014-12-05" ,
107+ " 2014-12-05" ,
108+ " 2014-12-04" ,
109+ " 2014-12-04" ,
110+ " 2014-12-04" ,
111+ " 2014-07-13" ,
112+ " 2014-12-02" ,
113+ " 2014-12-03" ,
114+ " 2014-12-03" ,
115+ " 2014-12-02" ,
116+ " 2014-12-02" ,
117+ " 2014-12-01" ,
118+ " 2014-12-01" ,
119+ " 2014-12-01" ,
120+ " 2014-04-02" ,
121+ " 2014-11-30" ,
122+ " 2014-11-30" ,
123+ " 2014-11-29" ,
124+ " 2014-11-28" ,
125+ " 2014-11-29" ,
126+ " 2014-11-27" ,
127+ " 2014-11-28" ,
128+ " 2014-11-27" ,
129+ " 2014-11-26" ,
130+ " 2014-11-25" ,
131+ " 2014-11-26" ,
132+ " 2014-11-25" ,
133+ " 2014-11-25" ,
134+ " 2014-11-24" ,
135+ " 2014-11-24" ,
136+ " 2014-11-23" ,
137+ " 2014-11-23" ,
138+ " 2014-11-24" ,
139+ " 2014-11-23" ,
140+ " 2014-11-22" ,
141+ " 2014-11-23" ,
142+ " 2014-11-22" ,
143+ " 2014-11-22" ,
144+ " 2014-11-21" ,
145+ " 2014-11-21" ,
146+ " 2014-11-21" ,
147+ " 2014-11-20" ,
148+ " 2014-11-20" ,
149+ " 2014-11-20" ,
150+ " 2014-11-19" ))
151+
152+ test_that(" datetime binning for class POSIXt works in histograms" , {
153+ kP <- killed
154+ kP $ date <- as.POSIXlt(kP $ date )
155+ histP <- ggplot(kP , aes(x = date )) + geom_histogram(binwidth = 2592000 )
156+
157+ L <- gg2list(histP )
158+ expect_equal(length(L ), 2 ) # 1 trace + layout
159+ expect_false(L [[1 ]]$ autobinx ) # No auto-binning
160+ expect_identical(L $ kwargs $ layout $ xaxis $ type , " date" )
161+ expect_equal(L [[1 ]]$ xbins $ size , 2592000000 ) # Bin size in ms
162+
163+ save_outputs(hist , " histogram-POSIXt-bins" )
164+ })
165+
166+ test_that(" datetime binning for class Date works in histograms" , {
167+ kD <- killed
168+ kD $ date <- as.Date(kD $ date )
169+ histD <- ggplot(kD , aes(x = date )) + geom_histogram(binwidth = 30 )
170+
171+ L <- gg2list(histD )
172+ expect_equal(length(L ), 2 ) # 1 trace + layout
173+ expect_false(L [[1 ]]$ autobinx ) # No auto-binning
174+ expect_identical(L $ kwargs $ layout $ xaxis $ type , " date" )
175+ expect_equal(L [[1 ]]$ xbins $ size , 2.592e+09 ) # Number of ms in 30 days
176+
177+ save_outputs(hist , " histogram-Date-bins" )
178+ })
0 commit comments