简体   繁体   English

R:绘制值的网格

[英]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=1Y=0.1会生成13.1

X=1 and Y=1.1 it generates 19.1 X=1Y=1.1会生成19.1

X=1 and Y=2.1 it generates 25.1 X=1Y=2.1会生成25.1

X=2 and Y=0.1 it generates 13.2 X=2Y=0.1会产生13.2

X=2 and Y=1.1 it generates 20.2 X=2Y=1.1会产生20.2

X=2 and Y=2.1 it generates 27.2 X=2Y=2.1会产生27.2

X=3 and Y=0.1 it generates 13.3 X=3Y=0.1会产生13.3

X=3 and Y=1.1 it generates 21.3 X=3Y=1.1会产生21.3

X=3 and Y=2.1 it generates 29.3 X=3Y=2.1会产生29.3

So then I get in total 9 values, what I want to do is to plot them in a grid. 因此,我总共得到9个值,我想要做的是将它们绘制在网格中。 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. X可以在顶部或底部无关紧要,也可以在Y上着色,以便较小的值具有较浅的颜色,较大的值具有较暗的颜色。

Is there any way to do this plot in R? 有什么办法可以在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. 或者,如果有人可以建议我另一种类型的绘图,则可以用它们的值绘制X和Y。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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