Shamar D. Bradley
Shamar D. Bradley

Reputation: 33

plot loop through columns and row in [r]

hi I have a df that looks like:

A     B     C
101   201   301
102   202   302

and I want to create a 2x5 image plot of each column (each containing 10 values corresponding to jpg files) without repeating the code 100 times. my questions is how would I set up a loop to plot these for me.

this is the hard code I used for the first column:

require(jpeg)
image_frame = par(mfrow=c(5,2))
par(mar = c(1,1,1,1))

plot(1:2, type='n' ,xaxt='n', yaxt='n')
mtext("101", side=2, line = 0)
img101 = readJPEG('images/101.jpg')
rasterImage(img201, 1, 2, 2, 1)

I would assume a function and some variables to hold the value '101' in order to call the label and file name rather than hard code would be most efficient.

plotALL = function{
plot(1:2, type='n' ,xaxt='n', yaxt='n')
mtext("colName")
mtext("i", side=2, line = 0)
img101 = readJPEG('images/'i'.jpg')
rasterImage(img'i', 1, 2, 2, 1)


for(i in dim(df))
plotALL(i)

Upvotes: 1

Views: 209

Answers (1)

drmariod
drmariod

Reputation: 11762

Since there is no reproducible code, here is a rough guess...

par(mfrow=c(5,2))
for(column in df) {
  plot(1:2, type='n' ,xaxt='n', yaxt='n') # i guess this depends in the jpg
  for(i in column) {
    jpg <- readJPEG(sprintf('images/%s.jpg', i))
    rasterImage(jpg,1,2,2,1)
  }
  # save image somehow...
}

Upvotes: 1

Related Questions