使用lapply从数据表创建多个格子图 [英] Create multiple lattice plots from a data table using lapply

查看:194
本文介绍了使用lapply从数据表创建多个格子图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图为众多变量(物种)中的每一个生成平均值,然后分别绘制每个变量。我试过列表和数据表格式。 base plot函数在for循环中工作:

  library(data.table)

for i in 3:5){
#生成列号为i b $ b temp <-v 2 [,lapply(.SD,mean),by =Season的物种的平均值列表, .subcols = i]
#将列表中的每一列作为散点图绘制,主标题=第二列的标题b $ b plot(temp [[2]]〜temp [[1]],main = colnames (temp)[[2]])
}

但是当我尝试创建格对于最后一个变量,只生成一个图:

  library(data.table)
library(lattice)

(i in 3:5){
#根据列号i
temp < - v2 [,lapply( .SD,mean),by = c(Season,Location),.SDcols = i]
#在单独的小图中的每个组
xyplot(temp [[3]]〜temp [[1]] | temp [[2]],main = colnames(temp)[3])
}

试过了保存或打印每个格子图,这是正确的想法?也许我一直在想这个错误的方式?



这是我的数据的一个小样本:

<$ p结构(列表(位置=结构(c(1L,1L,1L,1L,4L,4L,
4L,6L,6L,1L),。标签= c( BN,BS,GN,GS,SB,SL,
,class = (1995),1995L,1995L,1997L,1997L,2000L),Agrostis.magellanica = c(0.3,
0.3,0.3,0.01,0.6,0.3,0.3,0.3,0.6,0.05),Festuca.contracta = c (0.6,
0.05,0.3,0.01,0.01,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0) ),.Names = c(Location,
Season,Agrostis.magellanica,Festuca.contracta,Poa.annua
),class = c(data.table,data.frame),row.names = c(NA,-10L


解决方案

在R-FAQ中,需要一个 print ggplot)当在一个函数中使用时,a nd for 循环是一个函数:

 #需要
在定义对象之前需要(data.table)#。

pdf()#pdf是一个多页面设备。
for(i in 3:5){
#根据列数i
temp < - v2 [,lapply(.SD,mean, ),by = c(Season,Location),.SDcols = i]
#在一个单独的迷你小区
print(xyplot(temp [[3]]〜temp [ 1]] | temp [[2]],main = colnames(temp)[3]))
}
dev.off()#无法正确关闭文件图形设备是常见错误。


I am trying to generate mean values by group for each of numerous variables (species) and then plot each of these separately. I have tried list and data table formats. The base plot function works in a for loop:

library(data.table)

      for (i in 3:5) {
      # generate a list of mean value for the species in column number i
      temp <- v2[, lapply(.SD, mean), by="Season", .SDcols=i]
      # plot each col of the list as a scatterplot with main title = header of 2nd col
      plot(temp[[2]]~temp[[1]], main = colnames(temp)[[2]])
    }

But when I try to create lattice plots only a single plot is generated for the last variable:

library(data.table)
library(lattice)

for (i in 3:5) {
  # generate a list of mean value by season for the species in column number i
  temp <- v2[, lapply(.SD, mean), by=c("Season", "Location"), .SDcols=i]
  # Each group in a separate mini plot
  xyplot(temp[[3]]~temp[[1]] | temp[[2]], main = colnames(temp)[3])
}

Have tried saving or printing each lattice plot, is that the right idea? Perhaps I am going about this the wrong way altogether?

Here is a small sample of my data:

    structure(list(Location = structure(c(1L, 1L, 1L, 1L, 4L, 4L, 
4L, 6L, 6L, 1L), .Label = c("BN", "BS", "GN", "GS", "SB", "SL"
), class = "factor"), Season = c(1981L, 1981L, 1981L, 1981L, 
1995L, 1995L, 1995L, 1997L, 1997L, 2000L), Agrostis.magellanica = c(0.3, 
0.3, 0.3, 0.01, 0.6, 0.3, 0.3, 0.3, 0.6, 0.05), Festuca.contracta = c(0.6, 
0.05, 0.3, 0.01, 0.01, 0, 0, 0, 0.01, 0.05), Poa.annua = c(0.01, 
0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.05, 0.01, 0.01)), .Names = c("Location", 
"Season", "Agrostis.magellanica", "Festuca.contracta", "Poa.annua"
), class = c("data.table", "data.frame"), row.names = c(NA, -10L
)

解决方案

This is in the R-FAQ. Need a print statement around grid graphics (lattice or ggplot) when used inside a function, and the for loop is a function:

# Needed
require(data.table)  # before defining the object.

pdf()  # pdf is a multipage device.
for (i in 3:5) {
  # generate a list of mean value by season for the species in column number i
  temp <- v2[, lapply(.SD, mean), by=c("Season", "Location"), .SDcols=i]
  # Each group in a separate mini plot
  print( xyplot(temp[[3]]~temp[[1]] | temp[[2]], main = colnames(temp)[3]) )
}
dev.off()  # Failing to properly close a file graphics device is common error.

这篇关于使用lapply从数据表创建多个格子图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆