Matrix diagram in r

I would like to create a diagram like this in r:

enter image description here

I have such a 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 chart,

  • if the value is less than one ----> green circle
  • if the value is between one and two ----> the yellow circle
  • more than two ----> red circle

Are there any packages or methods in r to do this job? How can i do this?

+4
source share
3 answers

To build this, you will need three data points:

x, y, color

Thus, the first step is restructuring.
Fortunately, matricies is already a vector, just with a dimension attribute, so we just need to create data.frame x, y coordinates. We do this with help expand.grid.

# create sample data. 
mat <- matrix(round(runif(900-30, 0, 5),2), 30)

(x, y) data.frame.
, y - seq x seq

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)

sample output

+6

, corrplot package.

corrplot , . . , corrplot , , , , ..

@RicardoSaporta.

library(corrplot)

#sample data
mat <- matrix(round(runif(900, 0, 5),2), 30)

#plot
corrplot(mat, is.corr = FALSE)

enter image description here

+1

, image:

mat <- matrix(round(runif(900-30, 0, 5),2), 30)
image(mat,
      breaks=c(min(mat),1,2,max(mat)), #image can't handle Inf but that not a problem here
      col=c("green","yellow","red"), axes=FALSE)

enter image description here

, :

grid <- expand.grid(1:nrow(mat),1:ncol(mat)) #Same two steps as in Ricardo Sapporta answer
category <- cut(mat,c(-Inf,1,2,Inf))
plot(grid,                  #Then plot using base plot
     col=c("green","yellow","red")[category], #here is the trick
     pch=19, cex=2, axes=FALSE) #You can customize it as you wish

enter image description here

+1

Source: https://habr.com/ru/post/1542953/


All Articles