Matrix diagram in r
plot, r
Solution
to plot this, you need three data points:
x, y, color
Thus, first step is reshaping. Fortunately, matricies are already a vector, simply with a dimension attribute, so we just need to create a data.frame of x,y coordinates. We do this with `expand.grid`.
# create sample data.
mat <- matrix(round(runif(900-30, 0, 5),2), 30)
create the (x, y) data.frame. Notice that `y` is the seq of rows and `x` the seq of columns
dat <- expand.grid(y=seq(nrow(mat)), x=seq(ncol(mat)))
## add in the values from the matrix.
dat <- data.frame(dat, value=as.vector(mat))
## Create a column with the appropriate colors based on the 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)
Problem
I would like to create such a diagram in r: I have a 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 I want to have a diagram, - if the value less than one ----> green circle - if the value between one and two ----> yellow circle - more than two ----> red circle Is there any packages or method in r to do this job? how can I do that?