R - линии оси pca - PullRequest
       28

R - линии оси pca

0 голосов
/ 30 апреля 2020

Я пытаюсь выяснить, почему мои линии осей PCA не отображаются на моем окончательном графике PCA График PCA без линий осей , но ничего не выделяется. Я пытался пройтись и комментировать части кода, но это не помогло. Любая помощь приветствуется!

Данные: https://pastebin.com/MWNfUz3S

#not all of these are used for the pca portion of my code but all are used at some point
library(readxl)
library(dplyr)
library(broom)
library(tidyr)
library(ggthemes)
library(ggplot2)
library(gridExtra)
library(cowplot)
library(gtable)
library(ggfortify)
library(data.table)
library(vegan)
library(ggrepel)
library(forcats)
library(scales)


input <-"End Members 2020_03.xls"
mydata<-read_excel(input,sheet=1)
rm(input)

df<-mydata[,c(5:24)]
pca<-rda(df,scale=T) #Conducting a correlation PCA
#Percent variance explained by PCs
pervar<-round(pca$CA$eig/sum(pca$CA$eig)*100,2)
#Plot values and limits for all LOP plots
scrs<-scores(pca,display=c("sites","species"))
xlim<-with(scrs,range(species[,1],sites[,1]))*1.1
ylim<-with(scrs,range(species[,2],sites[,2]))*1.1
ordlab<-ordipointlabel(pca,display="species")$labels

factor.scores<-as.data.frame(pca$CA$v) %>% select(PC1,PC2) %>% mutate(variable=row.names(.),PC1=PC1)
factor.scores.alt<-as.data.frame(scrs$species)
site.scores<-as.data.frame((pca$CA$u)) %>% select(PC1,PC2)

pca.scores<-as.data.frame(scrs$sites)
mydata$PC1<-pca.scores$PC1
mydata$PC2<-pca.scores$PC2

#Generate better labels in PCA plot
variables<-factor.scores.alt %>% mutate(PC1.adj=PC1*1.7,PC2.adj=PC2*1.7,labels=rownames(factor.scores.alt))

landuse.color.and.season.shape.pca<-ggplot(mydata,aes(PC1,PC2))+
  geom_vline(xintercept=0.0)+
  geom_hline(yintercept=0.0)+
  #geom_point(aes(shape=Season,color=reorder(Landuse,Classification)),size=2)+
  geom_point(aes(shape=Season,color=Landuse),size=3)+
  geom_text_repel(data=variables,aes(PC1.adj,PC2.adj,label=labels),size=3,fontface="plain",family="serif")+
  scale_color_gdocs(name="Landuse")+
  #scale_shape_discrete(solid=T,name="Seasons")+
  xlim(-3,3)+
  ylim(3,-3)+
  xlab("PC1 [56.4%]")+
  ylab("PC2 [20.2%]")+
  theme(legend.text=element_text(size=10,face="plain",family="serif"),
        legend.title=element_text(size=10,face="plain",family="serif"),
        axis.title=element_text(size=10,face="plain",family="serif"),
        axis.text=element_text(size=10,face="plain",family="serif"),
        plot.background=element_blank())

pca.legend<-get_legend(landuse.color.and.season.shape.pca + theme(legend.text=element_text(size=8),
                                                                  legend.title=element_text(size=10),
                                                                  text=element_text(family="serif"),
                                                                  legend.position="bottom",
                                                                  legend.title.align=0.5,
                                                                  legend.box="horizontal",
                                                                  legend.box.just="top",
                                                                  legend.background=element_rect(fill="white",colour=F)))

pca.plot.final<-plot_grid(pca.legend,
                          landuse.color.and.season.shape.pca + theme(legend.position="none",
                                                                     panel.background=element_blank()),
                          nrow=2,rel_heights=c(0.1,1.1))

pca.plot.final

Эта часть поста на самом деле не связана с вопросом, а скорее используется для этот пост будет принят. Очевидно, что если вам нужно включить больше подробностей с большим количеством кода, который вы предоставляете. У меня нет больше подробностей, чтобы добавить, так что вот мы ... Надеюсь, у всех все хорошо.

...