Menu

Modifying axis labels on trace plots

runjags
Ed Merkle
2018-09-13
2018-09-13
  • Ed Merkle

    Ed Merkle - 2018-09-13

    Is there a way to modify the axis labels on trace plots? I ran the first regression model in the examples of the run.jags() help file. Then I tried this command:

    plot(results, "trace", trace.options=list(ylab=c("1","2","3")))

    which gives me the label "1 2 3" on all three panels of the plot. I also tried supplying a list to the ylab argument instead of a character vector, and I tried using the panel argument instead of ylab with no luck (but reading the lattice documentation brought back fond memories). Am I missing something simple?

    Thanks,
    Ed

     
  • Matt Denwood

    Matt Denwood - 2018-09-13

    Hi Ed

    Good question - to which the answer is 'kind of'. Or more precisely: 'no - but there is an ugly hack'.

    Before the ylab variable is evaluated (in a function deep inside runjags) the 'col' and 'varname' variables are assigned to the calling environment. So if we can map the current variable name to the desired variable name, then you can use a switch statement wrapped inside an expression statement (we need to force lazy evaluation because 'varname' is not known at the time of the call to plot()). So if we are running the regression model example with variables m, c and precision then we can define a labeller function as:

    label_fun <- function(var) switch(var, m="one", c="two", precision="three", "unknown")
    

    And then pass a call involving this label_fun as the argument to ylab:

    plot(results, 'trace', trace.options=list(ylab=expression(label_fun(varname))))
    

    This works ... but it is not pretty. To be honest, I'm not even sure if the expression(varname) trick is documented anywhere - if not it probably should be.

    Cheers,

    Matt

    ps. As an interesting aside, this doesn't work when the return from swtich is numeric - i.e. compare:

    label_fun <- function(var) switch(var, m=1, c=2, precision=3)
    

    with:

    label_fun <- function(var) as.character(switch(var, m=1, c=2, precision=3))
    

    I have no idea why this is ... if anybody can work this out I would greatfully accept answers on a postcard!

     
  • Ed Merkle

    Ed Merkle - 2018-09-13

    Thanks, that's exactly what I was looking for, and exactly what I would have never figured out by myself!

    Ed

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.