Skip to contents

Compliments create_histogram_df() to generate P() plots over time.

Usage

plot_probability(
  df,
  plot_stim_times,
  plot_value = "probability",
  xmax = 40,
  ymax = 1,
  y_label = NULL,
  df_inset = NULL,
  sd_value = NULL,
  manual_condition_color = NULL,
  ...
)

Arguments

df

Data Frame or Tibble with the following columns:

plot_stim_times

Integer, stimulus times in milliseconds.

plot_value

Character, df column to use for plotting. Defaults to "probability".

xmax

Optional integer, maximum x-value to plot in milliseconds (ms), values are rounded to the nearest 10. Default is 40, xmin is fixed at 0.

ymax

Optional integer, maximum y-value to plot, values are rounded the nearest 1. Default is 1, ymin is fixed at 0.

y_label

Optional character, custom label for the main y-axis. Default NULL, will use the column name from plot_value.

df_inset

Optional Data Frame, if present a inset is added to the plot in the upper right corner. Default is NULL.

sd_value

Character, df column to use for plotting error bars. Default is NULL.

manual_condition_color

Character, vector of color values. Default is NULL, which will automatically generating color values.

...

Optional vargs, to apply additional ggplot function calls, e.g. ylim(), ylab(). If df_inset is NULL, see note below.

Value

ggplot2 object

Note

When adding an inset to the plot, if you want to add additional ggplot function calls, you must add these using the varg, .... As this function will return a grob object. However, if the df_inset arg is left at the default value of NULL, then a ggplot object is returned and additional ggplot function calls can added post hoc, e.g. plot_probability() + ylab("y-axis label").

Examples

library(magrittr, include.only = "%>%")

# example setup
bins <- seq(0.5, 39.5, by = 1)
n_bins <- length(bins)
conditions <- c("control", "drug_1")
n_conditions <- length(conditions)

# example output from create_histogram_df()
simple_tbl <-
  tibble::tibble(
    condition = rep(conditions, each = n_bins),
    h_bin_centers = rep(bins, n_conditions),
    probability = 0
  ) %>%
  dplyr::mutate(probability = dplyr::case_when(
    condition == "control" & h_bin_centers %in% c(1.5, 11.5) ~ 1,
    condition == "drug_1" & h_bin_centers %in% c(1.5, 11.5) ~ 0.5,
    .default = 0
  ))

plot_probability(
  df = simple_tbl,
  plot_stim_times = c(0, 10, 20),
  plot_value = "probability"
)