Dear R users, I have a dataframe with 6 columns and I want to create a plot (with for loop). My question is how to add points in the for loop? I tried this code, but without success. vv<-rbind(colnames(ext)) for(i in 1:ncol(vv)){ with(ext, plot(rownames(ext), ext[,i], type="p",las=1, bty="n",cex.main=2, cex.axis=1.5)) } Please help me to solve that! Best regards! CR -- - - Catalin-Constantin ROIBU ? Lecturer PhD, Forestry engineer Forestry Faculty of Suceava Str. Universitatii no. 13, Suceava, 720229, Romania office phone +4 0230 52 29 78, ext. 531 mobile phone +4 0745 53 18 01 FAX: +4 0230 52 16 64 silvic.usv.ro <http://www.usv.ro/> [[alternative HTML version deleted]]
On 23/03/2016 5:36 AM, catalin roibu wrote:> Dear R users, > > I have a dataframe with 6 columns and I want to create a plot (with for > loop). My question is how to add points in the for loop? I tried this code, > but without success. > > vv<-rbind(colnames(ext)) > for(i in 1:ncol(vv)){ > with(ext, plot(rownames(ext), ext[,i], type="p",las=1, > bty="n",cex.main=2, cex.axis=1.5)) > } > > Please help me to solve that!You can call the points() function to add points to an existing plot. The main difficulty is that the initial call to plot() establishes the axes and coordinate system; if later points fall outside the plot area, they won't be shown. So you may need to work out xlim and ylim in advance. Duncan Murdoch
Martin Maechler
2016-Mar-23 11:29 UTC
[R] adding points in plot loop --> try using matplot() !
>>>>> Duncan Murdoch <murdoch.duncan at gmail.com> >>>>> on Wed, 23 Mar 2016 05:55:43 -0400 writes:> On 23/03/2016 5:36 AM, catalin roibu wrote: >> Dear R users, >> >> I have a dataframe with 6 columns and I want to create a plot (with for >> loop). My question is how to add points in the for loop? I tried this code, >> but without success. >> >> vv<-rbind(colnames(ext)) >> for(i in 1:ncol(vv)){ >> with(ext, plot(rownames(ext), ext[,i], type="p",las=1, >> bty="n",cex.main=2, cex.axis=1.5)) >> } >> >> Please help me to solve that! > You can call the points() function to add points to an existing plot. > The main difficulty is that the initial call to plot() establishes the > axes and coordinate system; if later points fall outside the plot area, > they won't be shown. So you may need to work out xlim and ylim in advance. Indeed! ... and that's why R has inherited the matplot() / matlines() utility functions from S for about 20 years now. These do plot all the columns of a numeric matrix and solve the xlim/ylim problem for you. As the above R code is not reproducible (we do not have your 'ext'), I have not tried if your use of "rownames"/"colnames" is easily portable to matplot. If not (which I doubt), just take matplot() as a template to writer your own function. BTW: Your usage of with(.) seems entirely unnecessary .. Martin Maechler
Hi Anothar approach is to reshape your ext data frame As you did not show your data I use my own.> dput(mikro)structure(list(cas = c(0L, 2L, 3L, 5L, 6L, 8L, 9L, 10L, 12L, 17L, 23L), zr = c(0.19, 0.115, 0.073, 0.068, 0.052, 0.055, 0.048, 0.047, 0.047, 0.045, 0.045), zrred = c(0.145, 0.07, 0.028, 0.023, 0.007, 0.01, 0.003, 0.002, 0.002, 0, 0), drt = c(0.397260273972603, 0.191780821917808, 0.0767123287671233, 0.063013698630137, 0.0191780821917808, 0.0273972602739726, 0.00821917808219179, 0.00547945205479452, 0.00547945205479452, 0, 0), drtf = c(0, 0.589041095890411, 0.723287671232877, 0.863013698630137, 0.904109589041096, 0.950684931506849, 0.968493150684931, 0.975342465753425, 0.986301369863014, 1, 1)), .Names = c("cas", "zr", "zrred", "drt", "drtf"), row.names = c(NA, -11L), class = "data.frame")>library(reshape2) mikro.m<-melt(mikro, id.vars="cas") plot(mikro.m$cas, mikro.m$value, type="n") points(mikro.m$cas, mikro.m$value, pch=as.numeric(mikro.m$variable)) Or you can use ggplot. Cheers Petr> -----Original Message----- > From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of catalin > roibu > Sent: Wednesday, March 23, 2016 10:37 AM > To: R Project Help > Subject: [R] adding points in plot loop > > Dear R users, > > I have a dataframe with 6 columns and I want to create a plot (with for > loop). My question is how to add points in the for loop? I tried this > code, but without success. > > vv<-rbind(colnames(ext)) > for(i in 1:ncol(vv)){ > with(ext, plot(rownames(ext), ext[,i], type="p",las=1, > bty="n",cex.main=2, cex.axis=1.5)) } > > Please help me to solve that! > > Best regards! > > CR > > > -- > > - > - > Catalin-Constantin ROIBU > ? > Lecturer PhD, Forestry engineer > Forestry Faculty of Suceava > Str. Universitatii no. 13, Suceava, 720229, Romania > office phone +4 0230 52 29 78, ext. 531 > mobile phone +4 0745 53 18 01 > FAX: +4 0230 52 16 64 > silvic.usv.ro <http://www.usv.ro/> > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.________________________________ Tento e-mail a jak?koliv k n?mu p?ipojen? dokumenty jsou d?v?rn? a jsou ur?eny pouze jeho adres?t?m. Jestli?e jste obdr?el(a) tento e-mail omylem, informujte laskav? neprodlen? jeho odes?latele. Obsah tohoto emailu i s p??lohami a jeho kopie vyma?te ze sv?ho syst?mu. Nejste-li zam??len?m adres?tem tohoto emailu, nejste opr?vn?ni tento email jakkoliv u??vat, roz?i?ovat, kop?rovat ?i zve?ej?ovat. Odes?latel e-mailu neodpov?d? za eventu?ln? ?kodu zp?sobenou modifikacemi ?i zpo?d?n?m p?enosu e-mailu. V p??pad?, ?e je tento e-mail sou??st? obchodn?ho jedn?n?: - vyhrazuje si odes?latel pr?vo ukon?it kdykoliv jedn?n? o uzav?en? smlouvy, a to z jak?hokoliv d?vodu i bez uveden? d?vodu. - a obsahuje-li nab?dku, je adres?t opr?vn?n nab?dku bezodkladn? p?ijmout; Odes?latel tohoto e-mailu (nab?dky) vylu?uje p?ijet? nab?dky ze strany p??jemce s dodatkem ?i odchylkou. - trv? odes?latel na tom, ?e p??slu?n? smlouva je uzav?ena teprve v?slovn?m dosa?en?m shody na v?ech jej?ch n?le?itostech. - odes?latel tohoto emailu informuje, ?e nen? opr?vn?n uzav?rat za spole?nost ??dn? smlouvy s v?jimkou p??pad?, kdy k tomu byl p?semn? zmocn?n nebo p?semn? pov??en a takov? pov??en? nebo pln? moc byly adres?tovi tohoto emailu p??padn? osob?, kterou adres?t zastupuje, p?edlo?eny nebo jejich existence je adres?tovi ?i osob? j?m zastoupen? zn?m?. This e-mail and any documents attached to it may be confidential and are intended only for its intended recipients. If you received this e-mail by mistake, please immediately inform its sender. Delete the contents of this e-mail with all attachments and its copies from your system. If you are not the intended recipient of this e-mail, you are not authorized to use, disseminate, copy or disclose this e-mail in any manner. The sender of this e-mail shall not be liable for any possible damage caused by modifications of the e-mail or by delay with transfer of the email. In case that this e-mail forms part of business dealings: - the sender reserves the right to end negotiations about entering into a contract in any time, for any reason, and without stating any reasoning. - if the e-mail contains an offer, the recipient is entitled to immediately accept such offer; The sender of this e-mail (offer) excludes any acceptance of the offer on the part of the recipient containing any amendment or variation. - the sender insists on that the respective contract is concluded only upon an express mutual agreement on all its aspects. - the sender of this e-mail informs that he/she is not authorized to enter into any contracts on behalf of the company except for cases in which he/she is expressly authorized to do so in writing, and such authorization or power of attorney is submitted to the recipient or the person represented by the recipient, or the existence of such authorization is known to the recipient of the person represented by the recipient.