Note that there are some explanatory texts on larger screens.

plurals
  1. POUse R to create differently sized graphs with same text size
    primarykey
    data
    text
    <p>I need to create small and large versions of several graphs such that the text size is the same. I'd like to avoid fiddling with low-level graphical parameters, but so far that's the only thing that's worked for me, and even that hasn't worked perfectly.</p> <p>Here's what I have now. It basically relies on setting the cex for the smaller graphs to the same ratio as that of the smaller image to the larger image size, and then setting the cex for all text appearing in the smaller graphs to the inverse of that ratio.</p> <pre><code># actual data to be plotted x = seq( 1, 10, 0.01 ) B = 1.0 a = 1.0 y = B*(x^((-1)*a)) # set graphical parameters xlim = c(0,10) ylim = c(0,5) xlab = "Number of repetitions" ylab = "Time in seconds" version = "large" # or "small" if ( version=="large" ) { w = 800 h = 510 } else if ( version=="small" ) { w = 266 h = 170 } # create the actual plots # I tried to remove unnecessary details but leave those that might matter for my question png( paste(version,".png",sep=""), width=w, height=h ) par( las=1 ) if ( version=="large" ) { plot( 0, 0, xlim=xlim, ylim=ylim, xlab=xlab, ylab=ylab, col=rgb(0,0,0,0) ) lines(spline(x,y, method='n', n=1000), lwd=3 ) } else if ( version=="small" ) { par( cex=1/3, mar=c(8,8,1,1) ) plot( 0, 0, xlim=xlim, ylim=ylim, col=rgb(0,0,0,0), axes=FALSE, xlab="", ylab="", frame.plot=TRUE ) lines(spline(x,y, method='n', n=1000), lwd=2 ) axis( side=1, cex.axis=3, mgp=c(5.5,2.5,0) ) title( xlab=xlab, cex.lab=3, mgp=c(5.5,2.5,0) ) axis( side=2, cex.axis=3, mgp=c(5,2,0) ) title( ylab=ylab, cex.lab=3, mgp=c(5,2,0) ) } dev.off() </code></pre> <p>This works OK but I had to spend a lot of time hand-coding the character expansions and margins, and even after I finished doing that, it still only solves my problem for the specific example shown. When I change almost any detail, e.g. if the labels on the y-axis become wider than they are now because they are 2 digits long instead of 1, I have to go back and handcraft new settings again. It's horrible! How can I make this happen automatically without having to handcraft low-level details?</p> <p>By the way, I'm eventually going to want to make the plot lines antialiased so I'd appreciate it if the proposed solution doesn't make that impossible. E.g. I saw somewhere that I can use cairoDevice to antialias, and might be able to use ggplot to resolve my sizing issues, but not sure whether I can do both at the same time? Need to antialias might be removed by using pdf instead of png but I want to display as images in a webpage so I think I need png. Saving large and displaying small does not work well for png format as I understand.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload