88# '
99# ' @return A ggplot2 object
1010create_ae_cm_plot <- function (data , x_limits , palette , sl_info , vline_vars , vline_day_numbers ,
11- ref_date ) {
11+ x_axis_unit , x_axis_breaks , ref_date ) {
1212 # set column for title banner
1313 data [[" title_banner" ]] <- " "
1414
@@ -104,12 +104,54 @@ create_ae_cm_plot <- function(data, x_limits, palette, sl_info, vline_vars, vlin
104104 )
105105
106106 as_CDISC_days <- function (days ) days + (days > = 0 )
107- p <- p + ggplot2 :: scale_x_continuous(
108- labels = function (days ) {
107+
108+ if (x_axis_unit == CONST $ PLOT_X_AXIS_UNITS $ DAYS ) {
109+ if (length(x_axis_breaks ) == 1 ) {
110+ breaks <- base :: pretty(x_limits_z , n = x_axis_breaks )
111+ } else {
112+ breaks <- x_axis_breaks
113+ }
114+
115+ lbl_fn <- function (days ) {
109116 dates <- ref_date + days
110117 days <- as_CDISC_days(days )
111118 sprintf(" %s\n Day %s" , dates , days )
112- },
119+ }
120+ } else if (x_axis_unit == CONST $ PLOT_X_AXIS_UNITS $ WEEKS ) {
121+ if (length(x_axis_breaks ) == 1 ) {
122+ # Calculates the breaks in weeks and move them back to days, round in case we incurr in a numerical error
123+ breaks <- round(base :: pretty(x_limits_z / 7 , n = x_axis_breaks ) * 7 )
124+ # Only return values within limits otherwise the appear in the labels as NA
125+ breaks <- breaks [breaks > = x_limits_z [[1 ]] & breaks < = x_limits_z [[2 ]]]
126+ } else {
127+ breaks <- x_axis_breaks * 7
128+ }
129+
130+ lbl_fn <- function (days ) {
131+ dates <- ref_date + days
132+ days_z <- days
133+ days <- as_CDISC_days(days )
134+ labels <- vector(mode = " character" , length = length(days ))
135+
136+ for (idx in seq_along(days )) {
137+ day <- days [[idx ]]
138+ day_z <- days_z [[idx ]]
139+ date <- dates [[idx ]]
140+ if (day_z == 0 ) {
141+ labels [[idx ]] <- sprintf(" %s\n Day %s" , date , day )
142+ } else {
143+ labels [[idx ]] <- sprintf(" %s\n Week %s" , date , day_z / 7 )
144+ }
145+ }
146+ return (labels )
147+ }
148+ } else {
149+ stop(" Unknown x_axis_unit" )
150+ }
151+
152+ p <- p + ggplot2 :: scale_x_continuous(
153+ labels = lbl_fn ,
154+ breaks = breaks ,
113155 limits = x_limits_z
114156 )
115157
@@ -135,7 +177,7 @@ create_ae_cm_plot <- function(data, x_limits, palette, sl_info, vline_vars, vlin
135177# '
136178# ' @return A ggplot2 object
137179create_lb_vs_plot <- function (data , date , val , low_limit , high_limit , param , summary_stats , x_limits ,
138- palette , sl_info , vline_vars , vline_day_numbers , ref_date ) {
180+ x_axis_unit , x_axis_breaks , palette , sl_info , vline_vars , vline_day_numbers , ref_date ) {
139181 # NOTE(miguel): The following song and dance courtesy of plotly::layout not supporting dates on axes
140182 # column names that end with '_z' are days that represent ref_date as zero (unlike CDISC)
141183 data [[" date_z" ]] <- as.numeric(as.Date(data [[date ]]) - ref_date )
@@ -193,7 +235,6 @@ create_lb_vs_plot <- function(data, date, val, low_limit, high_limit, param, sum
193235 plot <- plot + ggplot2 :: scale_color_manual(name = " Legend" , values = palette )
194236 plot <- plot + ggplot2 :: scale_fill_manual(name = " Legend" , values = palette )
195237
196- as_CDISC_days <- function (days ) days + (days > = 0 )
197238
198239 # get facet plots and set formats
199240 plot <- plot + ggplot2 :: facet_wrap(ggplot2 :: vars(.data [[param ]]), ncol = 1 , scales = " free_y" ) +
@@ -203,16 +244,60 @@ create_lb_vs_plot <- function(data, date, val, low_limit, high_limit, param, sum
203244 axis.text.y = ggplot2 :: element_text(size = 7 ), # y-axis text size
204245 strip.text = ggplot2 :: element_text(size = 6 ), # title text/banner size
205246 panel.spacing.y = ggplot2 :: unit(0 , " lines" ) # distance between plots in facet_wrap
206- ) +
207- ggplot2 :: xlab(" " ) + ggplot2 :: ylab(" " ) +
208- ggplot2 :: scale_x_continuous(
209- labels = function (days ) {
210- dates <- ref_date + days
211- days <- as_CDISC_days(days )
212- sprintf(" %s\n Day %s" , dates , days )
213- },
214- limits = x_limits_z
215247 )
248+ ggplot2 :: xlab(" " ) + ggplot2 :: ylab(" " )
249+
250+ as_CDISC_days <- function (days ) days + (days > = 0 )
251+
252+ if (x_axis_unit == CONST $ PLOT_X_AXIS_UNITS $ DAYS ) {
253+ if (length(x_axis_breaks ) == 1 ) {
254+ breaks <- base :: pretty(x_limits_z , n = x_axis_breaks )
255+ } else {
256+ breaks <- x_axis_breaks
257+ }
258+
259+ lbl_fn <- function (days ) {
260+ dates <- ref_date + days
261+ days <- as_CDISC_days(days )
262+ sprintf(" %s\n Day %s" , dates , days )
263+ }
264+ } else if (x_axis_unit == CONST $ PLOT_X_AXIS_UNITS $ WEEKS ) {
265+ if (length(x_axis_breaks ) == 1 ) {
266+ # Calculates the breaks in weeks and move them back to days, round in case we incurr in a numerical error
267+ breaks <- round(base :: pretty(x_limits_z / 7 , n = x_axis_breaks ) * 7 )
268+ # Only return values within limits otherwise the appear in the labels as NA
269+ breaks <- breaks [breaks > = x_limits_z [[1 ]] & breaks < = x_limits_z [[2 ]]]
270+ } else {
271+ breaks <- x_axis_breaks * 7
272+ }
273+
274+ lbl_fn <- function (days ) {
275+ dates <- ref_date + days
276+ days_z <- days
277+ days <- as_CDISC_days(days )
278+ labels <- vector(mode = " character" , length = length(days ))
279+
280+ for (idx in seq_along(days )) {
281+ day <- days [[idx ]]
282+ day_z <- days_z [[idx ]]
283+ date <- dates [[idx ]]
284+ if (day_z == 0 ) {
285+ labels [[idx ]] <- sprintf(" %s\n Day %s" , date , day )
286+ } else {
287+ labels [[idx ]] <- sprintf(" %s\n Week %s" , date , day_z / 7 )
288+ }
289+ }
290+ return (labels )
291+ }
292+ } else {
293+ stop(" Unknown x_axis_unit" )
294+ }
295+
296+ p <- p + ggplot2 :: scale_x_continuous(
297+ labels = lbl_fn ,
298+ breaks = breaks ,
299+ limits = x_limits_z
300+ )
216301
217302 # HACK: Offset the limits of the y axis to account for label strips
218303 # TODO: Remove during transition away from ggplot2+plotly
0 commit comments