gpt4 book ai didi

r - 创建循环以在独立页面中打印格子图中的选定面板

转载 作者:行者123 更新时间:2023-12-04 06:21:58 24 4
gpt4 key购买 nike

我在格子和格子Extra中生成了以下图
enter image description here

使用

library(lattice)
library(latticeExtra)
rate$Temp <- as.character(rate$Temp)
rate$Week <- as.character(rate$Week)
rate$Rep <- as.character(rate$Rep)
histogram(~Rate|Week+Temp, rate,
col=(NA),as.table=TRUE,
strip=strip.custom(strip.names=1)) +as.layer
(bwplot(~Rate|Week+Temp, rate,
as.table=TRUE,
strip=strip.custom(strip.names=1))
)

但是我需要将每个组合绘制在不同的页面中,以便每周的所有温度 11 都在一页中,依此类推。我知道这可以手动完成,但必须有一种使用循环来完成的方法。任何人??
这是用于生成绘图的数据
rate <- structure(list(Temp = c("11°C", "11°C", "11°C", "11°C", "11°C", 
"11°C", "11°C", "11°C", "11°C", "11°C", "11°C", "11°C", "11°C",
"11°C", "11°C", "11°C", "13°C", "13°C", "13°C", "13°C", "13°C",
"13°C", "13°C", "13°C", "13°C", "13°C", "13°C", "13°C", "13°C",
"13°C", "13°C", "13°C", "15°C", "15°C", "15°C", "15°C", "15°C",
"15°C", "15°C", "15°C", "15°C", "15°C", "15°C", "15°C", "15°C",
"15°C", "15°C", "15°C"), Rep = c("1", "1", "1", "1", "1", "1",
"1", "1", "2", "2", "2", "2", "2", "2", "2", "2", "1", "1", "1",
"1", "1", "1", "1", "1", "2", "2", "2", "2", "2", "2", "2", "2",
"1", "1", "1", "1", "1", "1", "1", "1", "2", "2", "2", "2", "2",
"2", "2", "2"), Ind = c(12L, 12L, 12L, 12L, 14L, 14L, 14L, 14L,
11L, 11L, 11L, 11L, 10L, 10L, 10L, 10L, 2L, 2L, 2L, 2L, 7L, 7L,
7L, 7L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L,
5L, 5L, 8L, 8L, 8L, 8L, 13L, 13L, 13L, 13L), Week = c("1", "2",
"3", "4", "1", "2", "3", "4", "1", "2", "3", "4", "1", "2", "3",
"4", "1", "2", "3", "4", "1", "2", "3", "4", "1", "2", "3", "4",
"1", "2", "3", "4", "1", "2", "3", "4", "1", "2", "3", "4", "1",
"2", "3", "4", "1", "2", "3", "4"), Weight = c(0.2099, 0.2099,
0.2099, 0.2099, 0.3483, 0.3483, 0.3483, 0.3483, 0.3724, 0.3724,
0.3724, 0.3724, 0.2751, 0.2751, 0.2751, 0.2751, 0.2562, 0.2562,
0.2562, 0.2562, 0.3842, 0.3842, 0.3842, 0.3842, 0.3206, 0.3206,
0.3206, 0.3206, 0.4803, 0.4803, 0.4803, 0.4803, 0.7392, 0.7392,
0.7392, 0.7392, 0.277, 0.277, 0.277, 0.277, 0.082, 0.082, 0.082,
0.082, 0.5663, 0.5663, 0.5663, 0.5663), Rate = c(13.733, 9.322,
12.46, 6.581, 2.338, 2.652, 2.772, 3.026, 1.554, 1.677, 2.127,
2.102, 2.522, 2.476, 2.847, 2.999, 1.764, 2.442, 2.877, 2.976,
2.479, 2.532, 2.874, 3.052, 1.724, 2.292, 2.405, 2.397, 1.553,
3.385, 3.178, 2.959, 0.687, 0.808, 1.042, 1.033, 2.255, 2.755,
2.878, 3.338, 4.515, 5.089, 7.349, 8.113, 1.185, 1.649, 2.382,
1.989)), .Names = c("Temp", "Rep", "Ind", "Week", "Weight", "Rate"
), row.names = c(NA, -48L), class = "data.frame")

最佳答案

这个小循环将快速连续打印三个不同的图:

temps <- unique(rate$Temp)
temps

for (i in seq_along(temps)){
#plot.new()
rate_subset <- rate[rate$Temp==temps[i], ]
print(histogram(~Rate|Week, rate_subset,
col=(NA),as.table=TRUE,
strip=strip.custom(strip.names=1)) +
as.layer(bwplot(~Rate|Week, rate_subset,
as.table=TRUE,
strip=strip.custom(strip.names=1))
))
}

至少有两种方法可以将其放入第二个文档中:
  • 确保 recording在您的 R 图形设备中设置为 TRUE。我用的是RGui设备,在History菜单有一个选项。然后照常复制粘贴。
  • 第二种方法是使用 pdf设备:

    pdf(一个文件=假)
    打印(直方图(...))
    device.off()

  • 那么你的循环变成:
    for (i in seq_along(temps)){
    rate_subset <- rate[rate$Temp==temps[i], ]
    pdf(onefile=FALSE)
    print(histogram(~Rate|Week, rate_subset,
    col=(NA),as.table=TRUE,
    strip=strip.custom(strip.names=1)) +
    as.layer(bwplot(~Rate|Week, rate_subset,
    as.table=TRUE,
    strip=strip.custom(strip.names=1))
    ))
    dev.off()
    }

    关于r - 创建循环以在独立页面中打印格子图中的选定面板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6441894/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com