[R] Writing Graphs to Files

Jerry Floren jerry.floren at state.mn.us
Wed Jan 27 16:14:05 CET 2010


Greetings,

I am trying to write multiple .emf graphs to individual files.

Our data is from agricultural laboratories where individual soil samples are
tested for various plant nutrients, for example, ammonium nitrogen,
phosphorus, and zinc. Basically, there are three important columns: the
laboratory performing the tests, the type of test for each soil sample, and
the results

Lab_ID       Anlysis_Soil     Results
55001	AmmonumN_Soil1	      5.3
55002	AmmoniumN_Soil2	      5.4
55003	AmmoniumN_Soil3       8.8
55004	AmmoniumN_Soil4	      6.9
55001	Phosphorus_ Soil1     15.3
55002	Phosphorus_ Soil2     25.8
55003	Phosphorus_ Soil3     37.8
55004	Phosphorus_ Soil4     46.9
55001	Zinc_ Soil1                1.3
55002	Zinc_ Soil2                1.4
55003	Zinc_ Soil3	              1.8
55004	Zinc_ Soil4	              1.9

Here is the introductory code:

#Set the working directory
setwd("C:\\Documents and Settings\\jfloren\\My Documents\\TestRSoil")

napt <- read.table(file = "./readin_all_for_2010_cert.csv ", header = TRUE,
sep = ",")
attach(napt)

## Set up the subset for statistics and graphs -- "myV" cycles through
Nitrogen_Soil1 to Zinc_Soil4. ##

myVars <- levels(Anlysis_Soil)

for (myV in myVars) 
{ 
rsubset <- subset(napt, Anlysis_Soil %in% myV) 
 if (nrow(rsubset) > 8) 
 { 

###########  These are the start of the statistical results for each subset 
###########  with more than 8 labs submitting results        #######

attach(rsubset)

count = length(Results)
amed = median(Results)
aMAD = mad(Results, constant 

### Many more statistical results and a table is written. Then generate the
graphs ### 

## INSERT FOR GRAPHS NEW MATERIAL

#Use "par(mfrow= 3,1) " to set up a report with 3 graphs in one columns (3 X
1 matrix) 

par(mfrow=c(3,1))

plot(Results,main=myV)
# The following give lines at plus or minus 3 MAD units and + or – 15% from
median
abline(h=amed, lty="dashed")
abline(h=m2aMAD)
abline(h=p2aMAD)
abline(h=m4aMAD, lty ="dotted")
abline(h=p4aMAD, lty ="dotted")

hist(Results)
boxplot(Results, horizontal=TRUE)

### I see the graphs generated on the R GUI screen, but how do I get them
into individual .emf files?
 ### I think I need something like the following, but how do I substitute
"myV"  instead of "Graphs.emf"  into the file name?

dev.copy(win.metafile, file = "C:\\Documents and Settings\\jfloren\\My
Documents\\TestRSoil\\Graphs.emf", width = 7, height = 10, pointsize = 12)

dev.off()

Thanks,

Jerry Floren
Minnesota Department of Agriculture




 
-- 
View this message in context: http://n4.nabble.com/Writing-Graphs-to-Files-tp1311709p1311709.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list