从数据框列表中绘制多个图 [英] Making multiple plots from a list of data frames

查看:76
本文介绍了从数据框列表中绘制多个图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有21个不同数据帧的列表,称为"proc.r1".

I have a list of 21 different data frames called "proc.r1".

我正在尝试制作21个图形,每个图形都使用列表中每个数据帧中的数据.

I'm trying to make 21 graphs, each one using data from each of the data frame in the list.

我在下面所做的操作仅适用于列表中的第一个数据框.我为"plotdf1"编写的ggplot是我需要从每个数据帧生成的图形.因此,我需要21个外观相同的"plotdf1",除了每​​个人都将显示来自不同数据帧的数据.

What I did below only works with the first data frame in the list. The ggplot I wrote for "plotdf1" is the graph I need generated from each data frame. So I need 21 of identical-looking "plotdf1" except that each one would display data from different data frames.

    #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" "等

Is there a way I can loop this so that I can generate 21 graphs? I don't really want to make "df1", "df2", df3", etc etc until "df21" then copy all the names into those few lines of functions and have to do "plotdf1", "plotdf2", "plotdf3", etc etc.

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

Please let me know if this question is confusing.

提前谢谢!

推荐答案

相对简单:

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循环,因为您不返回任何内容.

Using lapply is also possible since you have a list, but I prefer a for loop here since you're not returning anything.

这篇关于从数据框列表中绘制多个图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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