【问题标题】:Making multiple plots from a list of data frames从数据框列表中制作多个图
【发布时间】:2014-03-01 03:29:21
【问题描述】:

我有一个名为“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 个图表? 我真的不想在“df21”之前制作“df1”、“df2”、“df3”等,然后将所有名称复制到这几行函数中,并且必须执行“plotdf1”、“plotdf2”、“plotdf3” "等等等等。

如果这个问题令人困惑,请告诉我。

提前致谢!

【问题讨论】:

    标签: r list ggplot2


    【解决方案1】:

    比较简单:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-22
      • 1970-01-01
      • 2021-12-16
      • 2022-01-06
      相关资源
      最近更新 更多