从数据框列表中制作多个图

我有一个名为“proc.r1”的21个不同数据框的列表。

我试图制作21个图表,每个图表使用列表中每个数据框的数据。

我在下面做的只适用于列表中的第一个数据框。 我为“plotdf1”写的ggplot是我需要从每个数据帧生成的图。 所以我需要21个相同的“plotdf1”,只是每个人都会显示来自不同数据帧的数据。

    #making the first data frame in the "proc.r1" list as a separate data frame
    df1 <- as.data.frame(proc.r1 [[1]])
    #making all the NA values displayed as 0
    df1[is.na(df1)] <- 0
    #rounding off the "norm.n" column, which I'll use as a label in the plot, to just 1 decimal point
    df1 [,42] <-  round(df1[,42],1)
    plotdf1 <- ggplot(df1)+geom_rect(aes(xmin=0,xmax=5,ymin=0,ymax=5))+facet_grid(row~col)+geom_text(aes(x=2.5,y=2.5,label=norm.n,colour="white"))

有没有一种方法可以循环使用,以便生成21个图形? 我真的不想制作“df1”,“df2”,df3“等等,直到”df21“,然后将所有名称复制到那几行函数中,并且必须做”plotdf1“,”plotdf2“,”plotdf3 “等等等等

请让我知道这个问题是否令人困惑。

提前致谢!


这相对简单:

for(i in 1:length(proc.r1)
{
    df1 = as.data.frame(proc.r1[[i]])
    df1[is.na(df1)] <- 0
    df1[,42] <-  round(df1[,42],1)
    plotdf1 <- ggplot(df1)+geom_rect(aes(xmin=0,xmax=5,ymin=0,ymax=5))+
             facet_grid(row~col)+geom_text(aes(x=2.5,y=2.5,label=norm.n,colour="white"))
    print(plotdf1)
}

使用lapply也是可能的,因为你有一个列表,但我更喜欢for循环,因为你没有返回任何东西。

链接地址: http://www.djcxy.com/p/77163.html

上一篇: Making multiple plots from a list of data frames

下一篇: ember.js find() helper acceptable selectors