[R] Mirror Image on Biplot Graphic

Mark Difford mark_difford at yahoo.co.uk
Tue Oct 7 23:34:54 CEST 2008


Hi Rodrigo,

This is because [I now see] you are working on a capscale object, not a cca
object, which is what I thought you were using. It still works, though, even
with c(2:8,15), though DF #15 doesn't exist in capscale objects. To show you
that this is so, run the example below, which uses a data set from vegan. I
have dropped #15 from the selection set.

## Ex. of switching axes of capscale object
windows(); par(mfrow=c(2,2))
data(varespec)
data(varechem)
vare.cap <- capscale(varespec ~ N + P + K + Condition(Al), varechem,
dist="bray")
plot(vare.cap)
for(i in c(2:8)) vare.cap$CCA[[i]][, 1]<-vare.cap$CCA[[i]][, 1] * -1
plot(vare.cap)
for(i in c(2:8)) vare.cap$CCA[[i]][, 2]<-vare.cap$CCA[[i]][, 2] * -1
plot(vare.cap)
for(i in c(2:8)) vare.cap$CCA[[i]][, 1:2]<-vare.cap$CCA[[i]][, 1:2] * -1
plot(vare.cap)

Cheers, Mark.


Rodrigo Aluizio wrote:
> 
> Well this time I have to assume, I'm not understanding what is wrong now.
> And I have to say: 'Thank You for your patience', cause I'm going crazy 
> here! :-)
> 
> Mark, I tried this:
> 
> CAPpotiFTI<-CAPpotiFT
> for(i in c(2:8,15)) CAPpotiFTI$CCA[[i]][, 1]<-CAPpotiFTI$CCA[[i]][, 1] *
> -1
> 
> and got this:
> 
> Erro em CAPpotiFTI$CCA[[i]] : índice fora de limites (index out of limits)
> 
> Here is the script until this point
> 
> library(vegan)
> library(xlsReadWrite)
> #------------------------FT----------------------#
> PotiAbioFT<-read.xls('FatorialReplica.xls',sheet=4,rowNames=T)
> PotiBioFT<-read.xls('FatorialReplica.xls',sheet=6,rowNames=T)
> attach(PotiAbioFT)
> LogPotiBioFT<-log(PotiBioFT+1)
> CAPpotiFT<-capscale(t(LogPotiBioFT)~Envoronmental+Variables,dist="bray",add=T)
> PermCAPFT<-anova.cca(CAPpotiFT,alpha=0.05,model='full',first=F,permutations=999)
> PermCAPFT
> summary(CAPpotiFT)
> # Rotating the axis
> CAPpotiFTI<-CAPpotiFT
> for(i in c(2:8,15)) CAPpotiFTI$CCA[[i]][, 1]<-CAPpotiFTI$CCA[[i]][, 1] *
> -1
> 
> 
> 
> --------------------------------------------------
> From: "Mark Difford" <mark_difford at yahoo.co.uk>
> Sent: Tuesday, October 07, 2008 5:17 PM
> To: <r-help at r-project.org>
> Subject: Re: [R] Mirror Image on Biplot Graphic
> 
>>
>> Hi Rodrigo,
>>
>> Yes it does, but this will work. You have erroneously used a "," in your
>> call, it should be a ".", if anything. You have also inserted "cca"...
>> And
>> the axes being switched must be conformant:
>>
>>>> CAPpotiFTI<-CAPpotiFT
>>>> for(i in c(2:8,15))
>>>> CAPpotiFTI$CCA[[i]][,2]<-CAPpotiFTI,cca$CCA[[i]][,1]*-1  ## you are
>>>> writing axis 1 over axis 2; and there is no "...,cca..."
>>>>>>>>>>>>>>>Erro: unexpected ',' in "for(i in c(2:8,15))   ## unexpected
>> comma
>>>> CAPpotiFTI$CCA[[i]][,2]<-CAPpotiFTI,"
>>
>> ## Using your example to switch axis 1. Make sure that CAPpotiFT exists 
>> and
>> is your
>> ## original object. Copy and paste the lines below into your R console
>> window and run
>> CAPpotiFTI <- CAPpotiFT
>> for(i in c(2:8,15)) CAPpotiFTI$CCA[[i]][, 1]<-CAPpotiFTI$CCA[[i]][, 1] 
>> * -1
>>
>> Cheers, Mark.
>>
>>
>> Rodrigo Aluizio wrote:
>>>
> [[elided Yahoo spam]]
>>> Ah well, I tried the this other way, but it gave me an error.
>>> Something in the syntax, I tried to change something but didn't fix the
>>> error.
>>>
>>> CAPpotiFTI<-CAPpotiFT
>>>  for(i in c(2:8,15))
>>> CAPpotiFTI$CCA[[i]][,2]<-CAPpotiFTI,cca$CCA[[i]][,1]*-1
>>> Erro: unexpected ',' in "for(i in c(2:8,15))
>>> CAPpotiFTI$CCA[[i]][,2]<-CAPpotiFTI,"
>>>
>>>  for(i in c(2:8,15)) CAPpotiFTI$CCA[[i]][,1]<-CAPpotiFTI$CCA[[i]][,1]*-1
>>> Erro em CAPpotiFTI$CCA[[i]] : índice fora de limites (index out of 
>>> limits)
>>>
>>> Well, it needs lots of patience...
>>>
>>> --------------------------------------------------
> 
>>> Sent: Tuesday, October 07, 2008 3:50 PM
>>> To: <r-help at r-project.org>
>>> Subject: Re: [R] Mirror Image on Biplot Graphic
>>>
>>>>
>>>> Hi Rodrigo,
>>>>
>>>> Again an error, as that doesn't touch one of the data structures. You
>>>> need
>>>> to extend the range to include #15, as below:
>>>>
>>>> ## This does axis 2
>>>> mynew.cca <- my.cca
>>>> for (i in c(2:8,15)) mynew.cca$CCA[[i]][, 2] <- mynew,cca$CCA[[i]][, 2]
>>>> * -1
>>>>
>>>> Cheers, Mark.
>>>>
>>>>
>>>> Mark Difford wrote:
>>>>>
>>>>> Hi Rodrigo,
>>>>>
>>>>> I looked through my scripts and found my old hack. __WARNING__: Be
>>>>> very
>>>>> careful to check that
>>>>> the mirror isn't broken and that all is in order! Make sure that you 
>>>>> are
>>>>> reversing the axes you want. In your original post you mention axis 2,
>>>>> i.e. y-axis. Below you have used axis 1.
>>>>>
>>>>> ## This does axis 2
>>>>> mynew.cca <- my.cca
>>>>> for (i in 2:8) mynew.cca$CCA[[i]][, 2] <- mynew,cca$CCA[[i]][, 2] * -1
>>>>>
>>>>> plot(mynew.cca)
>>>>>
>>>>> ## This does axis 1
>>>>> for (i in 2:8) mynew.cca$CCA[[i]][, 1] <- mynew,cca$CCA[[i]][, 1] * -1
>>>>>
>>>>> Regards, Mark.
>>>>>
>>>>>
>>>>> Rodrigo Aluizio wrote:
>>>>>>
>>>>>> Ok Mark, it worked for the species.
>>>>>> I still get an error if I try with biplot (cn,sp), but it's not a
>>>>>> problem,
>>>>>> repositioning the species is enough.
>>>>>> Thanks once again.
>>>>>>
>>>>>> Just for future consults the error that still remains:
>>>>>> CAPpotiFTI<-scores(CAPpotiFT, display=c('bp','species','cn','sites')
>>>>>> CAPpotiFTI$species[,1]<-CAPpotiFTI$species[,1]*-1
>>>>>> CAPpotiFTI$cn[,1]<-CAPpotiFTI$cn[,1]*-1
>>>>>> CAPpotiFTI$sites[,1]<-CAPpotiFTI$sites[,1]*-1
>>>>>> CAPpotiFTI$bp[,1]<-CAPpotiFTI$bp[,1]*-1
>>>>>> 
>>>>>> plot.cca(CAPpotiFTI,type='none',display=c('bp','species'),main='Total
>>>>>> Fauna
>>>>>> + Species x Environment')
>>>>>> Erro em match.arg(display) : 'arg' must be of length 1
>>>>>>
>>>>>> or
>>>>>>
>>>>>> text.cca(scores(CAPpotiFTI$biplot),col=323232,cex=0.6,lwd=2,lty='dotted')
>>>>>> Doesn't return an error, but it's not a mirror image as expected,
>>>>>> it's
>>>>>> nonsense.
>>>>>>
>>>>>> --------------------------------------------------
>>>
>>>>>> Sent: Tuesday, October 07, 2008 12:22 PM
>>>>>> To: <r-help at r-project.org>
>>>>>> Subject: Re: [R] Mirror Image on Biplot Graphic
>>>>>>
>>>>>>>
>>>>>>> Hi Rodrigo,
>>>>>>>
>>>>>>> Sorry again: the structure vegan uses is quite complex. Technically
>>>>>>> you
>>>>>>> should use the extractor function scores() to extract what you want
>>>>>>> from
>>>>>>> the
>>>>>>> object, e.g. scores(my.cca, display="species") or scores(my.cca,
>>>>>>> display="cn") [centroids]. I did work out a quicker way of reversing
>>>>>>> axes,
>>>>>>> but that seems to have gone walking...and I am not using vegan at
>>>>>>> the
>>>>>>> moment.
>>>>>>>
>>>>>>> Quickest hack is to make a copy of the objects you want to plot,
>>>>>>> reversing
>>>>>>> orientations along the way. So do (my.cca is your original object):
>>>>>>>
>>>>>>> Temp.data <- scores(my.cca, display=c("sites", "species", "cn"))
>>>>>>>
>>>>>>> ## Reverse yaxes
>>>>>>> Temp.data$sites[, 2] <- Temp.data$sites[, 2] * -1
>>>>>>> Temp.data$species[, 2] <- Temp.data$species[, 2] * -1
>>>>>>> Temp.data$centroid[, 2] <- Temp.data$centroid[, 2] * -1
>>>>>>>
>>>>>>> Do this for all the things you are plotting whose axes you want
>>>>>>> reversed.
>>>>>>> Check this against your original plot to ensure that the mirror
>>>>>>> isn't
>>>>>>> broken.
>>>>>>>
>>>>>>> You can then use the Temp.data with the scores() function to make 
>>>>>>> your
>>>>>>> plot.
>>>>>>>
>>>>>>> ## Mock e.g.
>>>>>>> points(scores(Temp.data, display="sites"), pch=21)
>>>>>>>
>>>>>>> Regards, Mark.
>>>>>>>
>>>>>>>
>>>>>>> Rodrigo Aluizio wrote:
>>>>>>>>
>>>>>>>>> Thanks a lot for your help (again) Mark.
>>>>>>>>> I adapted your suggestion to my analysis.
>>>>>>>>> But I'm getting and error when trying to apply the new values.
>>>>>>>>> The error is:
>>>>>>>>>
>>>>>>>>> Erro em scores(CAPpotiFTI)$species[, 2] <-
>>>>>>>>> scores(CAPpotiFTI)$species[,
>>>>>>>>> :
>>>>>>>>>  não foi posssível encontrar a função "scores<-"
>>>>>>>>>
>>>>>>>>> Translating:
>>>>>>>>> Error at scores(CAPpotiFTI)$species[, 2] <-
>>>>>>>>> scores(CAPpotiFTI)$species[,
>>>>>>>>> :
>>>>>>>>>  it's not possible to find the function "scores<-"
>>>>>>>>>
>>>>>>>>> Bellow how I applied the suggestion:
>>>>>>>>> CAPpotiFTI<-CAPpotiFT
>>>>>>>>> scores(CAPpotiFTI)$species[,2]<-scores(CAPpotiFTI)$species[,2]*-1
>>>>>>>>> scores(CAPpotiFTI)$sites[,2]<-scores(CAPpotiFTI)$sites[,2]*-1
>>>>>>>>>
>>>>>>>>> Any ideas? I'm trying similar things but without success.
>>>>>>>>>
>>>>>>>>> --------------------------------------------------
>>>>>>
>>>>>>>>> Sent: Tuesday, October 07, 2008 10:34 AM
>>>>>>>>> To: <r-help at r-project.org>
>>>>>>>>> Subject: Re: [R] Mirror Image on Biplot Graphic
>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>> Hi Rodrigo,
>>>>>>>>>>
>>>>>>>>>> Sorry, that will not return a result (I use several different
>>>>>>>>>> ordination
>>>>>>>>>> packages, in most of which this is possible). What you need to do
>>>>>>>>>> with
>>>>>>>>>> vegan
>>>>>>>>>> is the following:
>>>>>>>>>>
>>>>>>>>>> scores(mynew.cca)$species[, 2] <- scores(mynew.cca)$species[, 2]
>>>>>>>>>> * -1
>>>>>>>>>>
>>>>>>>>>> You will be able to do the rest.
>>>>>>>>>>
>>>>>>>>>> Regards, Mark.
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>> Mark Difford wrote:
>>>>>>>>>>>
>>>>>>>>>>> Hi Rodrigo,
>>>>>>>>>>>
>>>>>>>>>>>>> I need to rotate on y axis the lines and symbols of
>>>>>>>>>>>>> constrained
>>>>>>>>>>>>> and
>>>>>>>>>>>>> sites representation.
>>>>>>>>>>>
>>>>>>>>>>> Easiest is to multiply the axis you want to invert by -1.
>>>>>>>>>>> Something
>>>>>>>>>>> like
>>>>>>>>>>> the following, where my.cca is the orginal object and yax =
>>>>>>>>>>> obj[,
>>>>>>>>>>> 2]
>>>>>>>>>>> (xax
>>>>>>>>>>> being obj[, 1]). Obviously, copying isn't necessary.
>>>>>>>>>>>
>>>>>>>>>>> mynew.cca <- my.cca
>>>>>>>>>>> mynew.cca$scores$species[, 2] <- my.cca$scores$species[, 2] * -1
>>>>>>>>>>> mynew.cca$scores$sites[, 2] <- my.cca$scores$sites[, 2] * -1
>>>>>>>>>>> mynew.cca$scores$centroids[, 2] <- my.cca$scores$centroids[, 2]
>>>>>>>>>>> * -1
>>>>>>>>>>>
>>>>>>>>>>> Regards, Mark.
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>> Rodrigo Aluizio wrote:
>>>>>>>>>>>>
>>>>>>>>>>>> He everybody,
>>>>>>>>>>>> Well I have a biplot CCA-like origined from plot.cca (vegan
>>>>>>>>>>>> package).
>>>>>>>>>>>> I
>>>>>>>>>>>> need to rotate on y axis the lines and symbols of constrained 
>>>>>>>>>>>> and
>>>>>>>>>>>> sites
>>>>>>>>>>>> representation. If I do that on an image editor, I rotate
>>>>>>>>>>>> everything,
>>>>>>>>>>>> including titles, axes labels and positions. I just need to
>>>>>>>>>>>> rotate
>>>>>>>>>>>> the
>>>>>>>>>>>> inner par and keep the variables names (constrained) and
>>>>>>>>>>>> symbols
>>>>>>>>>>>> in
>>>>>>>>>>>> the
>>>>>>>>>>>> new positions but with the right direction.
>>>>>>>>>>>> So, is there on R a way to do that while creating the image?
>>>>>>>>>>>> Here is the code that generate the graphic.
>>>>>>>>>>>>
>>>>>>>>>>>> plot.cca(CAPpotiFT,type='none',display=c('bp','sites'),main='Total
>>>>>>>>>>>> Fauna
>>>>>>>>>>>> Sites x Environment')
>>>>>>>>>>>> text.cca(CAPpotiFT,dis='cn',col='black',cex=0.6,lwd=0.5,lty='dotted')
>>>>>>>>>>>> points(ScoresCAPFT$sites[FatoresRep$BiofaciesBC=='1',],pch=21)
>>>>>>>>>>>> points(ScoresCAPFT$sites[FatoresRep$BiofaciesBC=='2',],pch=20)
>>>>>>>>>>>> points(ScoresCAPFT$sites[FatoresRep$BiofaciesBC=='3',],pch=22,bg='gray')
>>>>>>>>>>>> points(ScoresCAPFT$sites[FatoresRep$BiofaciesBC=='4',],pch=24)
>>>>>>>>>>>> points(ScoresCAPFT$sites[FatoresRep$BiofaciesBC=='5',],pch=24,bg='black')
>>>>>>>>>>>> points(ScoresCAPFT$sites[FatoresRep$BiofaciesBC=='6',],pch=25,bg='gray')
>>>>>>>>>>>>
>>>>>>>>>>>> Thanks in advice.
>>>>>>>>>>>> ___________________________________
>>>>>>>>>>>> MSc. Rodrigo Aluizio
>>>>>>>>>>>> Centro de Estudos do Mar/UFPR
>>>>>>>>>>>> Laboratório de Micropaleontologia
>>>>>>>>>>>>
>>>>>>>>>>>> [[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.
>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>> -- 
>>>>>>>>>> View this message in context:
>>>>>>>>>> http://www.nabble.com/Mirror-Image-on-Biplot-Graphic-tp19857549p19858268.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.
>>>>>>>>>>
>>>>>>>>
>>>>>>>> ______________________________________________
>>>>>>>> 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.
>>>>>>>>
>>>>>>>>
>>>>>>>
>>>>>>> -- 
>>>>>>> View this message in context:
>>>>>>> http://www.nabble.com/Mirror-Image-on-Biplot-Graphic-tp19857549p19860594.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.
>>>>>>>
>>>>>>
>>>>>> ______________________________________________
>>>>>> 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.
>>>>>>
>>>>>>
>>>>>
>>>>>
>>>>
>>>> -- 
>>>> View this message in context:
>>>> http://www.nabble.com/Mirror-Image-on-Biplot-Graphic-tp19857549p19864798.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.
>>>>
>>>
>>> ______________________________________________
>>> 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.
>>>
>>>
>>
>> -- 
>> View this message in context: 
>> http://www.nabble.com/Mirror-Image-on-Biplot-Graphic-tp19857549p19866348.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.
>>
> 
> ______________________________________________
> 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.
> 
> 

-- 
View this message in context: http://www.nabble.com/Mirror-Image-on-Biplot-Graphic-tp19857549p19867815.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list