Flipping scatterbar oreintations and positions

Flipping scatterbar oreintations and positions

This tutorial demonstrates how to change the orientation of a scatterbar plot using the scatterbar package. Specifically, we will flip the x and y coordinates to create an alternative view of the data.

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
# Load packages and example adult mouse brain dataset
library(scatterbar)
library(ggplot2)

data("adult_mouse_brain_ffpe")

Here, we flip the x and y coordinates in the pos data frame to change the orientation of the whole scatterbar plot, such that the adult mouse brain is now displayed horizontally.

# Flip the x and y columns of the position data
flipped_pos <- adult_mouse_brain_ffpe$pos[, c(2,1)]
# Rename the columns to ensure the position data has the correct column names
colnames(flipped_pos) <- c('x','y')
# Create the scatterbar plot with the flipped position data
start.time <- Sys.time()
scatterbar::scatterbar(adult_mouse_brain_ffpe$prop, 
                       flipped_pos, padding_x = 0.3, padding_y = 0.3,
                       size_x = 220, size_y = 220,
                       legend_title = "Cell Types") + coord_fixed()
end.time <- Sys.time()
print(end.time - start.time)

We can also change the orientation of the stacked bar charts themselves and how they are displayed. With coord_flip(), the adult mouse brain is now displayed vertically once more, but the stacked bar charts are now displayed horizontally.

# Create the scatterbar plot with the flipped scatterbar oreintation
start.time <- Sys.time()
scatterbar::scatterbar(adult_mouse_brain_ffpe$prop, 
                       flipped_pos, padding_x = 0.3, padding_y = 0.3,
                       size_x = 220, size_y = 220,
                       legend_title = "Cell Types") + coord_fixed() + coord_flip()
end.time <- Sys.time()
print(end.time - start.time)


Try the scatterbar package in your browser

Any scripts or data that you put into this service are public.

scatterbar documentation built on April 4, 2025, 5:23 a.m.