【问题标题】:ggplot loop over columns and show plot in one page [duplicate]ggplot循环列并在一页中显示图[重复]
【发布时间】:2018-01-27 14:19:40
【问题描述】:

正如您从标题中看到的那样,我遇到了一个已经花了我整个下午的问题。 我有一个可以从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)

这应该是here 和here 的答案,然而,事情对我来说似乎不是那么好,因为它给了我

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

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

从某种意义上说,这太奇怪了,令人沮丧,以至于没有错误抛出,但在某个地方出现了错误。任何建议将不胜感激。

- - - - - -“编辑” - - - -

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

【问题讨论】:

  • 请将数据集添加到小时问题中(例如,dput(temp))
  • 我愿意,但数据有点大,我认为至少需要 100 个 obs 才能使问题更加清晰以及其他人重现我的结果!
  • @PoGibas,你可能想取消我的问题,因为我解释了为什么不使用dput(temp) 提供数据的原因,并且这个问题有一个答案,虽然问题是一些想要重复,答案不是!
  • 抱歉造成误会,感谢您的cmets!问之前应该做更多的搜索!

标签: r loops plot ggplot2


【解决方案1】:

如果您想使用构面,可以试试这个:

    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")

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

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)

【讨论】:

  • 是,但不是。从结果可以看出,不同图中的所有点都是完全相同的,它们假设是不同的,因为我们在这里循环了不同的列!
  • 我认为现在的结果就是你想要的,九个图在它们之间是不同的,无论是在方面还是在网格解决方案上
  • 这确实解决了我的问题,我将其作为答案。对于可能遇到相同问题的人,此问题还有另一种解决方案,请参阅问题编辑下的链接。
猜你喜欢
  • 2018-04-05
  • 2020-12-06
  • 1970-01-01
  • 2021-10-19
  • 1970-01-01
  • 2018-07-22
  • 2022-01-09
  • 2014-01-07
  • 2023-04-03
相关资源
最近更新 更多