Multiple Graphs

code
AFM
Create multiple graphs in one plot using R
Author

Thomas Gredig

Published

December 4, 2024

In this code, we are computing the height-height correlation function for several AFM images at once. We need to check that the fit is good, such that the parameters that we will use are valid. At the same time, the idea is to condense the graph into a half-page; so that we can quickly check whether there are any problems with the fit.

Code
library(nanoAFMr)
library(dataZIF8)
library(ggplot2)
library(cowplot)
m <- dataAFMget()

We use the cowplot R package to create a 3x4 grid in this example. It includes the function plot_grid() to put multiple images into one graph:

Code
afm_IDs = c(1014, 1071, 1088, 1107, 1209,
            1210, 1211, 1244, 1256, 1266, 1275)
g <- list()
gHHCF <- list()
i = 0
for(ID in afm_IDs) {
  a <- dataAFMget(ID)
  a <- AFM.flatten(a)
  i = i+1
  g[[i]] <- plot(a, graphType = 3, quiet=TRUE)
  aHHCF <- AFM.hhcf(a, addFitLabels=FALSE)
  gHHCF[[i]] <- aHHCF
}
Error in nls(data = r, g ~ 2 * (sigma * sigma) * (1 - exp(-(r.nm/xi)^(2 *  : 
  number of iterations exceeded maximum of 50
Error in nls(data = r, g ~ 2 * (sigma * sigma) * (1 - exp(-(r.nm/xi)^(2 *  : 
  number of iterations exceeded maximum of 50
Error in numericDeriv(form[[3L]], names(ind), env, central = nDcentral) : 
  Missing value or an infinity produced when evaluating the model

AFM graphs:

Code
plot_grid(plotlist = g, ncol=4, labels=1:11)

Height-height Correlation Graphs:

Code
plot_grid(plotlist = gHHCF, ncol=4)

Code
if (!file.exists("multiGraph.png")) {
  ggsave("multiGraph.png", dpi=144, width=4, height=3)
}

Citation

BibTeX citation:
@misc{gredig2024,
  author = {Gredig, Thomas and Gredig, Thomas},
  title = {Multiple {Graphs}},
  date = {2024-12-04},
  url = {https://www.csulb.edu/~tgredig/posts/Rcode/multiGraph.html},
  langid = {en}
}
For attribution, please cite this work as:
Gredig, Thomas, and Thomas Gredig. 2024. “Multiple Graphs.” https://www.csulb.edu/~tgredig/posts/Rcode/multiGraph.html.