rmcelreath / rethinking

Statistical Rethinking course and book package
2.14k stars 600 forks source link

Plot(precis()) opposite directions for pars/labels on y axis #142

Open lottybrand opened 6 years ago

lottybrand commented 6 years ago

Not sure if this is on purpose but it strikes me as a little strange: when using plot(precis()) and specifying the pars, and then relabelling the pars, the labels go from bottom to top whereas the pars read from top to bottom, meaning you have to write them the opposite way around, so instead of writing:

plot(precis(Model1), pars = c("a", "b", "c", "d"), labels = c("Ape", "Bear", "Cat", "Dog"))

you have to write:

plot(precis(Model1), pars = c("a", "b", "c", "d"), labels = c("Dog", "Cat", "Bear", "Ape"))

Which is kind of clunky?

rmcelreath commented 6 years ago

Yeah, it's no bueno. Sorry. I thought I had fixed that in Experimental branch. But maybe that was a dream?

rmcelreath commented 6 years ago

I patched this in Experimental. I need to do a bunch of other unit tests on my local branch before putting it up here. But if you paste the code below, you should get nicer behavior:

precis_plot <- function( x , y , pars , col.ci="black" , xlab="Value" , add=FALSE , xlim=NULL , labels=rownames(x)[1:n] , ... ) {
    if ( !missing(pars) ) {
        x <- x[pars,]
    }
    n <- nrow(x)
    mu <- x[n:1,1]
    left <- x[[3]][n:1]
    right <- x[[4]][n:1]
    set_nice_margins()
    labels <- labels[n:1]
    if ( is.null(xlim) ) xlim <- c(min(left),max(right))
    if ( add==FALSE )
        dotchart( mu , labels=labels , xlab=xlab , xlim=xlim , ... )
    else
        points( mu[n:1] , n:1 , ... )
    for ( i in 1:length(mu) ) lines( c(left[i],right[i]) , c(i,i) , lwd=2 , col=col.ci )
    if ( add==FALSE ) abline( v=0 , lty=1 , col=col.alpha("black",0.15) )
}
setMethod( "plot" , "precis" , function(x,y,...) precis_plot(x,y,...) )