我有一个包含 21 个不同数据帧的列表,名为“proc.r1”。
我正在尝试制作 21 个图表,每个图表都使用列表中每个数据框的数据。
我下面所做的仅适用于列表中的第一个数据框。我为“plotdf1”编写的 ggplot 是我需要从每个数据帧生成的图表。因此,我需要 21 个外观相同的“plotdf1”,只不过每个“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)
}
Using lapply
也是可能的,因为你有一个列表,但我更喜欢for
在这里循环,因为你没有返回任何东西。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)