自动使用颜色循环绘制矩形

时间:2020-08-05 02:30:58

标签: r loops plot

我在下面列出了该数据集,将在下面用于我的问题。

Data<-read.table(file=file.choose(),header=T) 

Data;
    VARIABLE    TYPE    NGENES  BETA    BETA_STD    SE  P
    black   SET 43  -0.049246   -0.0078434  0.14654 0.63156
    blue    SET 152 -0.080217   -0.023193   0.08137 0.83781
    brown   SET 163 -0.057881   -0.017266   0.079054    0.76791
    cyan    SET 42  0.1498  0.023586    0.14128 0.1446
    darkgreen   SET 2   -0.65338    -0.022727   0.67635 0.83292
    green   SET 172 -0.13458    -0.041115   0.073527    0.96631
    greenyellow SET 40  0.026733    0.0041104   0.14624 0.42749
    grey    SET 4   0.16388 0.0080567   0.53064 0.37874
    grey60  SET 23  -0.1455 -0.017054   0.20066 0.76576
    lightcyan   SET 41  0.083008    0.012918    0.15225 0.29284
    magenta SET 32  -0.10777    -0.014858   0.16601 0.74184
    midnightblue    SET 23  0.00024188  2.84E-05    0.19544 0.49951
    pink    SET 64  -0.017662   -0.0034093  0.12521 0.55608
    purple  SET 60  0.12025 0.022504    0.12624 0.17048
    red SET 73  0.40737 0.083745    0.11427 0.00018742
    royalblue   SET 7   -0.27895    -0.018125   0.36009 0.78067
    salmon  SET 170 0.040831    0.01241 0.076001    0.29559
    turquoise   SET 450 0.027806    0.012383    0.050585    0.29131

对于这个数据集,我想在绘图上创建几个矩形,每个矩形都有不同的颜色编码,并在矩形顶部标有pvalue。我想遍历VARIABLE列,并为每个矩形分配一种颜色。此外,我想遍历P列并将P值写在每个矩形的顶部。因此,对于数据集中的每一行,颜色和p值应相同。这是我现在正在尝试的脚本。我没有看到如何使用此脚本循环关联的列。任何帮助都很好。

coords <- matrix(
c(100, 300, 110, 310,
120, 300, 130, 310,
140, 300, 150, 310,
160, 300, 170, 310,
180, 300, 190, 310,
100, 320, 110, 330,
120, 320, 130, 330,
140, 320, 150, 330,
160, 320, 170, 330,
180, 320, 190, 330,
100, 340, 110, 350,
120, 340, 130, 350,
140, 340, 150, 350,
160, 340, 170, 350,
180, 340, 190, 350,
100, 360, 110, 370,
120, 360, 130, 370,
140, 360, 150, 370),
ncol=4,byrow=TRUE)

 plot(c(100, 200), c(300, 450), type = "n",
         main = "Test")
rfun <- function(x,i) {
    do.call(rect,as.list(x))
}
apply(coords,1,rfun)
text((coords[,1]+coords[,3])/2,
     (coords[,2]+coords[,4])/2,
     seq(nrow(coords)))

1 个答案:

答案 0 :(得分:1)

我不确定,但是也许您想要这样的东西?

DF <- structure(list(VARIABLE = c("black", "blue", "brown", "cyan",
  "darkgreen", "green", "greenyellow", "grey", "grey60", "lightcyan",
  "magenta", "midnightblue", "pink", "purple", "red", "royalblue", 
  "salmon", "turquoise"), 
  TYPE = c("SET", "SET", "SET", "SET", 
           "SET", "SET", "SET", "SET", "SET", "SET", "SET", "SET", "SET", 
           "SET", "SET", "SET", "SET", "SET"), 
  NGENES = c(43L, 152L, 163L, 42L, 2L, 172L, 40L, 4L, 23L, 41L, 32L, 23L, 
             64L, 60L, 73L, 7L, 170L, 450L), 
  BETA = c(-0.049246, -0.080217, -0.057881, 0.1498, -0.65338, -0.13458, 
           0.026733, 0.16388, -0.1455, 0.083008, -0.10777,  0.00024188, 
           -0.017662, 0.12025, 0.40737, -0.27895, 0.040831, 0.027806), 
  BETA_STD = c(-0.0078434, -0.023193, -0.017266, 0.023586, -0.022727, 
               -0.041115, 0.0041104, 0.0080567, -0.017054, 0.012918, 
               -0.014858, 2.84e-05, -0.0034093, 0.022504, 0.083745, 
               -0.018125, 0.01241, 0.012383), 
  SE = c(0.14654, 0.08137, 0.079054, 0.14128, 0.67635, 0.073527, 0.14624, 
         0.53064, 0.20066, 0.15225, 0.16601, 0.19544, 0.12521, 0.12624,
         0.11427, 0.36009, 0.076001, 0.050585), 
  P = c(0.63156, 0.83781, 0.76791, 0.1446, 0.83292, 0.96631, 0.42749, 
        0.37874, 0.76576, 0.29284, 0.74184, 0.49951, 0.55608, 0.17048, 
        0.00018742, 0.78067, 0.29559, 0.29131)), 
  class = "data.frame", 
  row.names = c(NA, -18L))

coords <- matrix(
  c(100, 300, 110, 310,
    120, 300, 130, 310,
    140, 300, 150, 310,
    160, 300, 170, 310,
    180, 300, 190, 310,
    100, 320, 110, 330,
    120, 320, 130, 330,
    140, 320, 150, 330,
    160, 320, 170, 330,
    180, 320, 190, 330,
    100, 340, 110, 350,
    120, 340, 130, 350,
    140, 340, 150, 350,
    160, 340, 170, 350,
    180, 340, 190, 350,
    100, 360, 110, 370,
    120, 360, 130, 370,
    140, 360, 150, 370),
  ncol=4,byrow=TRUE)

rfun <- function(x, i) do.call(rect, c(as.list(x), border = i))

plot(c(100, 200), c(300, 450), type = "n",
     main = "Test")
invisible(sapply(seq_len(nrow(DF)), 
  function(y) do.call(rect, c(as.list(coords[y,]), border = DF$VARIABLE[y]))))
text((coords[,1]+coords[,3])/2,
     (coords[,2]+coords[,4])/2,
     round(DF$P, 2))

reprex package(v0.3.0)于2020-08-04创建