绘制随机布局并填充文本

发布于 2024-12-09 05:08:27 字数 917 浏览 0 评论 0原文

我正在尝试在 R/ 图形中绘制随机化布局。

Trt <- c(paste ("Trt#", 1:10, sep = ""))
    mydes <- data.frame (block1= sample(Trt), block2 = sample(Trt), block3= sample(Trt), block4= sample( Trt), block5= sample(Trt))
    plot(c(0, NCOL(mydes)), c(0, NROW(mydes)), type= "n", xlab="blocks", ylab = "range")
    grid(lty = 2, col = 1)

    mydes 
       block1 block2 block3 block4 block5
    1  Trt#10  Trt#5  Trt#4  Trt#6  Trt#8
    2   Trt#6  Trt#8  Trt#9  Trt#2  Trt#3
    3   Trt#3  Trt#6  Trt#5 Trt#10  Trt#9
    4   Trt#9  Trt#4  Trt#1  Trt#5  Trt#2
    5   Trt#5  Trt#9  Trt#7  Trt#3  Trt#5
    6   Trt#7  Trt#3  Trt#3  Trt#7  Trt#7
    7   Trt#8 Trt#10  Trt#8  Trt#4  Trt#4
    8   Trt#1  Trt#7 Trt#10  Trt#9  Trt#1
    9   Trt#4  Trt#1  Trt#2  Trt#1 Trt#10
    10  Trt#2  Trt#2  Trt#6  Trt#8  Trt#6

我想在每个网格矩形的中间添加文本。我知道通过一一添加文本和指定坐标会很痛苦。但我想要一个具有一些短循环的通用解决方案,它也可以应用于任何其他维度。

谢谢;

I am trying to plot randomization layout in R/ graphics.

Trt <- c(paste ("Trt#", 1:10, sep = ""))
    mydes <- data.frame (block1= sample(Trt), block2 = sample(Trt), block3= sample(Trt), block4= sample( Trt), block5= sample(Trt))
    plot(c(0, NCOL(mydes)), c(0, NROW(mydes)), type= "n", xlab="blocks", ylab = "range")
    grid(lty = 2, col = 1)

    mydes 
       block1 block2 block3 block4 block5
    1  Trt#10  Trt#5  Trt#4  Trt#6  Trt#8
    2   Trt#6  Trt#8  Trt#9  Trt#2  Trt#3
    3   Trt#3  Trt#6  Trt#5 Trt#10  Trt#9
    4   Trt#9  Trt#4  Trt#1  Trt#5  Trt#2
    5   Trt#5  Trt#9  Trt#7  Trt#3  Trt#5
    6   Trt#7  Trt#3  Trt#3  Trt#7  Trt#7
    7   Trt#8 Trt#10  Trt#8  Trt#4  Trt#4
    8   Trt#1  Trt#7 Trt#10  Trt#9  Trt#1
    9   Trt#4  Trt#1  Trt#2  Trt#1 Trt#10
    10  Trt#2  Trt#2  Trt#6  Trt#8  Trt#6

I want to add the text in middle of each grid rectangle. I know I can do it painfully by adding text and specifying coordinates one by one. But I want a generalized solution with some short of loop, that can be applied to any other dimensions as well.

Thanks;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

染年凉城似染瑾 2024-12-16 05:08:27

这是在 ggplot 中执行此操作的一种方法:

library(ggplot2)

blocks <- expand.grid(
  x = 1:ncol(mydes),
  y = 1:nrow(mydes)
)

blocks$label <- unname(rapply(mydes, as.character))

ggplot(blocks) + 
  geom_rect(aes(xmin=x-0.4, xmax=x+0.4, ymin=y-0.4, ymax=y+0.4), fill="cyan") +
  geom_text(aes(label=label, x=x, y=y)) +
  xlab("Blocks") + ylab("Treatments")

在此处输入图像描述

因为您想要绘制这样,您不仅可以打印表格,还可以使用色标来映射具有相同值的处理:

ggplot(blocks) + 
  geom_rect(aes(xmin=x-0.4, xmax=x+0.4, ymin=y-0.4, ymax=y+0.4, fill=label)) +
  geom_text(aes(label=label, x=x, y=y)) +
  xlab("Blocks") + ylab("Treatments") +
  scale_fill_hue("Treatment", h=c(90, 150))

Here is a way of doing it in ggplot:

library(ggplot2)

blocks <- expand.grid(
  x = 1:ncol(mydes),
  y = 1:nrow(mydes)
)

blocks$label <- unname(rapply(mydes, as.character))

ggplot(blocks) + 
  geom_rect(aes(xmin=x-0.4, xmax=x+0.4, ymin=y-0.4, ymax=y+0.4), fill="cyan") +
  geom_text(aes(label=label, x=x, y=y)) +
  xlab("Blocks") + ylab("Treatments")

enter image description here

Since you want to plot this, rather than just printing a table, you may as well use a colour scale to map treatments with the same value:

ggplot(blocks) + 
  geom_rect(aes(xmin=x-0.4, xmax=x+0.4, ymin=y-0.4, ymax=y+0.4, fill=label)) +
  geom_text(aes(label=label, x=x, y=y)) +
  xlab("Blocks") + ylab("Treatments") +
  scale_fill_hue("Treatment", h=c(90, 150))

enter image description here

咋地 2024-12-16 05:08:27

您可以尝试以下操作:

x = rep(1:5, each = 10) - 0.5
y = rep(1:10, 5) - 0.5
text(x, y, labels = unlist(mydes))

问题是您的网格在 y 方向包含 2 个单元格...

You can try this:

x = rep(1:5, each = 10) - 0.5
y = rep(1:10, 5) - 0.5
text(x, y, labels = unlist(mydes))

Problem is that your grid contains 2 cells in the y-direction...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文