Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h3>The "straight forward way"</h3> <p>Maybe you should save all your logical vectors in a data.frame or matrix before the loop:</p> <pre><code>selections &lt;- matrix(nrow = nrow(df), ncol = 4) selections[,1] &lt;- df$Test_11 == "A" selections[,2] &lt;- df$Test_11 == "B" selections[,3] &lt;- df$Test_12 == "A" selections[,4] &lt;- df$Test_12 == "B" # etc... par(mfcol = c(2, 2)) # here you should customize at will... for (i in 1:4) { z &lt;- subset(df, selections[,i]) plot(z$LVL11, z$Score_X_11, varwidth = TRUE, notch = TRUE, xlab = 'LVL', ylab = 'score') } </code></pre> <p>You can change your code so instead of using <code>z$Score_X_11</code>, use <code>z[,string]</code>. The value of <code>string</code> should be constructed with <code>paste</code> (or other string manipulating functions). For example:</p> <pre><code>v &lt;- c("X", "Y") n &lt;- c("11", "12") for (i in 1:2) { for (j in 1:2) { string &lt;- paste("Score", v[i], n[i], sep = "_") print(string) } } </code></pre> <p>A similar reasoning would be used with the <code>z$LVLXX</code> values, so you should be able to figure out a way to accommodate for that.</p> <h3>Alternative way, with ggplot2 &amp; reshape2</h3> <p>I'm not very experienced with using trellis graphics (like in the other anwser), but I know a little ggplot2, so I decided to take the challenge and try a bit. It is not great, but at least works:</p> <pre><code># df &lt;- read.table("data.txt", header = TRUE, na.string = "NA") require(reshape2) require(ggplot2) # Melt your data.frame, using the scores as the "values": mdf &lt;- melt(df[,-1], id = c("LVL11", "LV12", "Test_11", "Test_12")) # loop through level types: for (lvl in c("LVL11", "LV12")) { # looping through values of test11 for (test11 in c("A", "B")) { # Note the use of subset before ggplot p &lt;- ggplot(subset(mdf, Test_11 == test11), aes_string(x=lvl, y="value")) # I added the geom_jitter as in the example given there were only a few points g &lt;- p + geom_boxplot(aes(fill = variable)) + geom_jitter(aes(shape = variable)) print(g) # it is necessary to print p explicitly like this in order to use ggplot in a loop # Finally, save each plot with a relevant name: savePlot(paste0(lvl, "-t11", test11, ".png")) # (note that savePlot has some problems with RStudio iirc) } # Same as before, but with test_12 for (test12 in c("A", "B")) { p &lt;- ggplot(subset(mdf, Test_12 == test12), aes_string(x=lvl, y="value")) g &lt;- p + geom_boxplot(aes(fill = variable)) + geom_jitter(aes(shape = variable)) print(g) savePlot(paste0(lvl, "-t12", test12, ".png")) } } </code></pre> <p>If anyone knows how to use trellis graphics or maybe <code>facet_grid</code> in this case, so I can put all grahpics in one image, I would love to hear how.</p> <p>cheers.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload