Re: [Rd] one way to solve bad looking density plots in postscript

From: baptiste auguie <baptiste.auguie_at_googlemail.com>
Date: Thu, 04 Aug 2011 09:14:46 +1200

Hi,

I was going to suggest panel.last to redraw a box around the image, but for some reason it does not seem to come after the image neither (this is perhaps to be expected from the note in ?plot.default).

plot(1,1, panel.last={box(lwd=50, col="#0000FF")})

image(volcano, panel.last={box(lwd=50, col="#0000FF")})

sessionInfo()
R version 2.13.1 (2011-07-08)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)

locale:
[1] en_NZ.UTF-8/en_NZ.UTF-8/C/C/en_NZ.UTF-8/en_NZ.UTF-8

attached base packages:
[1] stats graphics grDevices utils datasets grid methods base

other attached packages:
[1] ggplot2_0.8.9 proto_0.3-9.2 reshape_0.8.4 plyr_1.6

loaded via a namespace (and not attached): [1] tools_2.13.1

Best,

baptiste

On 4 August 2011 00:23, Michael Lachmann <lachmann_at_eva.mpg.de> wrote:
> Some more digging.
>
> 1.
> The following code will fix density plots for me:
> ---
> .ps.prolog=grDevices:::.ps.prolog
> i=grep("/p2",.ps.prolog)
> .ps.prolog[i] = "/p2 { bg gsave fill grestore 0.001 setlinewidth stroke
> newpath } def"
> ---
>
> 2.
> It seems that it doesn't matter where in image.default() the box is drawn, before or after
> .Internal(image(as.double(x), as.double(y), as.integer(zi),
>        col))
> is called, the resulting eps has the box in front.
>
> Michael
>
> On 3 Aug 2011, at 9:25AM, Michael Lachmann wrote:
>
>> When R generates density plots and these are exported to postscript(
>> a=matrix(1:100,10,10);image(a,col=rainbow(100);dev.copy2eps(file="image.eps")
>> )
>> The result often looks bad when rendered on screen. The help page states that this is because programs use anti-aliasing. That seems to be true - turning off anti-aliasing for gs (-dGraphicsAlphaBits=1) of in OSX's preview makes the plots look really smooth, but makes everything else look bad (personal opinion..). The plots do look ok in acrobat reader. I think it would be much better if R corrected this problem - even if it is not totally R's fault.
>> It seems that using the option useRaster=T in image() solved this problem, but creates other problems for OSX's Preview (it seems that OSX's preview first anti-aliases the raster, and then scales it... creating a mess).
>>
>> Density plots produced by gnuplot do not seem to have this problem:
>> ---
>> set pm3d map
>> set pm3d at b
>> set ticslevel 0.8
>> set isosample 40,40
>> set output "gtest.eps"
>> set term postscript eps color
>> splot [-3:3] [-3:3] x*x*exp(-x*x)*y*y*exp(-y*y)
>> --
>> But I haven't figured out why that is. Maybe someone who understands more about postscript can. Maybe it is something about the order that the rectangles are rendered? I did notice that rectangles are plotted with slightly different sizes - 50, 51, 50, 51 and so on. Is that it?
>>
>> After a lot of experimentation, I found that a small change in the eps file can correct the output.
>> If in the eps file produced above, you change the line
>> /p2  { gsave bg fill grestore newpath } def
>> with
>> /p2  { bg gsave fill grestore stroke newpath } def
>> and add
>> 0.0001 setlinewidth
>> two lines before the next p2 - i.e. before we start plotting the rectangles of the image:
>> change:
>> /bg { 1 0 0 setrgb } def
>> 59.04 73.44 41.47 37.15 r p2
>> to:
>> 0.0001 setlinewidth
>> /bg { 1 0 0 setrgb } def
>> 59.04 73.44 41.47 37.15 r p2
>>
>> What this does is plot the outline of each rectangle in addition to filling it.
>> The page at:
>> http://pages.uoregon.edu/noeckel/MathematicaGraphics.html#ExportGraphics
>> Claims that Mathematica also has/had this problem, and how it can be solved there (sadly, I don't know enough Mathematica to understand that solution)
>>
>> The output of the image would look even better if the axes were drawn AFTER the density rectangles, not before. That would cause the rectangles not to overwrite part of the lines of the axes. But that is probably a change in the image() routine, not in the postscript driver....
>>
>> Thanks for listening,
>>
>> Michael Lachmann
>>
>> ______________________________________________
>> R-devel_at_r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>
>>
>>
>
> ______________________________________________
> R-devel_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>



R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Wed 03 Aug 2011 - 21:17:35 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 04 Aug 2011 - 00:50:14 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive