I have a list of 21 different data frames called "proc.r1".
我有一个名为“proc.r1”的21个不同数据框的列表。
I'm trying to make 21 graphs, each one using data from each of the data frame in the list.
我正在尝试制作21个图表,每个图表使用列表中每个数据框的数据。
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.
我在下面做的只适用于列表中的第一个数据框。我为“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"))
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.
有没有办法可以循环这个,这样我就可以生成21个图形?我真的不想制作“df1”,“df2”,df3“等等,直到”df21“然后将所有名称复制到那几行函数中并且必须做”plotdf1“,”plotdf2“,”plotdf3“ “等等
Please let me know if this question is confusing.
如果这个问题令人困惑,请告诉我。
Thanks in advance!
提前致谢!
1 个解决方案
#1
4
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.
因为你有一个列表,所以也可以使用lapply,但我更喜欢for循环,因为你没有返回任何东西。
#1
4
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.
因为你有一个列表,所以也可以使用lapply,但我更喜欢for循环,因为你没有返回任何东西。