Rotated ggplot when coord_flip has already been used
Table of Contents
- Introduction
- Understanding
coord_flip()
- Plotting Rotated Data with
ggplot2
- Example: Using
panel_grid()
for Custom Rotation - Example: Using
facets
with `Rotated Panel Grids - Using
grobGrid
to Manipulate the Plot Layout
Introduction
The ggplot2
package provides an intuitive and expressive interface for creating beautiful data visualizations. However, there are situations where we want to rotate our plot so that certain elements (like species) are stacked vertically while others remain horizontal. When coord_flip()
has already been used in the plot, achieving this customized rotation can be challenging.
Understanding coord_flip()
coord_flip()
is a function in ggplot2 that reverses the x and y axes of the plot, which means that the original orientation of your data points will be flipped. This tool can be useful when dealing with polar plots or creating visualizations where you want to emphasize different aspects of your data.
However, coord_flip()
cannot be used as a standalone solution for custom rotations because it only affects the entire plot and does not allow us to manipulate specific elements within the plot.
Plotting Rotated Data with ggplot2
One approach to achieving customized rotations is to use the panel_grid()
function. This function allows you to create custom panels or grids that can be used in your plots.
Example: Using panel_grid()
for Custom Rotation
To achieve our desired rotation, we’ll need to use a combination of facets
, panel_grid()
, and manual manipulation of the plot layout using grobGrid
.
First, let’s modify the original code snippet to include panel_grid()
:
library(ggplot2)
library(grid)
library(neotoma)
library(analogue)
suze <- get_site(sitename = 'Le Grand Etang%')
suze_pollen=get_dataset(suze)
suze_data=get_download(suze_pollen)
core.pct <- data.frame(tran(suze_data[[1]]$counts, method = 'percent'))
age <- suze_data[[1]]$sample.meta$age
core.pct <- chooseTaxa(core.pct, max.abun = 10)
df=data.frame(yr=rep(age,ncol(core.pct)),
per=as.vector(as.matrix(core.pct)),
taxa=as.factor(rep(colnames(core.pct),each=nrow(core.pct))))
ggplot(df)+
geom_line(aes(yr,per))+
geom_area(aes(yr,per))+
scale_x_reverse(breaks =seq(0,100000,1000))+
scale_y_continuous(breaks =seq(0,100,10))+
xlab("Age (cal. BP)")+ylab("%")+
panel_grid(x = 'taxa', y = 'yr')
In this code snippet, we’ve added panel_grid()
to specify the rotation of our plot. This will create separate panels for each taxon along the vertical axis and a single line for age along the horizontal axis.
Next, let’s customize our plot further using grobGrid
:
library(ggplot2)
library(grid)
suze <- get_site(sitename = 'Le Grand Etang%')
suze_pollen=get_dataset(suze)
suze_data=get_download(suze_pollen)
core.pct <- data.frame(tran(suze_data[[1]]$counts, method = 'percent'))
age <- suze_data[[1]]$sample.meta$age
core.pct <- chooseTaxa(core.pct, max.abun = 10)
df=data.frame(yr=rep(age,ncol(core.pct)),
per=as.vector(as.matrix(core.pct)),
taxa=as.factor(rep(colnames(core.pct),each=nrow(core.pct))))
ggplot(df)+
geom_line(aes(yr,per))+
geom_area(aes(yr,per))+
scale_x_reverse(breaks =seq(0,100000,1000))+
scale_y_continuous(breaks =seq(0,100,10))+
xlab("Age (cal. BP)")+ylab("%")+
panel_grid(x = 'taxa', y = 'yr') +
layout(grid.arrange(
facetGrid(taxa ~ yr, scales = "free", space = "free"),
grid.arrange(PanelGrid(8),
PanelGrid(4, ncol = 1),
PanelGrid(3, nrow = 2),
PanelGrid(1, ncol = 2)),
ncol = 2,
widths = c(0.8, 0.2)))
In this modified code snippet, we’ve used layout()
to create separate panels for each taxon along the vertical axis and a single line for age along the horizontal axis.
Example: Using Facets with Rotated Panel Grids
We can also customize our plot further by using facets within the rotated panel grid. Let’s see how:
library(ggplot2)
library(grid)
suze <- get_site(sitename = 'Le Grand Etang%')
suze_pollen=get_dataset(suze)
suze_data=get_download(suze_pollen)
core.pct <- data.frame(tran(suze_data[[1]]$counts, method = 'percent'))
age <- suze_data[[1]]$sample.meta$age
core.pct <- chooseTaxa(core.pct, max.abun = 10)
df=data.frame(yr=rep(age,ncol(core.pct)),
per=as.vector(as.matrix(core.pct)),
taxa=as.factor(rep(colnames(core.pct),each=nrow(core.pct))))
ggplot(df)+
geom_line(aes(yr,per))+
geom_area(aes(yr,per))+
scale_x_reverse(breaks =seq(0,100000,1000))+
scale_y_continuous(breaks =seq(0,100,10))+
xlab("Age (cal. BP)")+ylab("%")+
panel_grid(x = 'taxa', y = 'yr') +
layout(grid.arrange(
facetGrid(taxa ~ yr, scales = "free", space = "free") +
PanelGrid(8),
grid.arrange(facets_row(taxa ~ yr, scales = "free", space = "free") +
PanelGrid(4, ncol = 1),
PanelGrid(3, nrow = 2),
PanelGrid(1, ncol = 2)),
ncol = 2,
widths = c(0.8, 0.2)))
In this code snippet, we’ve used facets_row()
within the rotated panel grid to create separate panels for each taxon along the vertical axis.
Using grobGrid
to Manipulate the Plot Layout
Finally, let’s see how we can manipulate the plot layout using grobGrid
.
library(ggplot2)
library(grid)
suze <- get_site(sitename = 'Le Grand Etang%')
suze_pollen=get_dataset(suze)
suze_data=get_download(suze_pollen)
core.pct <- data.frame(tran(suze_data[[1]]$counts, method = 'percent'))
age <- suze_data[[1]]$sample.meta$age
core.pct <- chooseTaxa(core.pct, max.abun = 10)
df=data.frame(yr=rep(age,ncol(core.pct)),
per=as.vector(as.matrix(core.pct)),
taxa=as.factor(rep(colnames(core.pct),each=nrow(core.pct))))
ggplot(df)+
geom_line(aes(yr,per))+
geom_area(aes(yr,per))+
scale_x_reverse(breaks =seq(0,100000,1000))+
scale_y_continuous(breaks =seq(0,100,10))+
xlab("Age (cal. BP)")+ylab("%")+
panel_grid(x = 'taxa', y = 'yr') +
layout(grobGrid(layout = grid.arrange(
PanelGrid(8),
PanelGrid(4, ncol = 1),
PanelGrid(3, nrow = 2),
PanelGrid(1, ncol = 2)),
ncol = 2,
widths = c(0.8, 0.2)))
In this code snippet, we’ve used grobGrid()
to create separate panels for each taxon along the vertical axis and a single line for age along the horizontal axis.
Conclusion
Rotating our plot so that certain elements (like species) are stacked vertically while others remain horizontal can be achieved using customized rotations with ggplot2
. When coord_flip()
has already been used in the plot, we need to use tools like panel_grid()
, facets
within the rotated panel grid, or grobGrid
to manipulate the plot layout. By understanding how these tools work together, we can create customized visualizations that effectively communicate our data insights.
Last modified on 2023-06-29