# Example figures for plotting core data for PA
# Main issue is that ggplot2 doesn't do coord_flip with free_scales
# see https://github.com/hadley/ggplot2/issues/95
# and
# http://stackoverflow.com/questions/12560858/using-coord-flip-with-facet-wrapscales-free-y-in-ggplot2-seems-to-give-u
# and
# http://stackoverflow.com/questions/16574841/ggplot-0-9-3-issue-with-facet-wrap-free-scales-and-coord-flip-2nd-try
# So make the plots and then use grid.arrange to sort them out
# Load libraries
library(ggplot2)
library(reshape)
## Loading required package: plyr
##
## Attaching package: 'reshape'
##
## Les objets suivants sont masqués from 'package:plyr':
##
## rename, round_any
library(gridExtra)
## Loading required package: grid
# Create data
data <- data.frame(depth = 0:50, CN = 2 * sin(0:50/2) + 15, LOI = exp(50:0/15)/100)
# Problem figure
data.melted <- melt(data, id = "depth")
ggplot(data.melted, aes(x = depth, y = value)) + geom_point() + geom_line() +
facet_grid(. ~ variable, scales = "free") + coord_flip()
# Could do like this and then rotate in Inkscape
ggplot(data.melted, aes(x = depth, y = value)) + geom_point() + geom_line() +
facet_grid(variable ~ ., scales = "free")
# Or use grid.arrange
a <- ggplot(data, aes(x = depth, y = CN)) + geom_point() + geom_line() + scale_x_reverse() +
coord_flip() + labs(x = "Depth (cm)", y = "C:N (molar)")
b <- ggplot(data, aes(x = depth, y = LOI)) + geom_point() + geom_line() + scale_x_reverse() +
coord_flip() + labs(x = "", y = "LOI")
grid.arrange(a, b, nrow = 1)