Elena Wilson wrote:
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
There is also a win.metafile(), by the way.
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")
}
You probably do not want the legend function that is intended to works
with base graphics. Instead, use the "key" argument for your lattice
function histogram() as described in the help page ?xyplot.
Example:
histogram( ~ Delta_R2 | as.factor(Size), type="percent", col="red",
xlab="Delta OLS - SV R squared", main="R Squared Deviations",
panel = function(x, ...){
panel.histogram(x, ...)
draw.key(list(cex=0.7, text=list(lab=c(paste(c('Mean', 'SD',
'Median', 'IQR'), round(c(mean(x), sd(x), median(x), IQR(x)), 3),
sep="=")))),
draw = TRUE,
vp = viewport(x = unit(0.25, "npc"), y = unit(0.9,
"npc")))
})
Next, you probably do not want to copy but just re-print into an
appropriate device.
Uwe Ligges
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 d...@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:lig...@statistik.tu-dortmund.de]
Sent: Wednesday, 4 March 2009 9:42 PM
To: Elena Wilson
Cc: r-help@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:
Dear R helpers,
I have a problem with exporting a chart (to any format). The graphic device
becomes inactive and I get the 'Error: invalid graphics state' error message. I
searched the help, web and FAQ but couldn't find the solution.
This is my code:
I chart a histogram for differences in R2 by sample size (an extract from the
data is below). Altogether I have n=2500 observations (n=500 per sample size)
Size; Delta_R2
60; 0.0073842
60; 0.0007156
...
70; 0.0049717
70; 0.0121892
...
150; 0.0139615
150; 0.0088114
...
250; 0.0027976
250; 0.0109080
...
450; 0.0050917
450; 0.0088114
...
The histogram works ok and I can save or copy to pdf/jpeg/png etc with no
problems
library(lattice)
plot.new()
histogram(~Delta_R2|as.factor(Size), type="percent", col="red", xlab="Delta OLS - SV R squared", main="R Squared Deviations")
Once I put the legends (5 text boxes) on the chart and I try to save or copy it
as pdf / jpeg/png etc I get the above mentioned error message.
This is the code for adding the legends:
*The locations of the legends for each chart
leg_loc=matrix(c( -0.1, 0.26, 0.62, -0.1, 0.26, 0.4, 0.4, 0.4, 1, 1),ncol=2,
nrow=5, byrow=FALSE)
*Calculate the statistics for each sample size to display on the legends
for (i in 1:5) {
nR=(i-1)*500+1
nR2=nR+499
z=data[nR:nR2,13]
m<-mean(z)
std<-sqrt(var(z))
iqr=IQR(z)
median=median(z)
*Adding to the chart
legend(leg_loc[i,1],leg_loc[i,2], legend= paste(
"Mean=",round(m,3),'\n',
"SD=",round(std,3),'\n',
"Median =",round(median,3),'\n',
"IQR=", round(iqr,3)),bty="n")
}
Do you know why it is happening?
Thank you in advance,
Lena
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 d...@dbmcons.com.au, or reply by email direct to the sender and
then destroy any electronic or paper copy of this message.
______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.