Skip to contents

This function creates a scatterbar plot using ggplot2, where the bars are stacked based on the different proportions of groups in each 2-D location/spot. A scatterbar plot is a combination of a scatter plot and a stacked bar chart, allowing for the visualization of proportional data across spatial coordinates. The function allows for customized scaling factors and padding when creating the plot. If no scaling factors are specified, the function automatically determines the optimal scaling factors based on the data.

Usage

create_scatterbar(
  data,
  xy,
  size_x = NULL,
  size_y = NULL,
  padding_x = 0,
  padding_y = 0,
  show_legend = TRUE,
  legend_title = "Group",
  colors = NULL
)

Arguments

data

A data frame containing the proportions of different categories for each location. Each row represents a location, and each column (except the row names) represents a category.

xy

A data frame containing the positional information for each location. This data frame includes the x and y coordinates for each location/spot (the respective row names).

size_x

X-axis scaling factor (default is NULL). If not provided, it will be automatically calculated based on the data.

size_y

Y-axis scaling factor (default is NULL). If not provided, it will be automatically calculated based on the data.

padding_x

Padding for x-axis (default is 0).

padding_y

Padding for y-axis (default is 0).

show_legend

Boolean indicating whether to display the plot legend (default is TRUE).

legend_title

Custom title for the legend (default is "Group").

colors

Optional vector of colors to use for each category (default is NULL). If not provided, a default palette will be used.

Value

A ggplot object representing the scattered stacked bar chart plot.

Examples

data(mOB)
create_scatterbar(mOB$data, mOB$xy, padding_x = 0.3, padding_y = 0.3, legend_title = "Cell Types")