From cf7cbdf4507bdd78a8cfceb349558d02dc570266 Mon Sep 17 00:00:00 2001
From: Francesco Sabatini <francesco.sabatini@idiv.de>
Date: Wed, 29 Jul 2020 11:26:27 +0200
Subject: [PATCH] Fixed problem with labeling in Fig6, Created Fig6a,b

---
 01_Mesobromion.R | 80 +++++++++++++++++++++++++++++++++++-------------
 1 file changed, 59 insertions(+), 21 deletions(-)

diff --git a/01_Mesobromion.R b/01_Mesobromion.R
index 98aa7d6..4d68ad4 100644
--- a/01_Mesobromion.R
+++ b/01_Mesobromion.R
@@ -678,9 +678,16 @@ myvectors <- as.data.frame(env.cor) %>%
   mutate(fontface0=ifelse(mylab %in% best.5traits, "bold", "plain")) %>% 
   mutate(category=as.factor(category)) %>% 
   mutate(mycol=ifelse(category=="Trait", "Dark blue", "Dark red"))
+
+basemap0 <- ggplot(data=as.data.frame(pca.out$CA$u[,1:4]*5)) + 
+  theme_bw() + 
+  scale_color_identity() + 
+  scale_y_continuous(limits=c(-1, 1)) + 
+  scale_x_continuous(limits=c(-1, 1)) +  coord_equal() + 
+  theme(panel.grid = element_blank())
   
 
-PCA1_2 <- ggplot() + 
+PCA1_2 <- basemap0 + 
   geom_point(data=as.data.frame(pca.out$CA$u[,1:2]*5), 
              aes(x=PC1, y=PC2), pch="+", size=2, alpha=0.8) + 
   geom_segment(data=myvectors, 
@@ -692,15 +699,10 @@ PCA1_2 <- ggplot() +
              aes(x=PC1, y=PC2, label=mylab, col=mycol, fontface=fontface0), size=2, 
              position = position_dodge(1)) +#, segment.alpha=0.5, segment.colour=gray(0.8)) +
   xlab(paste("PC1 (", varexpl[1], "%)", sep="")) + 
-  ylab(paste("PC2 (", varexpl[2], "%)", sep="")) + 
-  theme_bw() + 
-  scale_color_identity() + 
-  scale_y_continuous(limits=c(-1, 1)) + 
-  scale_x_continuous(limits=c(-1, 1)) +  coord_equal() + 
-  theme(panel.grid = element_blank())
+  ylab(paste("PC2 (", varexpl[2], "%)", sep=""))
 
 
-PCA3_4 <- ggplot() + 
+PCA3_4 <- basemap0 + 
   geom_point(data=as.data.frame(pca.out$CA$u[,3:4]*5), 
              aes(x=PC3, y=PC4), pch="+", size=2, alpha=0.8) + 
   geom_segment(data=myvectors, 
@@ -711,19 +713,53 @@ PCA3_4 <- ggplot() +
              aes(x=PC3, y=PC4, label=mylab, col=mycol, fontface=fontface0), size=2, 
              position = position_dodge(1), segment.alpha=0.8, segment.colour=gray(0.7), segment.size = 0.5) +
   xlab(paste("PC3 (", varexpl[3], "%)", sep="")) + 
-  ylab(paste("PC4 (", varexpl[4], "%)", sep="")) + 
-  theme_bw() + 
-  scale_color_identity() + 
-  scale_y_continuous(limits=c(-1, 1)) + 
-  scale_x_continuous(limits=c(-1, 1)) + 
-  coord_equal() + 
-  theme(panel.grid = element_blank())
+  ylab(paste("PC4 (", varexpl[4], "%)", sep="")) 
 
 PC_beals <- cowplot::plot_grid(PCA1_2,PCA3_4, nrow=1)
 
 ggsave("_pics/Fig6_PC_Beals_1-4.png", width=10, height=5, dpi=300, last_plot())
 
 
+#### 4.1b Alternative showing species scores ####
+tmp <- as.data.frame(pca.out$CA$v[,1:4]*5) %>% 
+  mutate(species0=rownames(pca.out$CA$v)) %>% 
+  mutate(species=species0) %>% 
+  separate(species0, sep="_", into=c("Gen", "Spe")) %>% 
+  mutate(Gen=substr(Gen, 1, 3)) %>% 
+  mutate(Spe=substr(Spe, 1, 3)) %>% 
+  mutate(labels=paste(Gen, Spe, sep="_"))
+
+
+PCA1_2.sp <- baseplot %+% tmp + 
+  geom_text(aes(x=PC1, y=PC2, label=labels), size=2, alpha=0.8) + 
+  geom_segment(data=myvectors, 
+               aes(x=0, xend=PC1, y=0, yend=PC2, col=mycol), 
+               arrow = arrow(length = unit(0.08, "inches")), alpha=0.8) +
+  geom_path(data=dat,aes(x,y), col=gray(0.8), lwd=0.5) + #add correlation circle
+  geom_label(data=myvectors %>% 
+               mutate_if(~is.numeric(.), ~(.)*1.2),
+             aes(x=PC1, y=PC2, label=mylab, col=mycol, fontface=fontface0), size=2, 
+             position = position_dodge(1)) +#, segment.alpha=0.5, segment.colour=gray(0.8)) +
+  xlab(paste("PC1 (", varexpl[1], "%)", sep="")) + 
+  ylab(paste("PC2 (", varexpl[2], "%)", sep=""))
+
+PCA3_4.sp <- baseplot %+% tmp + 
+  geom_text(aes(x=PC3, y=PC4, label=labels), size=2, alpha=0.8) + 
+  geom_segment(data=myvectors, 
+               aes(x=0, xend=PC3, y=0, yend=PC4, col=mycol), 
+               arrow = arrow(length = unit(0.08, "inches")), alpha=0.8) +
+  geom_path(data=dat,aes(x,y), col=gray(0.8), lwd=0.5) + #add correlation circle
+  geom_label_repel(data=myvectors, 
+                   aes(x=PC3, y=PC4, label=mylab, col=mycol, fontface=fontface0), size=2, 
+                   position = position_dodge(1), segment.alpha=0.8, segment.colour=gray(0.7), segment.size = 0.5) +
+  xlab(paste("PC3 (", varexpl[3], "%)", sep="")) + 
+  ylab(paste("PC4 (", varexpl[4], "%)", sep="")) 
+
+
+ggsave("_pics/Fig6a_PCA_Traits_1-2_wSpecies.png", width=8, height=8, dpi=300, PCA1_2.sp)
+ggsave("_pics/Fig6b_PCA_Traits_3-4_wSpecies.png", width=8, height=8, dpi=300, PCA3_4.sp)
+
+
 
 
 
@@ -750,7 +786,7 @@ ggplot() +
   theme(panel.grid = element_blank()) + 
   title("PCA of CWMs")
 
-#### 4.3 PCoA of individual traits #####
+#### 4.3 PCA of individual traits #####
 # assign weights to traits [Growth forms and root structures are coded as binary, but should be downweighted]
 #myweights <- c(rep(1/9, 9), rep(1/17, 17), rep(1, 26))
 #traits.gowdis <- FD::gowdis(traits, w=myweights, ord="podani")
@@ -806,14 +842,15 @@ PCA.t2 <- baseplot +
                rownames_to_column("Trait") %>% 
                mutate(fontface0=ifelse(Trait %in% best.5traits, "bold", "plain")), 
              aes(x=Comp.3, y=Comp.4, label=Trait, col="Dark green", fontface=fontface0),  size=2, 
-             position = position_dodge(2),segment.alpha=0.5, segment.colour=gray(0.8)) 
+             position = position_dodge(2),segment.alpha=0.5, segment.colour=gray(0.8))  + 
+  xlab(paste("PC3 (", varexpl[3], "%)", sep="")) + 
+  ylab(paste("PC4 (", varexpl[4], "%)", sep=""))
 
 PC_traits <- cowplot::plot_grid(PCA.t1, PCA.t2, nrow=1)
 
 ggsave("_pics/FigS6_PCA_Traits_1-4_only11.png", width=10, height=5, dpi=300, PC_traits)
 
-
-## alternative version of figS6, showing the species
+##### 4.3b Alternative version of figS6, showing the species ####
 tmp <- as.data.frame(pca.scores[,1:4]*.2) %>% 
   mutate(species0=rownames(pca.scores)) %>% 
   mutate(species=species0) %>% 
@@ -846,8 +883,9 @@ PCA.t2.sp <- baseplot %+% tmp +
                      rownames_to_column("Trait") %>% 
                      mutate(fontface0=ifelse(Trait %in% best.5traits, "bold", "plain")), 
                    aes(x=Comp.3, y=Comp.4, label=Trait, col="Dark green", fontface=fontface0),  size=2, 
-                   position = position_dodge(2),segment.alpha=0.5, segment.colour=gray(0.8)) 
-#PC_traits_sp <- cowplot::plot_grid(PCA.t1.sp, PCA.t2.sp, nrow=1)
+                   position = position_dodge(2),segment.alpha=0.5, segment.colour=gray(0.8)) + 
+  xlab(paste("PC3 (", varexpl[3], "%)", sep="")) + 
+  ylab(paste("PC4 (", varexpl[4], "%)", sep=""))
 
 ggsave("_pics/FigS6_PCA_Traits_1-2_wSpecies.png", width=8, height=8, dpi=300, PCA.t1.sp)
 ggsave("_pics/FigS6_PCA_Traits_3-4_wSpecies.png", width=8, height=8, dpi=300, PCA.t2.sp)
-- 
GitLab