Skip to content

Commit 42af4fa

Browse files
committed
reworked the vignette and fixed a few small bugs concerning scale_date_**
1 parent 3f68021 commit 42af4fa

File tree

6 files changed

+156
-65
lines changed

6 files changed

+156
-65
lines changed

R/dint-package.R

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
dyn_register_s3_method("lubridate", "month", "date_xx")
99
dyn_register_s3_method("zoo", "as.yearqtr", "date_yq")
1010
dyn_register_s3_method("zoo", "as.yearmon", "date_ym")
11+
dyn_register_s3_method("ggplot2", "scale_type", "date_yq")
12+
dyn_register_s3_method("ggplot2", "scale_type", "date_ym")
13+
dyn_register_s3_method("ggplot2", "scale_type", "date_yw")
14+
1115

1216
# +- yq --------------------------------------------------------------------
1317
if (requireNamespace("scales", quietly = TRUE)){
@@ -62,7 +66,7 @@
6266
scales::trans_new(
6367
name = "date_yw",
6468
transform = function(x) {
65-
as.numeric(as.Date(x))
69+
as.numeric(first_of_isoweek(x)) + 3.5
6670
},
6771
inverse = function(x) {
6872
origin <- structure(0, class = c("POSIXct", "POSIXt"), tzone = "UTC")

R/first_of.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ last_of_month.default <- function(x){
215215

216216

217217
# isoweek -----------------------------------------------------------------
218-
218+
#TODO: deal with 53/52 week years?
219219

220220

221221

R/scale_date_xx.R

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ NULL
3434

3535

3636
# This tells ggplot2 what scale to look for
37+
3738
scale_type.date_yq <- function(x) "date_yq"
3839
scale_type.date_ym <- function(x) "date_ym"
3940
scale_type.date_yw <- function(x) "date_yw"
@@ -46,13 +47,15 @@ scale_type.date_yw <- function(x) "date_yw"
4647
scale_x_date_yq <- function(
4748
name = "Quarter",
4849
breaks = date_yq_breaks(),
50+
labels = ggplot2::waiver(),
4951
limits = NULL,
5052
position = "bottom"
5153
){
5254
scale_date_yq(
5355
aesthetics = c("x", "xmin", "xmax", "xend"),
5456
name = name,
5557
limits = limits,
58+
labels = labels,
5659
breaks = breaks,
5760
position = position
5861
)
@@ -66,13 +69,15 @@ scale_x_date_yq <- function(
6669
scale_y_date_yq <- function(
6770
name = "Quarter",
6871
breaks = date_yq_breaks(),
72+
labels = ggplot2::waiver(),
6973
limits = NULL,
7074
position = "left"
7175
){
7276
scale_date_yq(
7377
aesthetics = c("y", "ymin", "ymax", "yend"),
7478
name = name,
7579
limits = limits,
80+
labels = labels,
7681
breaks = breaks,
7782
position = position
7883
)
@@ -85,6 +90,7 @@ scale_date_yq <- function(
8590
aesthetics,
8691
name = "Quarter",
8792
breaks = date_yq_breaks(),
93+
labels = ggplot2::waiver(),
8894
limits = NULL,
8995
position = "bottom"
9096
){
@@ -93,6 +99,7 @@ scale_date_yq <- function(
9399
scale_name = "date_yq",
94100
name = name,
95101
palette = identity,
102+
labels = labels,
96103
guide = "none",
97104
trans = date_yq_trans,
98105
super = ggplot2::ScaleContinuousDate,
@@ -113,13 +120,15 @@ scale_date_yq <- function(
113120
scale_x_date_ym <- function(
114121
name = "Month",
115122
breaks = date_ym_breaks(),
123+
labels = ggplot2::waiver(),
116124
limits = NULL,
117125
position = "bottom"
118126
){
119127
scale_date_ym(
120128
aesthetics = c("x", "xmin", "xmax", "xend"),
121129
name = name,
122130
breaks = breaks,
131+
labels = labels,
123132
limits = limits,
124133
position = position
125134
)
@@ -133,13 +142,15 @@ scale_x_date_ym <- function(
133142
scale_y_date_ym <- function(
134143
name = "Month",
135144
breaks = date_ym_breaks(),
145+
labels = ggplot2::waiver(),
136146
limits = NULL,
137147
position = "left"
138148
) {
139149
scale_date_ym(aesthetics = c(
140150
"y", "ymin", "ymax", "yend"),
141151
name = name,
142152
breaks = breaks,
153+
labels = labels,
143154
limits = limits,
144155
position = position
145156
)
@@ -152,6 +163,7 @@ scale_date_ym <- function(
152163
aesthetics,
153164
name = "Month",
154165
breaks = date_ym_breaks(),
166+
labels = ggplot2::waiver(),
155167
limits = NULL,
156168
position = "left"
157169
){
@@ -161,6 +173,7 @@ scale_date_ym <- function(
161173
name = name,
162174
breaks = breaks,
163175
palette = identity,
176+
labels = labels,
164177
guide = "none",
165178
trans = date_ym_trans,
166179
super = ggplot2::ScaleContinuousDate,
@@ -176,15 +189,17 @@ scale_date_ym <- function(
176189
#' @rdname scale_date_xx
177190
#' @export
178191
scale_x_date_yw <- function(
179-
name = "Month",
192+
name = "Week",
180193
breaks = date_yw_breaks(),
194+
labels = ggplot2::waiver(),
181195
limits = NULL,
182196
position = "bottom"
183197
){
184198
scale_date_yw(
185199
aesthetics = c("x", "xmin", "xmax", "xend"),
186200
name = name,
187201
breaks = breaks,
202+
labels = labels,
188203
limits = limits,
189204
position = position
190205
)
@@ -196,8 +211,9 @@ scale_x_date_yw <- function(
196211
#' @rdname scale_date_xx
197212
#' @export
198213
scale_y_date_yw <- function(
199-
name = "Month",
214+
name = "Week",
200215
breaks = date_yw_breaks(),
216+
labels = ggplot2::waiver(),
201217
limits = NULL,
202218
position = "left"
203219
){
@@ -217,6 +233,7 @@ scale_date_yw <- function(
217233
aesthetics,
218234
name = "Week",
219235
breaks = date_yw_breaks(),
236+
labels = ggplot2::waiver(),
220237
limits = NULL,
221238
position = "bottom"
222239
){
@@ -227,6 +244,7 @@ scale_date_yw <- function(
227244
breaks = breaks,
228245
palette = identity,
229246
guide = "none",
247+
labels = labels,
230248
trans = date_yw_trans,
231249
super = ggplot2::ScaleContinuousDate,
232250
position = position,
@@ -407,9 +425,6 @@ date_yw_breaks <- function(
407425
breaks <- date_yw(seq(ywin, ywax, by = by), 1)
408426
}
409427

410-
# fix breaks at the corner of the plot (outside the data range)
411-
# this works well if the plot area is padded by 1 unit
412-
# (see scale_date_** expand argument)
413428
breaks <- breaks[breaks >= xmin & breaks <= xmax]
414429

415430
if (length(breaks) == 1){

man/dint-package.Rd

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

man/scale_date_xx.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)