【问题标题】:Creating a loop to print selected panels from a Lattice graph in independent pages创建一个循环以在独立页面中从 Lattice 图中打印选定的面板
【发布时间】:2011-09-20 11:27:47
【问题描述】:

我在 lattice 和 latticeExtra 中生成了以下图

使用

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")

【问题讨论】:

    标签: r loops plot lattice


    【解决方案1】:

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

    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))
            ))
    }    
    

    至少有两种方法可以将其写入第二个文档:

    • 确保在 R 图形设备中将 recording 设置为 TRUE。我使用 RGui 设备,在History 菜单中有一个选项。然后照常复制粘贴。
    • 第二种方式是使用pdf设备:

      pdf(一个文件=FALSE) 打印(直方图(...)) 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()
    }    
    

    【讨论】:

    • 我使用了trellis.device(),在这种情况下你不需要设置pdf(onefile=false)它会为每个绘图打开一个新的图形设备
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多