I am creating a multipanel graph using plot_grid
of the relationship of discharge and surface area with species richness in Amazon Rivers. Here is my data:
riverssub<-structure(list(richness = c(127L, 110L, 89L, 74L, 62L, 18L, 22L,
71L, 38L, 91L, 56L, 39L, 90L, 37L, 147L, 53L, 92L, 207L, 52L,
126L, 79L, 32L, 100L, 181L, 83L, 690L), surface = c(33490, 4410,
770, 164.7, 288.5, 9.85, 33.1, 750, 46.9, 970, 85.2, 39.2, 780,
97.3, 3983.71, 220, 500, 11250, 115, 1350, 278, 23.05, 310, 2050,
560, 34570), disch = c(2640L, 687L, 170L, 353L, 384L, 16L, 31L,
513L, 32L, 392L, 50L, 32L, 206L, 81L, 1260L, 104L, 220L, 6100L,
308L, 2060L, 443L, 102L, 348L, 4758L, 913L, 40487L)), class = "data.frame", row.names = c(NA,
-26L))
Here is the code for my graphs and multiplot:
library(cowplot)
a <- ggplot(data = riverssub, aes(x = surface , y = richness))
geom_point()
scale_y_log10()
scale_x_log10()
labs(x='Surface Area (100 km\u00b2)', y="Fish Species Richness")
theme_bw()
b <- ggplot(data = riverssub, aes(x = disch , y = richness))
geom_point()
scale_y_log10()
scale_x_log10()
labs(x=bquote('Mean Annual Discharge'~(m^3 * s^-1)), y=" ")
theme_bw()
plot_grid(a, b theme(axis.text.y = element_blank()),
nrow = 1, align = 'h', labels="AUTO", label_y=0.97, label_x=0.1)
I want the "A" label to be in the same position on the first plot as the "B" label is on the second plot. I know I can use hjust()
within plot_grid()
to achieve this, although I am unsure how to do it. Can anyone help? Thanks in advance.
CodePudding user response:
Instead of fiddling around with hjust to place the labels I would suggest to add the labels on the plots before aligning them via plot_grid
as already suggested by @Guillaume in his comment. One option to do so and to ensure that the labels will be put on the same relative positions would be to make use annotation_custom
:
library(cowplot)
library(ggplot2)
library(magrittr)
a <- ggplot(data = riverssub, aes(x = surface, y = richness))
geom_point()
scale_y_log10()
scale_x_log10()
labs(x = "Surface Area (100 km\u00b2)", y = "Fish Species Richness")
theme_bw()
b <- ggplot(data = riverssub, aes(x = disch, y = richness))
geom_point()
scale_y_log10()
scale_x_log10()
labs(x = bquote("Mean Annual Discharge" ~ (m^3 * s^-1)), y = " ")
theme_bw()
theme(axis.text.y = element_blank())
list(A = a, B = b) %>%
purrr::imap(function(x, y) x annotation_custom(grid::textGrob(label = y, x = .05, y = .97, gp = grid::gpar(fontface = "bold")))) %>%
plot_grid(plotlist = ., nrow = 1, align = "h")