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.
# 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()
#> Calculated size_x: 219.7
#> Calculated size_y: 219.7
#> Applied padding_x: 0.3
#> Applied padding_y: 0.3
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()
#> Calculated size_x: 219.7
#> Calculated size_y: 219.7
#> Applied padding_x: 0.3
#> Applied padding_y: 0.3
#> Coordinate system already present. Adding new coordinate system, which will
#> replace the existing one.