I have 4 columns and 56 rows of made up data that I want to plot as a series of bar graphs. The idea is to create one bar graph for each of the 4 columns using a for loop. I tried the following command in RStudio and when I type x in the console I get just the 4th graph instead of all four graphs. I did not define what x is before hand. I was not sure what it would be. Any suggestions on how you would properly define x? for(i in 1:4){ qplot(x[i]<-mydata[,i]) } The headers of the columns are Trial.1 Trial.2 Trial.3 and Trial.4. Also note that I can easily make any of the four graphs by i=1 or i=2, etc. qplot(x<-mydata[,i]) I just need to figure out how to make it loop correctly. The ultimate goal would also be to save these graphs to my computer with automatically generated file names. Something like test graph1.png, test graph2.png, etc. Thanks For The Help! -- View this message in context: http://r.789695.n4.nabble.com/qplot-and-for-loops-tp3663292p3663292.html Sent from the R help mailing list archive at Nabble.com.
Hi, You actually did the loop correctly. The problem is that the graphs were created very quickly so you only see the last one. One way around this is to make R wait for user input. You can turn this on and off for a particular device using: par(ask = TRUE) See ?par for details on this. To create the various files in a for loop, you actually do not need this. You should be able to get by with something like: png(filename = "Graph%03d.png") for(i in 1:4){ qplot(x[i]<-mydata[,i]) } dev.off() Hope this helps! Josh On Tue, Jul 12, 2011 at 12:21 PM, wwreith <reith_william at bah.com> wrote:> I have 4 columns and 56 rows of made up data that I want to plot as a series > of bar graphs. The idea is to create one bar graph for each of the 4 columns > using a for loop. I tried the following command in RStudio and when I type x > in the console I get just the 4th graph instead of all four graphs. I did > not define what x is before hand. I was not sure what it would be. Any > suggestions on how you would properly define x? > > for(i in 1:4){ > qplot(x[i]<-mydata[,i]) > } > > The headers of the columns are Trial.1 Trial.2 Trial.3 and Trial.4. Also > note that I can easily make any of the four graphs by > > i=1 or i=2, etc. > qplot(x<-mydata[,i]) > > I just need to figure out how to make it loop correctly. The ultimate goal > would also be to save these graphs to my computer with automatically > generated file names. Something like test graph1.png, test graph2.png, etc. > > Thanks For The Help! > > -- > View this message in context: http://r.789695.n4.nabble.com/qplot-and-for-loops-tp3663292p3663292.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles https://joshuawiley.com/
On Jul 12, 2011, at 3:21 PM, wwreith wrote:> I have 4 columns and 56 rows of made up data that I want to plot as > a series > of bar graphs. The idea is to create one bar graph for each of the 4 > columns > using a for loop. I tried the following command in RStudio and when > I type x > in the console I get just the 4th graph instead of all four graphs. > I did > not define what x is before hand. I was not sure what it would be. Any > suggestions on how you would properly define x?Does this work for(i in 1:4){ png(file=paste("graph",i,".png". sep="") print(qplot(x<-mydata[,i]) ) dev.off() } I'm not so sure about the x<-mydata[,i] part, but you say it's working. I would have guess that you should have had particular arguments. But then you didn't see fit to show either your data or str on data so you get a vague answer to a vague question.> The headers of the columns are Trial.1 Trial.2 Trial.3 and Trial.4. > Also > note that I can easily make any of the four graphs by > > i=1 or i=2, etc. > qplot(x<-mydata[,i]) > > I just need to figure out how to make it loop correctly. The > ultimate goal > would also be to save these graphs to my computer with automatically > generated file names. Something like test graph1.png, test > graph2.png, etc. > > Thanks For The Help! > > -- > View this message in context: http://r.789695.n4.nabble.com/qplot-and-for-loops-tp3663292p3663292.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.David Winsemius, MD West Hartford, CT
On Tue, Jul 12, 2011 at 2:01 PM, Reith, William [USA] <reith_william at bah.com> wrote:> Do I need to define x in any way before I do the loop?No, you should not need to define x explicitly. Just pass the data frame you want to qplot. David was absolutely right though that inside a loop, you should wrap the call to qplot() in print(). Slipped my mind (so follow his example). Josh> > Sent from my Verizon Wireless 4GLTE smartphone
I just wanted the post the results of the emails I been sending/getting. The following command will create the graphs and save them to the location of your choice. The names are graph1.png, graph2.png etc. for(i in 1:4) { png( file=paste("C:/Insert file location/graph",i,".png", sep="") ) print( qplot(x=mydata[,i]) ) dev.off()} Thanks for the help you guys. -- View this message in context: http://r.789695.n4.nabble.com/qplot-and-for-loops-tp3663292p3666489.html Sent from the R help mailing list archive at Nabble.com.