[R] losing x-label when exporting to PNG

Marc Schwartz (via MN) mschwartz at mn.rr.com
Thu Apr 27 18:14:32 CEST 2006


On Thu, 2006-04-27 at 08:43 -0400, John Kane wrote:
> I have a simple barplot that looks fine in the R
> graphics device window. However when I export it to
> png I am losing the x-label.  It must be an obvious
> problem but I cannot see it. Trying to resize the plot
> does not seem to  help.  Code is below.
> 
> Any help gratefully received.
> 
> ###### Start Code############
> 
> Groups <-c(21.8,45,  43, 17.2, 8.3,  18)
> names(Groups) <- c("Exeter", "Halifax", "Moosonee",
> "Ottawa",
>           "Montréal", "Saskatoon")
> # Here we are setting the font to Bold and addin some
> lines to the bottom
> # margin of the graph, 6 vs default of 5, to give us
> more room with the angled
> # labels.
> 
> par(font=2,mar= (c(6, 4, 4, 2) + 0.1))
> 
> Mycolours <- c("red", "blue", "green", "yellow",
> "orange" ,"purple")
> 
> # --------------plot to screen
> --------------------------------------
> mp <- barplot(Groups, beside=T ,
>       horiz=F , las=1, ylim=c(0,60),  axisnames=F,
> font.lab=2, col=Mycolours
>       )
> text(mp, par("usr")[3] - 1.5, srt = 45, adj = 1,
> labels = names(Groups), xpd = T ,
>      cex=.75,
>    )
>    
> mtext(side = 1, line=5, text="% of  Services above
> thresholds ")
> mtext(side=2, line=2.5, text="Percent")
> title(main="")
> box()
>  # -------------------plot to png
> file------------------------------
> png('gr_%04d.png', width=600, height=400)
> mp <- barplot(Groups, beside=T ,
>       horiz=F , las=1, ylim=c(0,60),  axisnames=F,
> font.lab=2, col=Mycolours
>       )
> text(mp, par("usr")[3] - 1.5, srt = 45, adj = 1,
> labels = names(Groups), xpd = T ,
>      cex=.75,
>    )
>    
> mtext(side = 1, line=5, text="% of  Services above
> thresholds ")
> mtext(side=2, line=2.5, text="Percent")
> title(main="")
> box()
> 
> dev.off()
> 
> ####   End code #####

John,

I suspect that you might want to find a cushioned wall first, in case
you might want to bang your head against it...   ;-)

If your PNG file is created using exactly the code you have above, you
have neglected to include the par() statement to adjust the margins
AFTER calling png().  Note that the prior par() statement is apropos to
your screen plot device and will not affect the png() device.

So insert the same par() call AFTER the png() call and you should be
good to go.

HTH,

Marc Schwartz




More information about the R-help mailing list