0

I have a chemical dataset with 6 variables: Oxygen, Nitrogen, Sodium, Calcium, Zinc, and Copper. I generated a matrix plot that has the the histogram of each variable and the corresponding qq plot with title of each plot as the variable's name. Below is my code, it's working, but it's inefficient because I did a lot of copy/paste. Is there a way to automate a process to generate plots for every variable in the dataset where every plot has the name of the variable as a title? Also, as you see in the code I am using mfrow in R-studio which limits the number of plots- It says margins are too large. Is there a way to send the output directly to a pdf without visualizing it in R-studio just to avoid the issue with margin too large? I just started learning R. Excuse me if my question is too basic. Thank you!

par(mfrow=c(6,2))

hist(Oxygen,main="Oxygen")
qqnorm(Oxygen,main="Oxygen");qqline(Oxygen, col='red')

hist(Nitrogen,main="Nitrogen")
qqnorm(Nitrogen,main="Nitrogen");qqline(Nitrogen, col='red')

hist(Sodium,main="Sodium")
qqnorm(Sodium,main="Sodium");qqline(Sodium, col='red')

hist(Calcium,main="Calcium")
qqnorm(Calcium,main="Calcium");qqline(Calcium, col='red')

hist(Zinc,main="Zinc")
qqnorm(Zinc,main="Zinc");qqline(Zinc, col='red')

hist(Copper,main="Copper")
qqnorm(Copper,main="Copper");qqline(Copper, col='red')
Moe.A
  • 81
  • 10
  • Have you tried a basic for loop? `for (i in c(variable1, variable2, etc.)) { print(i) }` – Benjamin Feb 13 '16 at 18:02
  • This link has an example of writing a plot to a file http://stackoverflow.com/questions/7144118/how-to-save-a-plot-as-image-on-the-disk. PDF is one option but so are jpeg and png. – steveb Feb 13 '16 at 18:22
  • @Benjamin thank you for your reply. I used the for loop and worked well for me. However I still can't get the title to be the variable name. – Moe.A Feb 14 '16 at 09:43
  • @Benjamin thank you for your reply. I used the for loop and worked well for me. However I still can't get the plot title to be the variable name. What should type after main= so I get the variable name as a title (oxygen, nitrogen .. ect). I spent 10 hours and couldn't figure it out. for (i in chemicals) { hist(i,main=??) qqnorm(i,main=??);qqline(i, col='red') } – Moe.A Feb 14 '16 at 09:50
  • You can use deparse(substitute(a)). – Benjamin Feb 14 '16 at 14:16

0 Answers0