使用矩阵列和行名称标记绘图中的x和y轴值

时间:2014-03-24 19:57:45

标签: r

我有以下矩阵然后按照如下所示的间隔进行绘制

mdat <- matrix(c(0.25,0.45,0.3, 0.75,0.15,0.62,0.40,0.90,1, 0.45,0.15,0), nrow = 4, ncol = 3, byrow = TRUE) 



plot(rep(1:4, 3), mdat, pch=15, cex=2.5,
                   col=c("red","orange","blue", "green")[findInterval(mdat, c(0,.25,.5,.75, 1.1))])

它的工作正常,如图所示。

但现在如果我定义

dimnames(mdat) <- list(  c("row1", "row2", "row3","row4"), c("col1", "col2", "col3"))

我需要在图中更改x轴和y轴中的值为row1..row4而不是值0.0 .. 1.0和col1 .. col3而不是1.0 .. 4.0 enter image description here

1 个答案:

答案 0 :(得分:1)

您是否考虑过使用ggplot进行此操作?只是一个建议......

library(ggplot2)
mdat <- data.frame(Y = c(0.25,0.45,0.3, 0.75,0.15,0.62,0.40,0.90,1, 0.45,0.15,0),
                   X = factor(c(rep("row1",3), rep("row2",3), rep("row3",3), rep("row4",3))))
mdat$Z <- factor(findInterval(mdat$Y, c(0,.25,.5,.75, 1.1)))
P <- ggplot(mdat, aes(x = X, y = Y)) + geom_point(aes(colour = Z), size = 11)
P + theme(axis.text.y = element_text(size = 20), axis.text.x = element_text(size = 20))

enter image description here