Hi all, I have a problem in including my plot in a loop. Here is a simple example for one plot: # Plot simple graph with super- and subscript a<-c(1,2,3,4) b<-c(1,2,3,4) plot(x=a,y=b, ylab=expression(paste("Apple"["P"])), xlab=expression(paste("Banana"^"th")), main=expression(paste(italic("i-")~"4"^"th"~"choice"))) Now I would like to include the titel (main) as a function of the number of trails for (trial in 1:nTrials) { plot( main=expression(paste(italic("i-")~"trial"^"th"~"choice"))) } e.g. nTrials = 5 The title should look like this: 5th plot: i ^th choice 4th plot: i-1 ^th choice 3th plot: i-2 ^th choice and so on I have problems to create that, could you please help me? Thank you!! [[alternative HTML version deleted]]
On Apr 24, 2013, at 9:22 PM, Eva G?nther wrote:> Hi all, > > I have a problem in including my plot in a loop. Here is a simple example > for one plot: > > # Plot simple graph with super- and subscript > a<-c(1,2,3,4) > b<-c(1,2,3,4) > > plot(x=a,y=b, > ylab=expression(paste("Apple"["P"])), > xlab=expression(paste("Banana"^"th")), > main=expression(paste(italic("i-")~"4"^"th"~"choice"))) > > Now I would like to include the titel (main) as a function of the number of > trails > for (trial in 1:nTrials) { > plot( > > main=expression(paste(italic("i-")~"trial"^"th"~"choice"))) > > } >I have not quite figured out what the exact goal is but this shows how to loop with expression that have evaluated component: .... bquote is your friend: for (trial in 1:length(a)) { plot(x=a,y=b, # you were at the very least missing an x and y argument main=bquote( italic(i)-.(trial)^th~choice)) } ... and you had too many quoted elements. Plotmath `paste` is usually not needed. Learn to use * and ~. David.> e.g. nTrials = 5 > The title should look like this: > > 5th plot: i ^th choice > 4th plot: i-1 ^th choice > 3th plot: i-2 ^th choice and so on > > I have problems to create that, could you please help me? > > Thank you!! > > [[alternative HTML version deleted]] > > ______________________________________________ > 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 Alameda, CA, USA
You could use bquote. Something like this: a<-c(1,2,3,4) b<-c(1,2,3,4) nTrials <- length(a) for (trial in 1:nTrials) { plot(x=a[1:trial], y=b[1:trial], ylab=expression(paste("Apple"["P"])), xlab=expression(paste("Banana"^"th")), main=bquote(italic("i-")~.(trial)^"th"~"choice")) } -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Eva G?nther Sent: Donnerstag, 25. April 2013 06:22 To: r-help at r-project.org Subject: [R] Loop for main title in a plot Hi all, I have a problem in including my plot in a loop. Here is a simple example for one plot: # Plot simple graph with super- and subscript a<-c(1,2,3,4) b<-c(1,2,3,4) plot(x=a,y=b, ylab=expression(paste("Apple"["P"])), xlab=expression(paste("Banana"^"th")), main=expression(paste(italic("i-")~"4"^"th"~"choice"))) Now I would like to include the titel (main) as a function of the number of trails for (trial in 1:nTrials) { plot( main=expression(paste(italic("i-")~"trial"^"th"~"choice"))) } e.g. nTrials = 5 The title should look like this: 5th plot: i ^th choice 4th plot: i-1 ^th choice 3th plot: i-2 ^th choice and so on I have problems to create that, could you please help me? Thank you!! [[alternative HTML version deleted]] ______________________________________________ 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.