I want to add an option (such as if showparamvalues=TRUE) to add to the center of each…
- User
I want to add an option (such as if show_param_values=TRUE) to add to the center of each section, above the line the value: combined_plot <- ggplot(coefficient_dataalpha_df, aes(x = Time)) + geom_step(aes(y = alpha), color = '#3f007d', linewidth = 1) + geom_step(data = coefficient_databeta_df, aes(y = beta + 4), color = '#fe9929', linewidth = 1) + scale_y_continuous( name = "new dg(1/h)\nlog2", sec.axis = sec_axis(~. + 4, name = "tx(RNA/h)\nlog2") ) + theme_classic(base_size = 15) + theme(strip.text = element_blank(), strip.background = element_blank(), axis.title.y = element_text(color = '#fe9929'), axis.title.y.right = element_text(color = '#3f007d') ) + xlim(0, 10) + facet_grid(~gene_name) + geom_hline(aes(yintercept = -Inf)) + geom_vline(aes(xintercept = -Inf)) + coord_cartesian(clip = "off")
old_beta_plot <- ggplot(coefficient_data$old_beta_df, aes(Time, beta)) + geom_step(color = '#fe9929', linewidth = 1) + facet_grid(~gene_name) + xlab("") + ylab("old dg(1/h)\nlog2") + theme_classic(base_size = 15) + theme(strip.text = element_blank(), strip.background = element_blank()) + xlim(0, 10) + geom_hline(aes(yintercept = -Inf)) + geom_vline(aes(xintercept = -Inf)) + coord_cartesian(clip = "off")
- Assistant
Got it 👍 You want to optionally add the parameter values as labels centered in each flat section of your step functions, above the line. That means:
For each step segment in alpha_df, beta_df, and old_beta_df, compute the midpoint of the x-interval.
Place the corresponding y-value slightly above the line.
Only do this if show_param_values = TRUE.
Here’s one way to do it: rCopyEditadd_step_labels <- function(df, value_col, offset = 0.2) { df |> dplyr275b:bbe4:2b33:f5c6:649:e851:20ee:dbfcmutate( x_mid = (Time + dplyr275b:bbe4:2b33:f5c6:649:e851:20ee:dbfclead(Time, default = max(Time))) / 2, y_pos = !!rlang275b:bbe4:2b33:f5c6:649:e851:20ee:dbfcsym(value_col) + offset, label = round(!!rlang275b:bbe4:2b33:f5c6:649:e851:20ee:dbfcsym(value_col), 2) ) }
Then in your plotting code: rCopyEditshow_param_values <- TRUE # toggle here
alpha_labels <- add_step_labels(coefficient_databeta_df, "beta", offset = 0.2) old_beta_labels <- add_step_labels(coefficient_data$old_beta_df, "beta", offset = 0.2)
combined_plot <- ggplot(coefficient_dataalpha_df, aes(x = Time)) + geom_step(aes(y = alpha), color = '#3f007d', linewidth = 1) + geom_step(data = coefficient_databeta_df, aes(y = beta + 4), color = '#fe9929', linewidth = 1) + scale_y_continuous( name = "new dg(1/h)\nlog2", sec.axis = sec_axis(~. + 4, name = "tx(RNA/h)\nlog2") ) + theme_classic(base_size = 15) + theme(strip.text = element_blank(), strip.background = element_blank(), axis.title.y = element_text(color = '#fe9929'), axis.title.y.right = element_text(color = '#3f007d') ) + xlim(0, 10) + facet_grid(~gene_name) + geom_hline(aes(yintercept = -Inf)) + geom_vline(aes(xintercept = -Inf)) + coord_cartesian(clip = "off") + if (show_param_values) { list( geom_text(data = alpha_labels, aes(x = x_mid, y = y_pos, label = label), inherit.aes = FALSE, color = '#3f007d', size = 3), geom_text(data = beta_labels, aes(x = x_mid, y = y_pos + 4, label = label), inherit.aes = FALSE, color = '#fe9929', size = 3) ) }
old_beta_plot <- ggplot(coefficient_data$old_beta_df, aes(Time, beta)) + geom_step(color = '#fe9929', linewidth = 1) + facet_grid(~gene_name) + xlab("") + ylab("old dg(1/h)\nlog2") + theme_classic(base_size = 15) + theme(strip.text = element_blank(), strip.background = element_blank()) + xlim(0, 10) + geom_hline(aes(yintercept = -Inf)) + geom_vline(aes(xintercept = -Inf)) + coord_cartesian(clip = "off") + if (show_param_values) { geom_text(data = old_beta_labels, aes(x = x_mid, y = y_pos, label = label), inherit.aes = FALSE, color = '#fe9929', size = 3) }
🔹 This way:
The labels are centered in each step interval.
show_param_values controls whether they’re drawn.
You can tweak offset or size for readability.
Do you want the labels always numeric (like 0.23) or sometimes formatted (e.g. scientific, or "β=0.23") for clarity?
- User
I want alpha=, and for beta I rather half life so t_1/2=
- Assistant
Perfect 👍 thanks for clarifying! So you want:
For α (alpha): labels like "alpha=0.23".
For β (beta): instead of showing β itself, show the half-life t1/2=ln(2)βt_{1/2} = \frac{\ln(2)}{\beta}t1/2=βln(2) with labels like "t₁/₂=2.14".
Here’s how you could adapt the helper function and plots: rCopyEditlibrary(dplyr) library(rlang)
Generic function to compute x midpoint + y offset
add_step_labels <- function(df, value_col, label_type = "alpha", offset = 0.2) { df %>% mutate( x_mid = (Time + lead(Time, default = max(Time))) / 2, value = !!sym(value_col), label = case_when( label_type == "alpha" ~ paste0("alpha=", round(value, 2)), label_type == "beta" ~ paste0("t\u2081\u2044\u2082=", round(log(2)/value, 2)), # t₁/₂ TRUE ~ as.character(round(value, 2)) ), y_pos = value + offset ) }
show_param_values <- TRUE # toggle here
Label datasets
alpha_labels <- add_step_labels(coefficient_databeta_df, "beta", label_type = "beta", offset = 0.2) old_beta_labels <- add_step_labels(coefficient_data$old_beta_df, "beta", label_type = "beta", offset = 0.2)
Main plot
combined_plot <- ggplot(coefficient_dataalpha_df, aes(x = Time)) + geom_step(aes(y = alpha), color = '#3f007d', linewidth = 1) + geom_step(data = coefficient_databeta_df, aes(y = beta + 4), color = '#fe9929', linewidth = 1) + scale_y_continuous( name = "new dg(1/h)\nlog2", sec.axis = sec_axis(~. + 4, name = "tx(RNA/h)\nlog2") ) + theme_classic(base_size = 15) + theme(strip.text = element_blank(), strip.background = element_blank(), axis.title.y = element_text(color = '#fe9929'), axis.title.y.right = element_text(color = '#3f007d') ) + xlim(0, 10) + facet_grid(~gene_name) + geom_hline(aes(yintercept = -Inf)) + geom_vline(aes(xintercept = -Inf)) + coord_cartesian(clip = "off") + if (show_param_values) { list( geom_text(data = alpha_labels, aes(x = x_mid, y = y_pos, label = label), inherit.aes = FALSE, color = '#3f007d', size = 3), geom_text(data = beta_labels, aes(x = x_mid, y = y_pos + 4, label = label), inherit.aes = FALSE, color = '#fe9929', size = 3) ) }
Old beta plot
old_beta_plot <- ggplot(coefficient_data$old_beta_df, aes(Time, beta)) + geom_step(color = '#fe9929', linewidth = 1) + facet_grid(~gene_name) + xlab("") + ylab("old dg(1/h)\nlog2") + theme_classic(base_size = 15) + theme(strip.text = element_blank(), strip.background = element_blank()) + xlim(0, 10) + geom_hline(aes(yintercept = -Inf)) + geom_vline(aes(xintercept = -Inf)) + coord_cartesian(clip = "off") + if (show_param_values) { geom_text(data = old_beta_labels, aes(x = x_mid, y = y_pos, label = label), inherit.aes = FALSE, color = '#fe9929', size = 3) }
🔹 This gives you "alpha=..." and "t₁/₂=..." as text labels centered above each step. 🔹 The "t\u2081\u2044\u2082" renders as t₁⁄₂ (with subscript 1/2). Do you want me to also make the half-life labels units-aware (like "t₁/₂=2.14 h") or just the number?