Re: [R-sig-eco] doubt in generating loops with For
Hi Vít, Thank you for your quick response. Indeed, I did not put all the code, because I thought it would mess up more than help. But I'll put here the full code. I realized the issue of columns, I had come to this conclusion also. However, I would like to generate a "random" matrix of community based in a "real" community in every loop, and likewise, generating a diversity measure of these communities, also in each loop. If you can help me with this question , I'd appreciate. Code: require (vegan) comm<-t(data.frame(sp1=rpois(10, 1), sp2=rpois(10,1), sp3=rpois(10,1), sp4=rpois(10,1), sp5=rpois(10,1))) traits<-t(data.frame(sp1=rnorm(5,1.2,.2),sp2=rnorm(5,2,.5), sp3=rnorm(5,3,.3), sp4=rnorm(5,0.5,.2), sp5=rnorm(5,.9,.6))) dist<-vegdist(traits,"gower") comm<-as.matrix(comm) func.sim<-data.frame(rep(NA, 1000)) for (z in 1:1000) { comm1<-apply(comm,2,sample) func.sim[z,]<- (t(comm[, z]) %*% (as.matrix(dist)^2) %*% comm[, z])/2/(sum(comm[, z])^2) } Error in comm[ ,z] : subscript out of bounds Best regards, Thiago _ *Thiago A. Pires* Ph.D. student in Ecology - Instituto de Biologia - UNICAMP Collaborator Researcher - Laboratório de Ecologia e Sistemática - UNIFESP Email: pires.thiagoaugu...@gmail.com / Skype: thiago.augusto.pires Home Page: http://thiagoaugustopires.wix.com/naturevisions CV Lattes: http://buscatextual.cnpq.br/buscatextual/visualizacv.do?id=K4298681Z0 _ 2015-04-16 4:40 GMT-03:00 Vít Syrovátka : > Hi Thiago, > I am not familiar with the diversity measure you are trying to calculate > and your example code is not complete - one can not simulate your comm and > dist objects. > But at the first sight one can see that in your loop you are trying to > select columns 1, 2, 3,... 1000 from the comm matrix, which probably has > only 10 columns (if sites are in columns and species in rows, contrary to > how such data are traditionally stored). > > for(z in 1:10) > instead of > for(z in 1:1000) > should do the job. > > Vit > > Dne 2015-04-15 23:24, Thiago A. Pires napsal: > >> Hi guys, >> >> I have a question when trying to generate 1000 measures with "for" >> function. For example, I have 10 locations with 5 species , and I want to >> apply a diversity measure on each site. But when I try to run one is with >> my function, I can not, I get an error message: "Error in com[, z] : >> subscript out of bounds", and I think it should be a simple mistake, but >> I'm not seeing. If you guys can help with it, I would appreciate. Right >> below is an example for my code: >> >> comm= a community matrix with 10 sites and 5 species >> dist= a pairwise distance-based matrix of species >> >> for (z in 1:1000) >> { >> >> func.sim[z,]<- (t(comm[, z]) %*% (as.matrix(dist)^2) %*% >> comm[, z])/2/(sum(comm[, z])^2) >> >> } >> >> >> Error in comm[, z] : subscript out of bounds >> >> >> >> >> >> >> Best regards, >> >> Thiago. >> >> >> _ >> >> *Thiago A. Pires* >> Ph.D. student in Ecology - Instituto de Biologia - UNICAMP >> Collaborator Researcher - Laboratório de Ecologia e Sistemática - UNIFESP >> >> >> Email: pires.thiagoaugu...@gmail.com / Skype: thiago.augusto.pires >> Home Page: >> http://thiagoaugustopires.wix.com/naturevisions >> CV Lattes: >> http://buscatextual.cnpq.br/buscatextual/visualizacv.do?id=K4298681Z0 >> _ >> >> [[alternative HTML version deleted]] >> >> ___ >> R-sig-ecology mailing list >> R-sig-ecology@r-project.org >> https://stat.ethz.ch/mailman/listinfo/r-sig-ecology >> > [[alternative HTML version deleted]] ___ R-sig-ecology mailing list R-sig-ecology@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-ecology
Re: [R-sig-eco] Change ggplot legend
Why don't you just adapt it in the dataframe and make sure the levels in the Sp column are renamed to "Sp1" and "Sp2"? http://www.cookbook-r.com/Manipulating_data/Renaming_levels_of_a_factor/ grs, Stijn On 04/16/2015 11:26 AM, Roman Luštrik wrote: Can you provide a reproducible example (using some available dataset or simulate some data) and perhaps mark on the picture what you want to have changed? Cheers, Roman On Thu, Apr 16, 2015 at 11:11 AM, Luis Fernando García wrote: Dear R experts, I am trying to make a new plot. I have already finished and I just want to change the color for the axis number and the legends, I have tried to make the last function by removing the legend and adding a new one but it did not work. If any of you knows how to correct this, I would really appreciate it. My aim is to rename the series as "Sp1" and "Sp2". Thanks in advance! HEre is the plot code p <- ggplot(t,aes(x=Ratio,y=Time)) + geom_point(aes(shape=factor(Sp))) p + geom_smooth(aes(linetype=factor(Sp), ),colour="black", method='lm', se=F)+theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),panel.background = element_blank(), axis.line = element_line(colour = "black"))+ theme(legend.position="none")+#Remove legend to add a new one scale_shape_discrete(name ="Species",labels=c("Sp1", "Sp2")) [[alternative HTML version deleted]] ___ R-sig-ecology mailing list R-sig-ecology@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-ecology -- -- ir. Stijn Van Hoey BIOMATH Department of Mathematical Modelling, Statistics and Bioinformatics Ghent University - Faculty of Bioscience Engineering Coupure Links 653, 9000 Gent, Belgium TEL: +32 (0)9 264 5935 ___ R-sig-ecology mailing list R-sig-ecology@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-ecology
Re: [R-sig-eco] Change ggplot legend
Sure! I can provide a sample graph I want to get the axis values to be black and remove the legend with the name "factor(Sp)" and get the values for the axis to be black! Thanks! This is the code t<-read.table ("covartimesfinal2.txt", header=T) attach(t) p <- ggplot(t,aes(x=Ratio,y=Time)) + geom_point(aes(shape=factor(Sp))) p + geom_smooth(aes(linetype=factor(Sp), ),colour="black", method='lm', se=F)+theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),panel.background = element_blank(), axis.line = element_line(colour = "black"))+ scale_shape_discrete(name ="Species",labels=c("Sp1", "Sp2")) 2015-04-16 6:26 GMT-03:00 Roman Luštrik : > Can you provide a reproducible example (using some available dataset or > simulate some data) and perhaps mark on the picture what you want to have > changed? > > Cheers, > Roman > > > On Thu, Apr 16, 2015 at 11:11 AM, Luis Fernando García < > luysgar...@gmail.com> wrote: > >> Dear R experts, >> >> I am trying to make a new plot. I have already finished and I just want to >> change the color for the axis number and the legends, I have tried to make >> the last function by removing the legend and adding a new one but it did >> not work. >> >> If any of you knows how to correct this, I would really appreciate it. My >> aim is to rename the series as "Sp1" and "Sp2". >> >> Thanks in advance! >> >> HEre is the plot code >> >> p <- ggplot(t,aes(x=Ratio,y=Time)) + geom_point(aes(shape=factor(Sp))) >> >> p + geom_smooth(aes(linetype=factor(Sp), ),colour="black", method='lm', >> se=F)+theme(panel.grid.major = element_blank(), panel.grid.minor = >> element_blank(),panel.background = element_blank(), axis.line = >> element_line(colour = "black"))+ >> >> theme(legend.position="none")+#Remove legend to add a new one >> >> scale_shape_discrete(name ="Species",labels=c("Sp1", "Sp2")) >> >> [[alternative HTML version deleted]] >> >> ___ >> R-sig-ecology mailing list >> R-sig-ecology@r-project.org >> https://stat.ethz.ch/mailman/listinfo/r-sig-ecology >> > > > > -- > In God we trust, all others bring data. > Sp TimeRatio H 17 0.85 H 13 0.84 H 7 1.18 H 9 0.90 H 22 2.10 H 12 1.18 H 9 1.10 H 10 0.81 H 11 0.95 H 18 1.75 H 11 1.18 H 24 1.14 H 16 1.27 N 7 2.23 N 9 3.25 N 8 2.92 N 10 4.19 N 10 1.86 N 9 2.03 N 6 1.59 N 4 1.58 N 7 2.44 N 8 1.46 N 8 2.21 N 8 1.48 N 15 1.5 N 15 2.1 N 8 1.58 N 10 2.03 N 12 1.92 N 7 1.49 N 12 2.1 N 7 2.45 N 7 1.74 N 6 1.81 N 9 1.7 N 11 1.47 N 10 1.85 N 6 2.29 ___ R-sig-ecology mailing list R-sig-ecology@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-ecology
Re: [R-sig-eco] Change ggplot legend
To change the axis text color, use theme(). Compare library(ggplot2) ggplot(PlantGrowth, aes(x=group, y=weight)) + geom_boxplot() to ggplot(PlantGrowth, aes(x=group, y=weight)) + geom_boxplot() + theme(axis.text = element_text(color = "black")) To change legend title, see http://www.cookbook-r.com/Graphs/Legends_(ggplot2)/#modifying-the-text-of-legend-titles-and-labels Cheers, Roman On Thu, Apr 16, 2015 at 11:36 AM, Luis Fernando García wrote: > Sure! > > I can provide a sample graph > > I want to get the axis values to be black and remove the legend with the > name "factor(Sp)" and get the values for the axis to be black! > > Thanks! > > > This is the code > > t<-read.table ("covartimesfinal2.txt", header=T) > attach(t) > p <- ggplot(t,aes(x=Ratio,y=Time)) + geom_point(aes(shape=factor(Sp))) > p + geom_smooth(aes(linetype=factor(Sp), ),colour="black", method='lm', > se=F)+theme(panel.grid.major = element_blank(), panel.grid.minor = > element_blank(),panel.background = element_blank(), axis.line = > element_line(colour = "black"))+ > scale_shape_discrete(name ="Species",labels=c("Sp1", "Sp2")) > > 2015-04-16 6:26 GMT-03:00 Roman Luštrik : > >> Can you provide a reproducible example (using some available dataset or >> simulate some data) and perhaps mark on the picture what you want to have >> changed? >> >> Cheers, >> Roman >> >> >> On Thu, Apr 16, 2015 at 11:11 AM, Luis Fernando García < >> luysgar...@gmail.com> wrote: >> >>> Dear R experts, >>> >>> I am trying to make a new plot. I have already finished and I just want >>> to >>> change the color for the axis number and the legends, I have tried to >>> make >>> the last function by removing the legend and adding a new one but it did >>> not work. >>> >>> If any of you knows how to correct this, I would really appreciate it. My >>> aim is to rename the series as "Sp1" and "Sp2". >>> >>> Thanks in advance! >>> >>> HEre is the plot code >>> >>> p <- ggplot(t,aes(x=Ratio,y=Time)) + geom_point(aes(shape=factor(Sp))) >>> >>> p + geom_smooth(aes(linetype=factor(Sp), ),colour="black", method='lm', >>> se=F)+theme(panel.grid.major = element_blank(), panel.grid.minor = >>> element_blank(),panel.background = element_blank(), axis.line = >>> element_line(colour = "black"))+ >>> >>> theme(legend.position="none")+#Remove legend to add a new one >>> >>> scale_shape_discrete(name ="Species",labels=c("Sp1", "Sp2")) >>> >>> [[alternative HTML version deleted]] >>> >>> ___ >>> R-sig-ecology mailing list >>> R-sig-ecology@r-project.org >>> https://stat.ethz.ch/mailman/listinfo/r-sig-ecology >>> >> >> >> >> -- >> In God we trust, all others bring data. >> > > -- In God we trust, all others bring data. [[alternative HTML version deleted]] ___ R-sig-ecology mailing list R-sig-ecology@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-ecology
Re: [R-sig-eco] Change ggplot legend
Can you provide a reproducible example (using some available dataset or simulate some data) and perhaps mark on the picture what you want to have changed? Cheers, Roman On Thu, Apr 16, 2015 at 11:11 AM, Luis Fernando García wrote: > Dear R experts, > > I am trying to make a new plot. I have already finished and I just want to > change the color for the axis number and the legends, I have tried to make > the last function by removing the legend and adding a new one but it did > not work. > > If any of you knows how to correct this, I would really appreciate it. My > aim is to rename the series as "Sp1" and "Sp2". > > Thanks in advance! > > HEre is the plot code > > p <- ggplot(t,aes(x=Ratio,y=Time)) + geom_point(aes(shape=factor(Sp))) > > p + geom_smooth(aes(linetype=factor(Sp), ),colour="black", method='lm', > se=F)+theme(panel.grid.major = element_blank(), panel.grid.minor = > element_blank(),panel.background = element_blank(), axis.line = > element_line(colour = "black"))+ > > theme(legend.position="none")+#Remove legend to add a new one > > scale_shape_discrete(name ="Species",labels=c("Sp1", "Sp2")) > > [[alternative HTML version deleted]] > > ___ > R-sig-ecology mailing list > R-sig-ecology@r-project.org > https://stat.ethz.ch/mailman/listinfo/r-sig-ecology > -- In God we trust, all others bring data. [[alternative HTML version deleted]] ___ R-sig-ecology mailing list R-sig-ecology@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-ecology
[R-sig-eco] Change ggplot legend
Dear R experts, I am trying to make a new plot. I have already finished and I just want to change the color for the axis number and the legends, I have tried to make the last function by removing the legend and adding a new one but it did not work. If any of you knows how to correct this, I would really appreciate it. My aim is to rename the series as "Sp1" and "Sp2". Thanks in advance! HEre is the plot code p <- ggplot(t,aes(x=Ratio,y=Time)) + geom_point(aes(shape=factor(Sp))) p + geom_smooth(aes(linetype=factor(Sp), ),colour="black", method='lm', se=F)+theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),panel.background = element_blank(), axis.line = element_line(colour = "black"))+ theme(legend.position="none")+#Remove legend to add a new one scale_shape_discrete(name ="Species",labels=c("Sp1", "Sp2")) [[alternative HTML version deleted]] ___ R-sig-ecology mailing list R-sig-ecology@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-ecology
Re: [R-sig-eco] doubt in generating loops with For
Hi Thiago, I am not familiar with the diversity measure you are trying to calculate and your example code is not complete - one can not simulate your comm and dist objects. But at the first sight one can see that in your loop you are trying to select columns 1, 2, 3,... 1000 from the comm matrix, which probably has only 10 columns (if sites are in columns and species in rows, contrary to how such data are traditionally stored). for(z in 1:10) instead of for(z in 1:1000) should do the job. Vit Dne 2015-04-15 23:24, Thiago A. Pires napsal: Hi guys, I have a question when trying to generate 1000 measures with "for" function. For example, I have 10 locations with 5 species , and I want to apply a diversity measure on each site. But when I try to run one is with my function, I can not, I get an error message: "Error in com[, z] : subscript out of bounds", and I think it should be a simple mistake, but I'm not seeing. If you guys can help with it, I would appreciate. Right below is an example for my code: comm= a community matrix with 10 sites and 5 species dist= a pairwise distance-based matrix of species for (z in 1:1000) { func.sim[z,]<- (t(comm[, z]) %*% (as.matrix(dist)^2) %*% comm[, z])/2/(sum(comm[, z])^2) } Error in comm[, z] : subscript out of bounds Best regards, Thiago. _ *Thiago A. Pires* Ph.D. student in Ecology - Instituto de Biologia - UNICAMP Collaborator Researcher - Laboratório de Ecologia e Sistemática - UNIFESP Email: pires.thiagoaugu...@gmail.com / Skype: thiago.augusto.pires Home Page: http://thiagoaugustopires.wix.com/naturevisions CV Lattes: http://buscatextual.cnpq.br/buscatextual/visualizacv.do?id=K4298681Z0 _ [[alternative HTML version deleted]] ___ R-sig-ecology mailing list R-sig-ecology@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-ecology ___ R-sig-ecology mailing list R-sig-ecology@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-ecology
[R-sig-eco] doubt in generating loops with For
Hi guys, I have a question when trying to generate 1000 measures with "for" function. For example, I have 10 locations with 5 species , and I want to apply a diversity measure on each site. But when I try to run one is with my function, I can not, I get an error message: "Error in com[, z] : subscript out of bounds", and I think it should be a simple mistake, but I'm not seeing. If you guys can help with it, I would appreciate. Right below is an example for my code: comm= a community matrix with 10 sites and 5 species dist= a pairwise distance-based matrix of species for (z in 1:1000) { func.sim[z,]<- (t(comm[, z]) %*% (as.matrix(dist)^2) %*% comm[, z])/2/(sum(comm[, z])^2) } Error in comm[, z] : subscript out of bounds Best regards, Thiago. _ *Thiago A. Pires* Ph.D. student in Ecology - Instituto de Biologia - UNICAMP Collaborator Researcher - Laboratório de Ecologia e Sistemática - UNIFESP Email: pires.thiagoaugu...@gmail.com / Skype: thiago.augusto.pires Home Page: http://thiagoaugustopires.wix.com/naturevisions CV Lattes: http://buscatextual.cnpq.br/buscatextual/visualizacv.do?id=K4298681Z0 _ [[alternative HTML version deleted]] ___ R-sig-ecology mailing list R-sig-ecology@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-ecology