Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions R/annotate_gg_km.R
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,24 @@ annotate_riskdf <- function(gg_plt, fit_km, title = "Patients at Risk:",
) +
ggplot2::coord_cartesian(clip = "off", ylim = c(0.5, nrow(at_risk_tbl)))

# 1. Get the exact x-range from the top plot (e.g. 0-1200 range)
top_range <- layer_scales(gg_plt)$x$range$range
top_breaks <- layer_scales(gg_plt)$x$break_positions()

# 2. Force the bottom plot (table) to use the SAME range and breaks
# This ensures 0 on the top is exactly above 0 on the bottom
gg_at_risk <- gg_at_risk +
scale_x_continuous(
limits = top_range,
breaks = top_breaks
)

# 3. Force the top plot to also have no expansion so they match perfectly
gg_plt <- gg_plt + scale_x_continuous(limits = top_range)

gg_plt <- cowplot::plot_grid(
gg_plt, gg_at_risk,
align = "vh", axis = "b", ncol = 1,
align = "v", axis = "rl", ncol = 1,
rel_heights = c(rel_height_plot, 1 - rel_height_plot)
)
gg_plt
Expand Down Expand Up @@ -203,7 +218,7 @@ annotate_surv_med <- function(gg_plt, fit_km, ...) {
ggplot2::coord_cartesian(clip = "off", ylim = c(0.5, nrow(surv_med_tbl) + 1.5))
gg_surv_med <- suppressMessages(
gg_surv_med +
ggplot2::scale_x_continuous(expand = c(0.025, 0)) +
ggplot2::scale_x_continuous() +
ggplot2::scale_y_continuous(labels = rev(rownames(surv_med_tbl)), breaks = seq_len(nrow(surv_med_tbl)))
)

Expand Down Expand Up @@ -269,7 +284,7 @@ annotate_coxph <- function(gg_plt, coxph_tbl, ...) {
ggplot2::coord_cartesian(clip = "off", ylim = c(0.5, nrow(coxph_tbl) + 1.5))
gg_coxph <- suppressMessages(
gg_coxph +
ggplot2::scale_x_continuous(expand = c(0.025, 0)) +
ggplot2::scale_x_continuous() +
ggplot2::scale_y_continuous(labels = rev(rownames(coxph_tbl)), breaks = seq_len(nrow(coxph_tbl)))
)

Expand Down
4 changes: 2 additions & 2 deletions R/forest.R
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ gg_forest_plot <- function(data,
ci_upper_log = .data$ci_upper
)
x_aesthetic_vars <- aes(x = .data$estimate, xend = .data$ci_lower_log, yend = .data$ci_upper_log)
x_scale <- scale_x_log10(expand = c(0.01, 0))
x_scale <- scale_x_log10()
}

# Create plot
Expand Down Expand Up @@ -140,7 +140,7 @@ gg_forest_plot <- function(data,
coord_cartesian(xlim = xlim) +
scale_y_continuous(
limits = c(0, nrow(data) + 2.5), breaks = data$y_pos,
labels = rep("", length(data$estimate)), expand = c(0, 0)
labels = rep("", length(data$estimate))
) +
theme_minimal() +
theme(
Expand Down
10 changes: 5 additions & 5 deletions R/gg_km.R
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ gg_km <- function(surv_plot_data,
)
) +
ggplot2::theme_bw(base_size = font_size) +
ggplot2::scale_y_continuous(limits = ylim, expand = c(0.025, 0)) +
ggplot2::scale_y_continuous(limits = ylim) +
ggplot2::labs(x = "Days", y = paste(yval, "Probability")) +
ggplot2::theme(
axis.text = ggplot2::element_text(size = font_size),
Expand All @@ -236,18 +236,18 @@ gg_km <- function(surv_plot_data,

if (!is.null(max_time) && !is.null(xticks)) {
gg_plt <- gg_plt + ggplot2::scale_x_continuous(
breaks = xticks, limits = c(min(0, xticks), max(c(xticks, max_time))), expand = c(0.025, 0)
breaks = xticks, limits = c(min(0, xticks), max(c(xticks, max_time)))
)
} else if (!is.null(xticks)) {
if (max(data$time) <= max(xticks)) {
gg_plt <- gg_plt + ggplot2::scale_x_continuous(
breaks = xticks, limits = c(min(0, min(xticks)), max(xticks)), expand = c(0.025, 0)
breaks = xticks, limits = c(min(0, min(xticks)), max(xticks))
)
} else {
gg_plt <- gg_plt + ggplot2::scale_x_continuous(breaks = xticks, expand = c(0.025, 0))
gg_plt <- gg_plt + ggplot2::scale_x_continuous(breaks = xticks)
}
} else if (!is.null(max_time)) {
gg_plt <- gg_plt + ggplot2::scale_x_continuous(limits = c(0, max_time), expand = c(0.025, 0))
gg_plt <- gg_plt + ggplot2::scale_x_continuous(limits = c(0, max_time))
}

if (!is.null(legend_pos)) {
Expand Down
Loading