简体   繁体   中英

R: Plotting a grid of values

I have this simple reproducible code that generates some values:

total=0
X=1
for(i in 1:3){
Y=0.1
index=1
index1=1
value=0
values=0
for(i in 1:15){
  value[[index]]=10+X*Y+i
  if(index%%5==0){
    values[[index1]]=mean(value)
    index1=index1+1;
    index=0
    value=0
    Y=Y+1
  }
  index=index+1;
}
total=c(total,values)
X=X+1
}
total=total[-1]

For

X=1 and Y=0.1 it generates 13.1

X=1 and Y=1.1 it generates 19.1

X=1 and Y=2.1 it generates 25.1

X=2 and Y=0.1 it generates 13.2

X=2 and Y=1.1 it generates 20.2

X=2 and Y=2.1 it generates 27.2

X=3 and Y=0.1 it generates 13.3

X=3 and Y=1.1 it generates 21.3

X=3 and Y=2.1 it generates 29.3

So then I get in total 9 values, what I want to do is to plot them in a grid. It should be something like this or similar:

gridOfValues

The X can be at the top or bottom it doesn't matter or the Y, and I want to color them so that the small values have a lighter color and the bigger values a darker one.

Is there any way to do this plot in R? I would very much appreciate any hint. I don't really know how to do it in the simplest way. I can plot an empty grid but I don't know how add the lines and values. Is there maybe any package that does something similar quickly?

Or if anyone could suggest me another type of plot where I could plot X and Y with their values.

I would very much appreciate any suggestion.

Try something like this and play with the colors:

plot_table <- function(d, colors, marginColor,main="", text.cex=1.0){
  plot(c(-1,ncol(d)),c(0,nrow(d)+1), type="n", xaxt="n", yaxt="n", xlab="",ylab="",main=main, bty="n")

  for (c in 1:ncol(d)) {
    rect(c-1, nrow(d), c, nrow(d) + 1, col=marginColor)
    text(c-.5,nrow(d) +.5,colnames(d)[c], cex=text.cex)
  }
  for (r in 1:nrow(d)) {
    rect(-1, r-1, 0, r, col=marginColor)
    text(-.5, r-.5,rownames(d)[nrow(d) - r + 1], cex=text.cex)
  }

  for (r in 1:nrow(d))
    for (c in 1:ncol(d)) {
      rect(c-1, r-1, c, r, col=colors[nrow(d) - r + 1,c])
      text(c-.5,r-.5,d[nrow(d) - r + 1,c], cex=text.cex)
    }
}
d <- matrix(c(1,2,3,4,5,6,7,8,9) , nrow = 3)
colnames(d) <- c("1","2","3")
rownames(d) <- c("1","2","3")

colors <- matrix(sapply(d, function(x) ifelse(x < 20, "purple","green")),ncol=ncol(d))

par(mar=c(0,0,1,0))
plot_table(d, colors, "gray",main="returns", text.cex=0.8)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM