Re: [R] problem with legend on other graphics devices
On Fri, 2006-06-16 at 19:42 +0100, Gavin Simpson wrote: Hi, I have a bit of a problem with a legend in png, eps and pdf plots produced from a custom plotting function. I was writing a little function to produce some stock plots for a routine analysis conducted in our lab. I have a wrapper function figures() (see below) that produces eps, png and pdf versions of the lab output. When I draw the plot on the X11() device the correct amount of space is made available for the legend, but when I draw the plot directly on the png, postscript or pdf devices, the legend is placed too close to the edge of the plot and is clipped. Hi, To follow up, the posted code does work and the legend is not clipped on my home machine but that was a slightly less-up-to-date version of R 2.3.1 patched than the machine at work. So I compiled the latest R2.3.1 patched (svn revision 38350) and it works fine there also. So must have been something peculiar to my set-up at work or R session at the time. For the record: version _ platform i686-pc-linux-gnu arch i686 os linux-gnu system i686, linux-gnu status Patched major 2 minor 3.1 year 2006 month 06 day16 svn rev38350 language R version.string Version 2.3.1 Patched (2006-06-16 r38350) G -- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% *Note new Address and Fax and Telephone numbers from 10th April 2006* %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% Gavin Simpson [t] +44 (0)20 7679 0522 ECRC [f] +44 (0)20 7679 0565 UCL Department of Geography Pearson Building [e] gavin.simpsonATNOSPAMucl.ac.uk Gower Street London, UK[w] http://www.ucl.ac.uk/~ucfagls/cv/ WC1E 6BT [w] http://www.ucl.ac.uk/~ucfagls/ %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] problem with legend on other graphics devices
Hi, I have a bit of a problem with a legend in png, eps and pdf plots produced from a custom plotting function. I was writing a little function to produce some stock plots for a routine analysis conducted in our lab. I have a wrapper function figures() (see below) that produces eps, png and pdf versions of the lab output. When I draw the plot on the X11() device the correct amount of space is made available for the legend, but when I draw the plot directly on the png, postscript or pdf devices, the legend is placed too close to the edge of the plot and is clipped. This example uses the actual test data I'm using to write the functions. figures() calls the dating plot method I wrote to do the plotting, and dating() is a wrapper for read.csv() that creates a dating object with the lab analysis results and some extra meta data. All the R code is in dating.R at the URL below. felb5 - dating(http://ecrc3.geog.ucl.ac.uk/download/dating/felb5.csv;, cs = 1963, csError = 5, csDepth = 22.5, csDepthError = 2) source(http://ecrc3.geog.ucl.ac.uk/download/dating/dating.R;) plot(felb5, as.Age = TRUE) figures(felb5, felbrig5, as.Age = TRUE) As you can see, in the generated png, eps and pdf files the legend is clipped, but it displays fine on the screen. From the list archives, I seem to be doing things in the recommended way (not using dev.copy() etc.) What can I do to force sufficient space for the legend in the pdf, postscript and png plots? Many thanks, Gav figures - function(object, filename, eps = TRUE, png = TRUE, pdf = TRUE, inch.width = 6, inch.height = 6, horizontal = TRUE, pixel.width = 600, pixel.height = 600, paper = special, onefile = FALSE, pointsize = 12, ...) { if(eps) { postscript(paste(filename, .eps), width = inch.width, height = inch.height, onefile = onefile, paper = paper, pointsize = pointsize, horizontal = horizontal) plot(object, ...) invisible(dev.off()) } if(png) { png(paste(filename, .png), width = pixel.width, height = pixel.height, pointsize = pointsize) plot(object, ...) invisible(dev.off()) } if(pdf) { pdf(paste(filename, .pdf), width = inch.width, height = inch.height, onefile = onefile, paper = paper, pointsize = pointsize) plot(object, ...) invisible(dev.off()) } } -- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% * Note new Address, Telephone Fax numbers from 6th April 2006 * %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% Gavin Simpson ECRC ENSIS [t] +44 (0)20 7679 0522 UCL Department of Geography [f] +44 (0)20 7679 0565 Pearson Building [e] gavin.simpsonATNOSPAMucl.ac.uk Gower Street [w] http://www.ucl.ac.uk/~ucfagls/cv/ London, UK. [w] http://www.ucl.ac.uk/~ucfagls/ WC1E 6BT. %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] problem with legend
I have problem with legend command. Please look this script: dcbv.fm Time Series: Start = 1980 End = 2002 Frequency = 1 [1] 2994.023 3388.414 3111.762 2990.967 3077.438 3058.274 3049.934 2974.130 [9] 2889.659 2801.790 2631.391 2661.700 2312.526 2518.968 2567.044 2443.952 [17] 2117.638 2042.461 2025.816 1939.560 1640.775 1583.609 1659.912 dcbv.ms Time Series: Start = 1980 End = 2002 Frequency = 1 [1] 3700.239 4076.438 3856.495 3680.345 3871.887 3789.770 3831.173 3768.876 [9] 3585.572 3754.374 3372.859 3419.667 3185.194 3319.215 3445.845 3265.214 [17] 2773.961 2661.904 2669.835 2569.190 2187.719 2217.756 2196.378 plot(dcbv.ms,ylim=c(min(dcbv.fm),max(dcbv.ms))) lines(dcbv.fm,col=2) legend(1984,2500,c(DCVB-MS,DCBV-FM),col=c(1,2),cex=.6,fill=T) At end of script the legend of plot have only one color: black. I think the legend will have two colors: black and red. Where I make mistake? version _ platform i386-pc-mingw32 arch i386 os mingw32 system i386, mingw32 status major2 minor0.1 year 2004 month11 day 15 language R Thanks in advance Bernardo Rangel Tura, MD, MSc National Institute of Cardiology Laranjeiras Rio de Janeiro Brazil -- No virus found in this outgoing message. Checked by AVG Anti-Virus. __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] problem with legend
Bernardo Rangel Tura wrote: I have problem with legend command. Please look this script: dcbv.fm Time Series: Start = 1980 End = 2002 Frequency = 1 [1] 2994.023 3388.414 3111.762 2990.967 3077.438 3058.274 3049.934 2974.130 [9] 2889.659 2801.790 2631.391 2661.700 2312.526 2518.968 2567.044 2443.952 [17] 2117.638 2042.461 2025.816 1939.560 1640.775 1583.609 1659.912 dcbv.ms Time Series: Start = 1980 End = 2002 Frequency = 1 [1] 3700.239 4076.438 3856.495 3680.345 3871.887 3789.770 3831.173 3768.876 [9] 3585.572 3754.374 3372.859 3419.667 3185.194 3319.215 3445.845 3265.214 [17] 2773.961 2661.904 2669.835 2569.190 2187.719 2217.756 2196.378 plot(dcbv.ms,ylim=c(min(dcbv.fm),max(dcbv.ms))) lines(dcbv.fm,col=2) legend(1984,2500,c(DCVB-MS,DCBV-FM),col=c(1,2),cex=.6,fill=T) So you want filles boxes? Then you should specify the color in the fill argument: legend(1984, 2500, c(DCVB-MS, DCBV-FM), cex=.6, fill=1:2) or do you want some lines? legend(1984, 2500, c(DCVB-MS, DCBV-FM), cex=.6, col=1:2, lwd=2) Uwe Ligges At end of script the legend of plot have only one color: black. I think the legend will have two colors: black and red. Where I make mistake? version _ platform i386-pc-mingw32 arch i386 os mingw32 system i386, mingw32 status major2 minor0.1 year 2004 month11 day 15 language R Thanks in advance Bernardo Rangel Tura, MD, MSc National Institute of Cardiology Laranjeiras Rio de Janeiro Brazil __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html