0

I have quick question, I do I take these type of data and loop through and put each datapoint into a new plot?

quantity <- c(3,5,2)
names(quantity) <- c("apples","bananas", "pears")
plot(quantity)

png(file = Path, width =4, height =4, units = "in", res = 600)
par(mfrow = c(3,3)
for (i in names(quantity)){
 print(i)
 plot(x = quantity[i], y = names(quantity)[i])
}
dev.off()
Genetics
  • 279
  • 2
  • 11
  • Do you want a separate png for each subset? If so put the `png()` and `dev.off()` lines *inside* the `for` loop, and create the file name with e.g., `paste(i, ...)`. Or, switch from png to pdf, and you will get a single pdf with multiple pages (1 for each plot). – sashahafner Mar 17 '22 at 14:47
  • for (i in names(FFmeans)){ print(paste("/Users/highfillca/Desktop/Test/", i, ".png", sep = "")) png(file = paste("/Users/highfillca/Desktop/Test", i, ".png", sep = ""), width = 4, height =4, units = 'in', res=600) plot(x = FFmeans[i], y = names(FFmeans)[i]) dev.off() } – Genetics Mar 17 '22 at 14:56
  • gives error: Error in plot.window(...) : need finite 'ylim' values – Genetics Mar 17 '22 at 14:56
  • @Genetics: would you consider using `ggplot2` https://stackoverflow.com/a/52045613/786542 ? – Tung Mar 17 '22 at 15:02
  • That error is not specific to this loop problem but presumably the data you are trying to plot. Try to take a look at the specific subset you are trying to plot in your console. It looks like you have one numeric value for x, and one character value for y. Seems problematic. – sashahafner Mar 17 '22 at 15:02
  • @sashahafner, this is the str: Named num [1:3] 3 5 2 - attr(*, "names")= chr [1:3] "A" "B" "C" I am wanting to make an individual plot for the value of A and label A, then make a new plot for B, etc. – Genetics Mar 17 '22 at 15:06
  • @ Tung, not opposed to ggplot2, just always thinking of base R – Genetics Mar 17 '22 at 15:08
  • Two issues: 1) that structure shows the entire vector, while your `plot()` call is using single elements, 2) as far as I know you cannot do `plot(, )`, which is what you are doing. If I try this, for example, I see the same error: `plot(1, 'cat')`. My point is the axis limit error you see is not specific to your question. It is a more basic problem related to how you have called `plot`. My advice: think about what it is you want to plot, test out your `plot` call on a sample subset, and then work on your loop. – sashahafner Mar 17 '22 at 15:36

0 Answers0