ggplot循环遍历列并在一个页面中显示绘图

时间:2018-01-27 14:19:41

标签: r loops plot ggplot2

正如你从标题中看到的那样,我遇到了一个问题,这个问题已经花了整整一个下午。 我有一个可以从here访问的数据框,我想在其他列上绘制几列,每次都是一对列。

因此,我创建了一个data.frame来存储我想要相互绘制的列名称对:

varname.df<-data.frame(num=c(1:9),
                       cust= c("custEnvironment",'custCommunity','custHuman','custEmp','custDiversity',
                               'custProduct','custCorp',"custtotal.index","custtotal.noHC.index"),
                       firm=c("firmEnvironment",'firmCommunity','firmHuman','firmEmp','firmDiversity',
                              'firmProduct','firmCorp',"firmtotal.index","firmtotal.noHC.index"))

## factor to character
i<-sapply(varname.df,is.factor)
varname.df[i]<-lapply(varname.df[i], as.character)
rm(i)

然后使用ggplot2绘图并将结果图存储在列表中,请参阅下面的代码:

## data I provided in the link above
temp<-read.xlsx('sample data.xlsx',1)
f <- list()
for(i in 1:9) { #dim(varname.df)[1]
  # p[[i]]<-plot(x = SC.csr[,varname.df[i,'cust']],y = SC.csr[,varname.df[i,'firm']])
  dat<-subset(temp,select = c(varname.df[i,'cust'], varname.df[i,'firm']))
  pc1 <- ggplot(dat,aes(y = dat[,1], x =  dat[,2])) +
    # labs(title="Plot of CSR", x =colnames(dat)[2], y = colnames(dat)[1]) + 
    geom_point()

  f[[i]]<-pc1
  print(pc1)
  Sys.sleep(5)
  rm(pc1,pc2,pc3)

}
do.call(grid.arrange,f)

这假设可以作为答案herehere,但事情对我来说似乎并不好,因为它给了我enter image description here

所有图中的完全相同的点,但如果你运行for循环,它会在每次迭代时产生不同的数字,你可以亲眼看到。

在将近一个下午调试之后,似乎每当我向列表中添加一个新的ggplot对象时,它只会更改同一列表中所有其他ggplot个对象的所有数据点。

这在某种意义上是如此奇怪和令人沮丧,没有错误抛出但在某处出现问题。任何建议都将深表感谢。

-----------&#34;编辑&#34; -------

问题解决了 here ,第三个答案。

1 个答案:

答案 0 :(得分:0)

如果你想使用facets,你可以试试这个:

    varname.df<-data.frame(num=c(1:9),
                       cust= c("custEnvironment",'custCommunity','custHuman','custEmp','custDiversity',
                               'custProduct','custCorp',"custtotal.index","custtotal.noHC.index"),
                       firm=c("firmEnvironment",'firmCommunity','firmHuman','firmEmp','firmDiversity',
                              'firmProduct','firmCorp',"firmtotal.index","firmtotal.noHC.index"))
## factor to character
i<-sapply(varname.df,is.factor)

    varname.df[i]<-lapply(varname.df[i], as.character)
    rm(i)
    temp<-read.xlsx('sample data.xlsx',1)
    temp1=temp%>%select(c(varname.df$cust))%>%melt(value.name = "y")%>%mutate(id=str_replace(variable,"cust",""))
    temp2=temp%>%select(c(varname.df$firm))%>%melt(value.name = "x")%>%mutate(id=str_replace(variable,"firm",""))
    temp0=merge(temp1,temp2,by="id")%>%select(id,x,y)
ggplot(temp0,aes(x=x,y=y))+geom_point()+facet_grid(.~id)+xlab("Firm")+ylab("Cust")

enter image description here

如果您希望将图表存储在列表中并将它们绘制在网格中,则下面的代码似乎可以解决问题。

varname.df<-data.frame(num=c(1:9),
                       cust= c("custEnvironment",'custCommunity','custHuman','custEmp','custDiversity',
                               'custProduct','custCorp',"custtotal.index","custtotal.noHC.index"),
                       firm=c("firmEnvironment",'firmCommunity','firmHuman','firmEmp','firmDiversity',
                              'firmProduct','firmCorp',"firmtotal.index","firmtotal.noHC.index"))
## factor to character
i<-sapply(varname.df,is.factor)
varname.df[i]<-lapply(varname.df[i], as.character)
rm(i)
temp<-read.xlsx('sample data.xlsx',1)
f <- list()
for(i in 1:9) { #dim(varname.df)[1]
  f[[i]]<-subset(temp,select = c(varname.df[i,'cust'], varname.df[i,'firm']))

}

plotlist=lapply(1:9,function(x) ggplot(f[[x]],aes(y = f[[x]][,1], x =  f[[x]][,2])) +geom_point()+xlab("Firm")+
  ylab("Cust"))

plot_grid(plotlist=plotlist)

enter image description here