0

I want to draw a table (actually just one column of a table with differently coloured fonts) in the upper left corner of each subplot of a facet plot. The major difficulty here is, that the table should be individual for each facet by only choosing the values for each facet variable (in this case two variables: Boden ~ Ionenstärke). So far I was only able to draw the same table (with all values) in corner of each facet.

So here is an plot example which I achieved so far:

enter image description here

And this is what I want to get:

enter image description here

For the drawing of the table I use annotation_custom() in combination with the tableGrob(). However, this might be to limited for my particular problem.

This is the code for the plot:

valency.facet.plot <- function(df, preds, rmse, Substance) {
  ggplot() + 
    geom_ribbon(data = ribbon_oecd, aes(x = x, ymin = y_min, ymax = y_max), fill = "thistle1", color = NA, alpha = 0.3) + # oecd crit area < kd 0.6
    geom_ribbon(data = ribbon_ana, aes(x = x, ymin = y_min, ymax = y_max), fill = "plum1", color = NA, alpha = 0.3) + # analytical error area < kd 0.2
    geom_ribbon(data = preds, aes(x = c, ymin = s_lwr, ymax = s_upr, fill = Reihe), alpha = 0.5, color = NA) +
    geom_point(data = df, aes(x = c, y = s, shape = Reihe, color = Reihe), size = 2.2) + # raw data points
    geom_line(data = preds, aes(x = c, y = s_fit, color = Reihe, lty = model_name)) + # add different premodels to the plot
    facet_rep_grid(Boden ~ Ionenstärke,
                   labeller = labeller(Boden = c("A" = "Horizon A", "B" = "Horizon B"), 
                                       Ionenstärke = c("0.03 M" = "medium IS", "0.3 M" = "high IS"))) +
    coord_capped_cart(bottom='both', left='both') +
    scale_shape_manual(name = "Observation:",
                       breaks = c("0.01 M CaCl2", "0.03 M NaCl", "0.1 M CaCl2", "0.3 M NaCl"),
                       labels = c(bquote("0.01 M" ~ CaCl[2]),
                                  "0.03 M NaCl" ,
                                  bquote("0.1 M" ~ CaCl[2]),
                                  "0.3 M NaCl"),
                       values = c("0.01 M CaCl2" = 22, "0.03 M NaCl" = 23, "0.1 M CaCl2" = 24, "0.3 M NaCl" = 25)) +
    scale_color_manual(name = "Isotherm with 95% CI:",
                       breaks = c("0.01 M CaCl2", "0.03 M NaCl", "0.1 M CaCl2", "0.3 M NaCl"),
                       labels = c(bquote("0.01 M" ~ CaCl[2]),
                                  "0.03 M NaCl" ,
                                  bquote("0.1 M" ~ CaCl[2]),
                                  "0.3 M NaCl"),
                       values = c("0.01 M CaCl2" = "#fcb514",
                                  "0.03 M NaCl" = "#B4AF46",
                                  "0.1 M CaCl2" = "#637d9e",
                                  "0.3 M NaCl" = "#B4464B")) +
    scale_fill_manual(name = "Isotherm with 95% CI:",
                      breaks = c("0.01 M CaCl2", "0.03 M NaCl", "0.1 M CaCl2", "0.3 M NaCl"),
                      labels = c(bquote("0.01 M" ~ CaCl[2]),
                                 "0.03 M NaCl" ,
                                 bquote("0.1 M" ~ CaCl[2]),
                                 "0.3 M NaCl"),
                      values = c("0.01 M CaCl2" = "grey",
                                 "0.03 M NaCl" = "grey", "0.1 M CaCl2" = "grey",
                                 "0.3 M NaCl" = "grey")) +
    scale_linetype_manual(name = "Isotherm model:",
                          breaks = c("henry.wls", "freund.wls", "hydrus.wls"),
                          labels = c("Henry", "Freundlich", "Mixed"),
                          values = c("henry.wls" = 1, "freund.wls" = 2, "hydrus.wls" = 4)) +
    annotation_custom(tableGrob(rmse[,3], # add parameters as table into the plot
                                rows = NULL,
                                cols = NULL,
                                theme = ttheme_minimal(base_size = 12,
                                                       padding = unit(c(2, 2), "mm"),
                                                       core=list(fg_params=list(col = rmse$cols)))),
                      xmin = max(df$c)*0.06, xmax = max(df$c)*0.06,
                      ymin =  max(preds$s_upr)*0.7, ymax =  max(preds$s_upr)*0.7) +
    labs(title = "Influence of cation valency (facet-plot)", subtitle = paste0("PM-Substance: ",Substance),
         y = "s (µg/kg)", x = "c (µg/l)") +
    coord_cartesian(expand = FALSE, xlim = c(max(df$c)*-0.02, max(df$c)*1.05),
                    ylim = c(max(preds$s_upr)*-0.02, max(preds$s_upr)*1.07)) +
    theme_classic() +
    theme(panel.border=element_blank(), axis.line=element_line(),
          legend.position = "bottom", legend.margin = margin(0,0,0,0, unit="cm"),
          strip.background = element_rect(colour = NA), strip.text = element_text(face = "bold", size = 13.5),
          axis.text = element_text(size = 13), axis.title = element_text(size = 14), legend.text = element_text(size = 12)) +
    guides(shape = guide_legend(order = 1, ncol = 1,
                                override.aes = list(color = c("0.01 M CaCl2" = "#fcb514",
                                                              "0.03 M NaCl" = "#B4AF46",
                                                              "0.1 M CaCl2" = "#637d9e",
                                                              "0.3 M NaCl" = "#B4464B"))), # order the different legends
           fill = guide_legend(order = 2, ncol = 1),
           color = guide_legend(order = 2, ncol = 1, override.aes = list(shape = NA)),
           linetype = guide_legend(order = 3, ncol = 1))
  
}

Im working with a plot function to use the purrr package to automatically plot the same plot for different variables (here different pollutants). The subtable behind the "rmse" variable in the plot function, which is called in the tableGrob() function for the example looks like this:

enter image description here

So the two variables (Boden ~ Ionenstärke) in the facet_rep_grid() exist as columns of the "rmse" subtable.

stefan
  • 90,330
  • 6
  • 25
  • 51
Mogens
  • 1
  • 1
  • This is one of the downsides when using `annotation_custom`. The grob will be drawn in each panel. One possible option would be the [`gggrid`](https://www.stat.auckland.ac.nz/~paul/Reports/gggrid/gggrid.html) package which has options to overcome this limitation. For more help I'm afraid we need a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your data or some fake data to reproduce your chart and figure out a solution. – stefan Jun 13 '23 at 11:34

0 Answers0