I need to generate paged display of multiple plots. Intended format is html file generated from RMarkdown. I generate 110 plots. Need to find a way to present them in single html file.
I have dataset with measured concentrations of 22 substances. For each substance I generate 5 kinds of faceted histograms (3 vs 3 or 3 vs 2 - countries vs collection time, countries vs collection material etc.).
Each series of 22 plots is generated by for looping on the list of analytes names. The result in the report is vertical column of plots which is to long for comfortable scrolling.
I need to organise each o 22 charts group somehow. For obvious reasons I can't do this by another faceting.
I've tried to find a way to do this in the same manner as paged table display. Ideal solution would have names of analytes instead numbers as page labels.
I'm able to do this by manually editing html file but it is tedious work and probably I'll need to generate few more of this kind of reports in the future.
I'll appreciate any suggestions to make this task easier.
br Radek
edit: as requested I'm adding code example for one of the datatasets, and one kind of plots.
I have two datasets of similar structure.
I need 5 faceted plots for each compound. (2 for both datasets separately, and 1 comparing two datasets)
I have 3 functions to generate 3 kinds of plots.
Her is a truncated dataframe named 'mleko' (here only 10 rows - real datasets have ~200 observations each):
mleko <- structure(list(name = c("Z22-003355", "Z22-003356", "Z22-003357",
"Z22-003358", "Z22-003359", "Z22-003360", "Z22-003361", "Z22-003362",
"Z22-003363", "Z22-003364"), id = c("PL/WA/1 A", "PL/WA/1 B",
"PL/WA/1 C", "PL/WA/2 A", "PL/WA/2 B", "PL/WA/2 C", "PL/WA/5 A",
"PL/WA/5 B", "PL/WA/6 A", "PL/WA/6 B"), kraj = c("PL", "PL",
"PL", "PL", "PL", "PL", "PL", "PL", "PL", "PL"), osrodek = c("PL_WA",
"PL_WA", "PL_WA", "PL_WA", "PL_WA", "PL_WA", "PL_WA", "PL_WA",
"PL_WA", "PL_WA"), pobranie = c("A", "B", "C", "A", "B", "C",
"A", "B", "A", "B"), pacjent = c("WA_1", "WA_1", "WA_1", "WA_2",
"WA_2", "WA_2", "WA_5", "WA_5", "WA_6", "WA_6"), b1 = c(48.61,
42.929, 15.79, 25.806, 32.523, 21.143, 40.248, 47.691, 11.345,
35.598), b1_p = c(114.91, 169.298, 520.474, 406.057, 412.395,
1045.064, 488.215, 520.79, 439.307, 307.419), b2 = c(29.5, 48.762,
137.479, 111.126, 63.51, 43.571, 57.455, 186.378, 30.541, 332.967
), b3_acid = c(2.56, 6.44, 2.23, 2.51, 1.7, 2.79, 3.59, 5.09,
3.17, 2.95), b3_amide = c(367.44, 632.56, 1741.39, 295.07, 291.74,
565.64, 376.41, 951.17, 480.75, 230.35), b5 = c(1915.09, 2062.84,
2886.69, 2207.94, 5200.05, 2351.09, 1901.2, 3522.52, 2010.99,
1815), b6_amine = c(NA, NA, 8.11, 7.35, 10.51, 7.35, 28.4, 49.03,
7.2, 14.34), b6 = c(NA, 1.24, 0.79, 0.11, 0.46, 0.25, 0.25, 414.42,
0.47, 0.15), b6_al = c(148.21, 33.9, 46.54, 69.77, 112.04, 81.75,
79.07, 251.68, 51.91, 119.3), b7 = c(4.7, 5.63, 6.86, 7.7, 420.63,
8.24, 4.29, 9.87, 4.72, 8.76), b9 = c(8.98, NA, 1.81, NA, NA,
5.31, NA, 3.65, 19.89, 25.02), vit_c = c(3.9727, 0.0813, 2.614,
28.5194, 45.2353, 22.2135, 17.5204, 4.8187, 4.8742, 35.1743),
x25ohd3 = c(0.4893, 0.8018, 1.2627, 1.6619, 1.0247, 0.9958,
1.0195, 1.2343, 1.3659, 0.9405), pla = c(1229.84, 1663.01,
2589.72, 2219.31, 1459.39, 2558.98, 2472.36, 1800.24, 2486.02,
1426.25), ola = c(4898.71, 4101.36, 3988.89, 9719.39, 6551.23,
4185.18, 3604.93, 5774.73, 3536.59, 6225.97), la = c(1049.94,
777.15, 1242.82, 2390.08, 1901.75, 1154.73, 822.53, 1459,
485.42, 1664.16), ala = c(49.88, 90.86, 154.59, 179.29, 133.77,
77.53, 103.62, 59.95, 99.01, 84.97), gla = c(33.98, 59.19,
105.67, 98.24, 80.39, 47.06, 79.95, 47.73, 68.7, 56.52),
tva = c(4130.46, 3662.84, 10949.69, 6354.79, 3815.33, 3250.47,
6208.32, 3449.27, 7404.68, 6609.39), dha = c(104.38, 76.71,
124.12, 111.22, 107.06, 125.81, 64.23, 50.52, 158.14, 110.71
), epa = c(17.79, 18.87, 47.66, 51.48, 41.71, 30.84, 27.32,
14.01, 29.7, 27.18), ara = c(126.51, 142.05, 269.47, 235.31,
130.14, 111.5, 166.17, 93.85, 220.51, 80.44)), row.names = c(NA,
10L), class = "data. Frame")
Vector parametry
containing names of columns in which results are stored:
parametry <- c("b1", "b1_p", "b2", "b3_acid", "b3_amide", "b5", "b6_amine", "b6", "b6_al", "b7", "b9", "vit_c", "x25ohd3", "pla", "ola" "la", "ala", "gla", "tva", "dha", "epa", "ara")
One of the functions that generates the first kind of plots:
fac_hist_plot2 <- function(dane, parametr) {
outplot <- ggplot(aes(x = .data[[parametr]],
fill = pobranie),
data = dane) +
geom_histogram(
bins = 15,
color = "#000000",
label = count,
show.legend = FALSE) +
stat_bin(
bins = 15,
geom = 'text',
color = 'black',
size = 3.5,
na.rm = TRUE,
aes(x = .data[[parametr]], label = ifelse(..count.. > 0, ..count.., "")),
position = position_nudge(y = 2)) +
facet_grid(kraj ~ pobranie,
scales = "fixed") +
facet_rep_grid(kraj ~ pobranie,
scales = 'fixed',
repeat.tick.labels = 'bottom') +
scale_fill_manual(
aesthetics = "fill",
values = c(
"A" = "yellow",
"B" = "green",
"C" = "#0099F8"
)
) +
theme_ipsum_pub(base_family = "Iwona") +
labs(
x = "[ng/mL]",
y = "",
title = paste("Histogram stężeń", parametry_label[[parametr]]),
subtitle = "z podziałem na pobrania i kraje")
print(outplot)
}
Then I generate series of histograms in RMarkdown document:
for (ziutek in parametry) {
eval(call2("fac_hist_plot2", mleko, ziutek))
}
What I get is long vertical column of 22 plots. Each in separate file.
In the result file it looks like that. I removed all png meat because it would be to long, and copied only three segments of 22.
<!-- rnb-plot-begin eyJoZWlnaHQiOjYsIndpZHRoIjoxMCwic2l6ZV9iZWhhdmlvciI6MSwiY29uZGl0aW9ucyI6W119 -->
<p><img src="data:image/png;base64,(... png code ...)" /></p>
<!-- rnb-plot-end -->
<!-- rnb-plot-begin eyJoZWlnaHQiOjYsIndpZHRoIjoxMCwic2l6ZV9iZWhhdmlvciI6MSwiY29uZGl0aW9ucyI6W119 -->
<p><img src="data:image/png;base64,(... png code ...)" /></p>
<!-- rnb-plot-end -->
<!-- rnb-plot-begin eyJoZWlnaHQiOjYsIndpZHRoIjoxMCwic2l6ZV9iZWhhdmlvciI6MSwiY29uZGl0aW9ucyI6W119 -->
<p><img src="data:image/png;base64,(... png code ...)" /></p>
<!-- rnb-plot-end -->
I want it to look like RM paged display of dataframes. The first plot visible and labels of compounds (from named list parametry_label
) to click to view different plot.
I hope it is now clear what I'm trying to do.
br Radek
EDIT 2: Got idea but it not working as I expected.
Ok. I have an idea. I figured out how to generate tabbed output from loop.
In RMarkdown I have header that defines tabset.
## Histograms (xxx) {.tabset}
Then I generate plots as previously from loop but with every iteration I also generate markdown code for lower level header. R chunk with loop have results='asis' and fig.show='asis' in chunk options.
for (ziutek in parametry) {
header <- paste(" \n###", ziutek, "\n\n", sep = " ")
cat(header)
eval(call2("fac_hist_plot2", mleko, Ziutek))
}
This code generates tabs properly. The unexpected part is that all the graphs are in the last tab... Others tab are empty or have text warnings (on fonts - not relevant) for each plot.
How can I force it to render output in the order that it is generated in the loop? As I understand, now it renders all text output of the chunk first and after that all the graphics. Why "fig.show='asis'" doesn't work? The result I see is as described for "fig.show='hold'".