Skip to content
Prev 172623 / 398506 Next

problems with exporting a chart

Dear Uwe, 
Thank you very much for your email. I think I have worked out that the problem was related to the coordinates of the legend that are manually specified in the leg_loc command. However, I'm not exactly sure what was wrong with exporting the picture of the plot...  

To avoid the problem I have to run plot.new() before running the histogram command to refresh the default parameters of the Device window (that might have changed after plotting previous charts or changing the size of the device window or some other reasons I don't know of...) 

I'm sorry that I wasn't clear enough in my question. I'll try to be this time and give you an idea of what happened.

If there is one legend, keywords like "topleft", "rightleft" etc are very good, but they don't work with several legends to be placed on each individual sub-plot. Maybe you can suggest a better way of placing multiple legends on the charts that would automatically detect the coordinates for each legend???

R version 2.8.0
Lattice version 0.17-15

With regards to format and device, I've tried many ways:
- Use the (Windows) device directly, then once the chart is ready I either copy it as a metafile and then paste to a document, or try to save it as pdf/png/jpeg etc OR
- Use pdf / png / jpeg functions to directly save the output to an external file

This is the code (which works now):

*To generate a similar data frame to the one I use:
data=data.frame("Size"=rep(c(60,70,150,250, 450),each=500), "Delta_R2"=rnorm(2500,mean=0,sd=1)) 
attach(data)

library(lattice)
plot.new() # I run it here to restore the default par options 

histogram(~Delta_R2|as.factor(Size), type="percent", col="red", xlab="Delta OLS - SV R squared", main="R Squared Deviations")
leg_loc=matrix(c(-0.05, 0.3, 0.65, -0.05, 0.3, 0.4, 0.4, 0.4, 0.98, 0.98),ncol=2, nrow=5, byrow=FALSE) # specifying the coordinates of the legends
for (i in 1:5) {
nR=(i-1)*500+1
nR2=nR+499 
z=data[nR:nR2,2]

m<-mean(z)
std<-sqrt(var(z))
iqr=IQR(z)
median=median(z)
legend(leg_loc[i,1],leg_loc[i,2], cex=0.7,
legend= paste(
"Mean=",round(m,3),'\n',
"SD=",round(std,3),'\n',
"Median =",round(median,3),'\n',
"IQR=", round(iqr,3)),bty="n")
}

Then I copy it or save as pdf / png / jpeg etc...

Thanks a lot for getting back to me regarding this!

Best regards, 

Elena Wilson
DBM Consultants Pty Ltd
5-7 Guest Street, Hawthorn, Victoria 3122, Australia
T: (61 3) 9819 1555
www.dbmconsultants.com

Please consider the environment before printing this email.

NOTICE - The information contained in this email may be confidential and/or privileged. You should only read, disclose, re-transmit, copy, distribute, act in reliance on or commercialise the information if you are authorised to do so. If you receive this email communication in error, please notify us immediately by email to dbm at dbmcons.com.au, or reply by email direct to the sender and then destroy any electronic or paper copy of this message.

 -----Original Message-----
From: 	Uwe Ligges [mailto:ligges at statistik.tu-dortmund.de] 
Sent:	Wednesday, 4 March 2009 9:42 PM
To:	Elena Wilson
Cc:	r-help at r-project.org
Subject:	Re: [R] problems with exporting a chart

Please read the posting guide which asks you to answer basic questions 
such as:

Which R / lattice versions are we talking about?
Which is the "any" format?
Are you using the Devices directly or are you using some other way to 
copy contents of one device into another device?
What is the exact, minimal code (including data!) that reproduces your 
problem? It would be nice if we could copy and paste in it work on our 
machines.
Why do you call plot.new()?

Uwe Ligges
Elena Wilson wrote: