2

I am trying to add a table that summarizes the info I have in a facet grid boxplot to the same plot.

df<- structure(list(design = c("F", "F", "F", "F", "H", "H", "H", 
"H", "H", "R", "R", "R", "R", "R", "SR", "SR", "SR", "SR", "SR"
), Species = c("LMB", "LMB", "BLG", "BLG", "BLG", "BLG", "LMB", 
"BLG", "LMB", "BLG", "LMB", "LMB", "LMB", "BLG", "BLG", "LMB", 
"BLG", "BLG", "LMB"), Value = c(10.9673144080207, 9.69716158252908, 
13.6176455626267, 12.5184221861068, 7.07161082124456, 26.6081334130079, 
4.83476200538245, -2.44881629932982, 1.63297655884495, 18.8302775817964, 
6.8889811208883, 4.6387733793561, 7.56071784901834, -26.3109417867886, 
27.4553930399756, -3.19241479249914, -9.65440732501407, 40.3505935270246, 
2.9681096231288), season = structure(c(1L, 2L, 1L, 2L, 2L, 2L, 
1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L), levels = c("Spring", 
"Fall"), class = "factor")), row.names = c(NA, -19L), class = c("data.table", 
"data.frame"))

ggplot(df, aes(x=design, y=Value))+
  geom_boxplot()+
  stat_summary(fun = mean, shape=21, size=1, fill='red', col='red', geom='point')+
  #geom_jitter(alpha = 0.09, width = 0.2)+
  facet_grid(season ~ Species)+
  ylab("Relative Bias (RB%)")+
  xlab("Design")+
   theme_light()

I want to add the following summed mean values across observations into a table and to the right of the plot:

table<- structure(list(indicator = c("total_spring", "total_fall"), season = c("Spring", 
"Fall"), f = c("22", "15"), h = c("1.9", "23.1"), r = c("-6.1", 
"13"), sr = c("9", "-8")), class = "data.frame", row.names = c(NA, 
-2L)) # numbers are made up for example, not representative of data in boxplots

I've tried a couple of solutions using plotrix and ggpmisc, but had no luck. Ideally, this table would just take the place of another column added to the grid. Wondering if this might just be easiest with gridExtra and adding them together as separate objects? Any help would be greatly appreciated.

hugh_man
  • 399
  • 1
  • 6

2 Answers2

2

You could try with patchwork and gridExtra:


library(ggplot2)
library(patchwork)
library(gridExtra)

gg <- ggplot(df, aes(x=design, y=Value))+
  geom_boxplot()+
  stat_summary(fun = mean, shape=21, size=1, fill='red', col='red', geom='point')+
  facet_grid(season ~ Species)+
  ylab("Relative Bias (RB%)")+
  xlab("Design")+
  theme_light()

# use gridExtra to turn the table into a Grob
table <- tableGrob(table)

# plot side by side with patchwork, control relative size with `widths` and `heights` arguments
gg + table +
  plot_layout(widths = c(5, 7),
              heights = c(5, 3))

Created on 2022-05-06 by the reprex package (v2.0.1)

Peter
  • 11,500
  • 5
  • 21
  • 31
2

There's many ways, e.g. using cowplot, if your plot is p:

library(gridExtra)
library(cowplot)

table_grob <- tableGrob(table, rows = NULL)

ggdraw() +
  draw_plot(p, width = 0.6) +
  draw_grob(table_grob, x = 0.6, width = 0.4)

enter image description here

Axeman
  • 32,068
  • 8
  • 81
  • 94
  • Thank you! I haven't used cowplot before but seems like this would also work really well in addition to Peter's answer. Thanks for your response! – hugh_man May 06 '22 at 18:01