for loop从单个数据帧创建多个GGPLOT

for loop从单个数据帧创建多个GGPLOT,r,loops,for-loop,plot,ggplot2,R,Loops,For Loop,Plot,Ggplot2,我一直在尝试创建一个for循环,它将遍历我的数据帧,并为我拥有的每个ID绘制相同的图。以下是一些示例数据: TJID1 <- c("TJ22", "TJ22", "TJ23", "TJ23", "TJ23", "TJ24", "TJ24") Day <- c("2005-11-22", "2005-11-23", "2006-12-01", "2006-12-02", "2006-12-03","2005-07-08", "2005-07-08") Mean.Depth <-

我一直在尝试创建一个for循环,它将遍历我的数据帧,并为我拥有的每个ID绘制相同的图。以下是一些示例数据:

TJID1 <- c("TJ22", "TJ22", "TJ23", "TJ23", "TJ23", "TJ24", "TJ24")
Day <- c("2005-11-22", "2005-11-23", "2006-12-01", "2006-12-02", "2006-12-03","2005-07-08", "2005-07-08")
Mean.Depth <- c (2, 2, 3, 4, 5, 6, 6)
SE.Depth <- c(1, 1, 2, 2, 1, 2, 2)
sample  <- cbind(TJID1, Day, Mean.Depth, SE.Depth)
sample <- as.data.frame(sample)

TJID1这里有一个想法。我们可以设计一个函数来子集
sample
,然后创建并返回一个绘图。之后,我们使用
lappy
循环遍历
TJID1
中的唯一值

请注意,在原始的
示例
数据框中,这些数字列表示为因子。我改变了创建
sample
数据框的方法来解决这个问题。最后一个音符
sample
是个坏名字,因为R中有一个名为
sample
的函数,这会导致混淆。请使用将来与其他函数名不匹配的其他名称命名数据帧

# Load package
library(ggplot2)

# Create example data frame
sample <- data.frame(TJID1 = c("TJ22", "TJ22", "TJ23", "TJ23", "TJ23", "TJ24", "TJ24"),
                     Day = c("2005-11-22", "2005-11-23", "2006-12-01", "2006-12-02", "2006-12-03","2005-07-08", "2005-07-08"),
                     Mean.Depth = c (2, 2, 3, 4, 5, 6, 6),
                     SE.Depth = c(1, 1, 2, 2, 1, 2, 2),
                     stringsAsFactors = FALSE)

# Design a function
gg_fun <- function(parameter, dt){

  p <- ggplot(dt[dt$TJID1 == parameter, ], aes(x=Day, y=Mean.Depth))+
    geom_point()+
    geom_line()+
    geom_errorbar(aes(ymin=Mean.Depth-1.96*SE.Depth, ymax=Mean.Depth+1.96*SE.Depth), width = 0.5, col="red") +
    ggtitle(parameter)

  return(p)
}

# Apply the function
plot_list <- lapply(unique(sample$TJID1), gg_fun, dt = sample)
#加载包
图书馆(GG2)
#创建示例数据帧

样本必须
打印(p)
。为什么不直接使用
facet\u wrap(~TJID1)
而不是循环呢?哇!非常感谢:)我非常专注于尝试使用循环,以至于我甚至没有想到函数。这很有效!另外,如果我想添加到函数+ggtitle(),我如何让它显示每个单独绘图的TJ编号作为标题?
var_list = combn(names(sample) [3:4], 3, simplify=FALSE)
plot_list = list()
for (i in unique (sample$TJID1)){
     TJ <- sample[sample$TJID1== i,]

     p = ggplot(TJ, aes_string(x=var_list[[i]][1], y=var_list[[i]][2])) +
    geom_point()+
    geom_line()

 plot_list[[i]] = p
 }
# Load package
library(ggplot2)

# Create example data frame
sample <- data.frame(TJID1 = c("TJ22", "TJ22", "TJ23", "TJ23", "TJ23", "TJ24", "TJ24"),
                     Day = c("2005-11-22", "2005-11-23", "2006-12-01", "2006-12-02", "2006-12-03","2005-07-08", "2005-07-08"),
                     Mean.Depth = c (2, 2, 3, 4, 5, 6, 6),
                     SE.Depth = c(1, 1, 2, 2, 1, 2, 2),
                     stringsAsFactors = FALSE)

# Design a function
gg_fun <- function(parameter, dt){

  p <- ggplot(dt[dt$TJID1 == parameter, ], aes(x=Day, y=Mean.Depth))+
    geom_point()+
    geom_line()+
    geom_errorbar(aes(ymin=Mean.Depth-1.96*SE.Depth, ymax=Mean.Depth+1.96*SE.Depth), width = 0.5, col="red") +
    ggtitle(parameter)

  return(p)
}

# Apply the function
plot_list <- lapply(unique(sample$TJID1), gg_fun, dt = sample)