-->

(R) Axis widths in gbm.plot

2019-09-06 17:13发布

问题:

Hoping for some pointers or some experiences insight as i'm literally losing my mind over this, been trying for 2 full days to set up the right values to have a function spit out clean simple line plots from the gbm.plot function (packages dismo & gbm).

Here's where I start. bty=n in par to turn off the box & leave me with only left & bottom axes. Gbm.plot typically spits out one plot per explanatory variable, so usually 6 plots etc, but I'm tweaking it to do one per variable & looping it. I've removed the loop & lots of other code so it's easy to see what's going on.

png(filename = "whatever.png",width=4*480, height=4*480, units="px", pointsize=80, bg="white", res = NA, family="", type="cairo-png")
par(mar=c(2.6,2,0.4,0.5), fig=c(0,1,0.1,1), las=1, bty="n", mgp=c(1.6,0.5,0))
  gbm.plot(my_gbm_model,
         n.plots=1,
         plot.layout = c(1,1),
         y.label = "",
         write.title=F,
         variable.no = 1, #this is part of the multiple plots thing, calls the explanatory variable
         lwd=8,  #this controls the width of the main result line ONLY
         rug=F)
dev.off()

So this is what the starting condition looks like. Aim: make the axes & ticks thicker. That's it.

Putting "lwd=20" in par does nothing.

Adding axes=F into gbm.plot() turns the axes and their numbers off. So I conclude that the control of these axes is handled by gbm.plot, not par. Here's where it get's frustrating and crap. Accepted wisdom from searches says that lwd should control this but it only controls the wiggly centre line as per my note above. So maybe I could add axis(side=1, lwd=8) into gbm.plot() ?

It runs but inexplicably adds a smoother! (which is very thin & hard to see on the web but it's there, I promise). It adds these warnings:

In if (smooth & is.vector(predictors[[j]])) { ... :
  the condition has length > 1 and only the first element will be used

Fine, R's going to be a dick for seemingly no reason, I'll keep plugging the leaks as they come up. New code with axis as before and now smoother turned off:

png(filename = "whatever.png",width=4*480, height=4*480, units="px", pointsize=80, bg="white", res = NA, family="", type="cairo-png")
    par(mar=c(2.6,2,0.4,0.5), fig=c(0,1,0.1,1), las=1, bty="n", mgp=c(1.6,0.5,0))
      gbm.plot(my_gbm_model,
             n.plots=1,
             plot.layout = c(1,1),
             y.label = "",
             write.title=F,
             variable.no = 1,
             lwd=8,
             rug=F,
             smooth=F,
             axis(side=1,lwd=8))
    dev.off()

Gives error:

Error in axis(side = 1, lwd = 8) : plot.new has not been called yet

So it's CLEARLY drawing axes within plot since I can't affect the axes from par and I can turn them off in plot. I can do what I want and make one axis bold, but that results in a smoother and warnings. I can turn the smoother off, but then it fails because it says plot.new hadn't been called. And this doesn't even account for the other axis I have to deal with, which also causes the plot.new failure if I call 2 axis sequentially and allow the smoother.

Am I the butt of a big joke here, or am I missing something obvious? It took me long enough to work out that par is supposed to be before all plots unless you're outputting them with png etc in which case it has to be between png & plot - unbelievably this info isn't in ?par. I know I'm going off topic by ranting, sorry, but yeah, 2 full days. Has this been everyone's experience of plotting in R?

I'm going to open the vodka in the freezer. I appreciate I've not put the full reproducible code here, apologies, I can do if absolutely necessary, but it's such a huge timesuck to get to reproducible stage and I'm hoping someone can see a basic logical/coding failure screaming out at them from what I've given.

Thanks guys.

EDIT: reproducibility core data csv: https://drive.google.com/file/d/0B6LsdZetdypkWnBJVDJ5U3l4UFU (I've tried to make these data reproducible before and I can't work out how to do so) samples<-read.csv("data.csv", header = TRUE, row.names=NULL) my_gbm_model<-gbm.step(data=samples, gbm.x=1:6, gbm.y=7, family = "bernoulli", tree.complexity = 2, learning.rate = 0.01, bag.fraction = 0.5))

回答1:

Here's what will widen your axis ticks:

  ..... , lwd.ticks=4 , ...

I predict on the basis of no testing because I keep getting errors with what limited code you have provided) that it will get handled correctly in either gbm.plot or in a subsequent axis call. There will need to be a subsequent axis call, two of them in fact (because as you noted 'lwd' gets passed around indiscriminately):

png(filename = "whatever.png",width=4*480, height=4*480, units="px", pointsize=80, bg="white", res = NA, family="", type="cairo-png")
    par(mar=c(2.6,2,0.4,0.5), fig=c(0,1,0.1,1), las=1, bty="n", mgp=c(1.6,0.5,0))
      gbm.plot(my_gbm_model,
             n.plots=1,
             plot.layout = c(1,1),
             y.label = "",
             write.title=F,
             variable.no = 1,
             lwd=8,
             rug=F,
             smooth=F,  axes="F", 
             axis(side=1,lwd=8))

      axis(1, lwd.ticks=4, lwd=4) 
           # the only way to prevent `lwd` from also affecting plot line
      axis(2, lwd.ticks=4, lwd=4)
    dev.off()

This is what I see with a simple example:

png(); Speed <- cars$speed
Distance <- cars$dist
plot(Speed, Distance,
     panel.first = lines(stats::lowess(Speed, Distance), lty = "dashed"),
     pch = 0, cex = 1.2, col = "blue", axes=FALSE)
 axis(1, lwd.ticks=4, lwd=4) 
      axis(2, lwd.ticks=4, lwd=4)
dev.off()