plot - Matrix diagram in r -
i create such diagram in r:
i have such matrix
[1] [2] [3] [4] [5] .... [30] [1] 0.5 0.75 1.5 0.25 2.5 .... 0.51 [1] 0.84 0.24 3.5 0.85 0.25.... 1.75 [1] 0.35 4.2 0.52 1.5 0.35.... 0.75 . . ....................................... . [30]0.84 1.24 0.55 1.5 0.85.... 2.75
and want have diagram,
- if value less 1 ----> green circle
- if value between 1 , 2 ----> yellow circle
- more 2 ----> red circle
is there packages or method in r job? how can that?
to plot this, need 3 data points:
x, y, color
thus, first step reshaping.
fortunately, matricies vector, dimension attribute, need create data.frame of x,y coordinates. expand.grid
.
# create sample data. mat <- matrix(round(runif(900-30, 0, 5),2), 30)
create (x, y) data.frame.
notice y
seq of rows , x
seq of columns
dat <- expand.grid(y=seq(nrow(mat)), x=seq(ncol(mat))) ## add in values matrix. dat <- data.frame(dat, value=as.vector(mat)) ## create column appropriate colors based on value. dat$color <- cut( dat$value, breaks=c(-inf, 1, 2, inf), labels=c("green", "yellow", "red") ) ## plotting library(ggplot2) ggplot(data=dat, aes(x=x, y=y)) + geom_point(color=dat$color, size=7)
Comments
Post a Comment