0

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()

this is what the initial situation looks like

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() ?

smooth criminal

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))

dez93_2000
  • 1,730
  • 2
  • 23
  • 34
  • 1
    Post a reproducible example. (I tried building one using the vignette and after several steps ended up getting errors. Seems you are shirking your responsibilities here.) – IRTFM Sep 03 '14 at 00:39
  • Without a sample `gbm` object we cannot reproduce the plot ourselves so we cannot help you troubleshoot. But `axis(side=1,lwd=8)` should never be passed as a parameter to a function. That is a separate function that is meant to be called *after* a `plot()` has been called: `plot(1:3, 1:3, xaxt="n"); axis(1, lwd=7)`, for example. But `gbm.plot` may have a different way of dealing with that. – MrFlick Sep 03 '14 at 00:49
  • Often when you take the time to create a minimal, reproducible example, you can easily find the mistake yourself. Not doing so before posting a question is really a waste of everyone's time. The key words are "minimal" and "reproducible". Often R help pages are a great starting point for such examples. This is a basic skill you should develop no matter what language you program in. – MrFlick Sep 03 '14 at 00:53
  • cheers for the help guys. MrFlick: in principle yes, but I tried this last time and spent 2 hours trying to debug why my attempt to create a minimal analog to my core data didn't run in gbm. Same min/max, similar means, same n, etc.. I was just hoping that someone would see the code and go "oh yeah - you need "laxt()" or something like that. – dez93_2000 Sep 03 '14 at 01:10
  • Well I had a "well, why don't you just do ...X" inmpulse, but I prefer to test my answers. I've been burned too many times by my own errors. You should be able to construct a working example from the vignette easier that I can. – IRTFM Sep 03 '14 at 01:24
  • sorry man, really hitting walls here. Googled vignettes, suggested it was the supplementary info, so this [http://cran.r-project.org/web/packages/dismo/vignettes/brt.pdf] and this [http://cran.r-project.org/web/packages/dismo/vignettes/sdm.pdf] from this [http://cran.r-project.org/web/packages/dismo/index.html]? Neither have anything useful; please let me know if i'm misinterpreting vignette. And hey, if you still have that hunch, by all means throw it out there. – dez93_2000 Sep 03 '14 at 01:54

1 Answers1

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()

enter image description here

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • Cheers. Feels like gbm.plot is causing the strange behaviour. I'll have a test when I'm up. – dez93_2000 Sep 03 '14 at 06:23
  • I don't think I would blame `gbm.plot`. It's passing most of the plotting parameters as well as most hard-coded plotting functions do. There no distinction between `lwd` and `lwd.line` in the base plotting functions. – IRTFM Sep 03 '14 at 06:28
  • Thanks for the help. By doing the extra axis() lines and removing the existing ones, the axis labels were just 20% increments. This got me looking into the function again & I realised that gbm.plot is just a fancy wrapper around plot.gbm, so I'm not calling that directly. Unfortunately now I can't REMOVE the yaxis labels, so that's the next problem...! – dez93_2000 Sep 04 '14 at 20:33