how to add layers in ggplot using a for-loop
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I would like to plot each column of a dataframe to a separate layer in ggplot2. Building the plot layer by layer works well: df<-data.frame(x1=c(1:5),y1=c(2.0,5.4,7.1,4.6,5.0),y2=c(0.4,9.4,2.9,5.4,1.1),y3=c(2.4,6.6,8.1,5.6,6.3)) ggplot(data=df,aes(df[,1]))+geom_line(aes(y=df[,2]))+geom_line(aes(y=df[,3])) Is there a way to plot all available columns at ones by using a single function? I tried to do it this way but it does not work: plotAllLayers<-function(df){ p<-ggplot(data=df,aes(df[,1])) for(i in seq(2:ncol(df))){ p<-p+geom_line(aes(y=df[