---
title: "Flipping scatterbar oreintations and positions"
author: "Dee Velazquez and Jean Fan"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Flipping scatterbar oreintations and positions}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---
# 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.

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

```{r setup}
# 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.

```{r flipped plot}
# 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.  

```{r flipped orientation}
# 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)
```
