Making multiple plots from a list of data frames
I have a list of 21 different data frames called "proc.r1".
I'm trying to make 21 graphs, each one using data from each of the data frame in the list.
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"))
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.
Thanks in advance!
It's relatively simple:
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
is also possible since you have a list, but I prefer a for
loop here since you're not returning anything.
上一篇: 如何使(1 << 9)通过MISRA?
下一篇: 从数据框列表中制作多个图