add ticks to parcoord (parallel coordinates plot)

妖精的绣舞 提交于 2019-12-22 10:58:07

问题


The parcoord function from the MASS package looks quite ok, but how can I add ticks to the four y-axis?

Code is here:

 ir <- rbind(iris3[,,1], iris3[,,2], iris3[,,3])
 parcoord(log(ir)[, c(3, 4, 2, 1)], col = 1 + (0:149)%/%50)


回答1:


Did you try setting var.label=T in the parcoord function? Is that insufficient?

Otherwise i didn't see any easy way to change the axis with the default function. But it turns out that function is pretty short and we can easily steal ideas form it and make our own version that can. Here's one way to modify it

parcoordlabel<-function (x, col = 1, lty = 1,  lblcol="blue",...) 
{
    df <- as.data.frame(x)
    pr <- lapply(df, pretty)
    rx <- lapply(pr, range, na.rm = TRUE)
    x <- mapply(function(x,r) {
            (x-r[1])/(r[2]-r[1])
        },
        df, rx)
    matplot(1L:ncol(x), t(x), type = "l", col = col, lty = lty, 
        xlab = "", ylab = "", axes = FALSE, ...)
    axis(1, at = 1L:ncol(x), labels = colnames(x))
    for (i in 1L:ncol(x)) {
        lines(c(i, i), c(0, 1), col = "grey70")
        text(c(i, i), seq(0,1,length.out=length(pr[[i]])), labels = pr[[i]], 
            xpd = NA, col=lblcol)
    }
    invisible()
}

and you can run it with

ir <- log(rbind(iris3[,,1], iris3[,,2], iris3[,,3]))[,c(3,4,2,1)]
parcoordlabel(ir, col = 1 + (0:149)%/%50)

Now i will admit it's a bit ugly. But hopefully you can understand the pieces well enough to customize how you like



来源:https://stackoverflow.com/questions/23553210/add-ticks-to-parcoord-parallel-coordinates-plot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!