Hello, I was trying to adjust the position of legend, actually I want legend to be on the second plot (right side). Can someone please help me with this?
Code is below:
`
df <- read.csv("D:/Chapter6_Files_Figures/mfa_file.csv", stringsAsFactors = F)
colnames(df)[1] = "sample_name"
colnames(df)[23] = "O Potato ID"
colnames(df)[24] = "Raw Potato"
colnames(df)[25] = "Starchy"
colnames(df)[26] = "Cardboard"
colnames(df)[27] = "Umami"
colnames(df)[28] = "Beany"
colnames(df)[29] = "Cooked"
colnames(df)[30] = "Earthy"
colnames(df)[31] = "Musty-earthy"
colnames(df)[32] = "Egg"
colnames(df)[33] = "Sweet"
colnames(df)[34] = "Salt"
colnames(df)[35] = "Metallic"
colnames(df)[36] = "Bitter"
colnames(df)[37] = "Astringent"
colnames(df)[38] = "Nutty"
colnames(df)[39] = "O Sweet Impr"
colnames(df)[40] = "Vegetable complx"
colnames(df)[41] = "Raw potato peel"
colnames(df)[42] = "Sweet potato"
colnames(df)[43] = "Cauliflower"
df1 <- subset(df, select = c(1,23:31,34:41,43))
df2 <- df1[,-1]
#not scaled to unit variance (covariance), but data is centered automatically
res.pca <- prcomp(df2, scale = F)
#scaled to unit variance (correlation)
res.pca2 <- prcomp (df2, scale = T)
#Correlation matrix check
res<- cor(df2, method = "pearson", use = "complete.obs")
res
#> O Potato ID Raw Potato Starchy Cardboard Umami
#> O Potato ID 1.00000000 -0.06188877 0.50394290 0.031229164 0.63407017
#> Raw Potato -0.06188877 1.00000000 -0.32133115 0.232187255 -0.29785653
#> Starchy 0.50394290 -0.32133115 1.00000000 0.014455033 0.43770512
#> Cardboard 0.03122916 0.23218725 0.01445503 1.000000000 0.17085253
#> Umami 0.63407017 -0.29785653 0.43770512 0.170852532 1.00000000
#> Beany -0.03037446 0.06246432 -0.14099585 0.254581303 0.26529931
#> Cooked 0.53128565 -0.59414455 0.55906263 -0.218353139 0.82508224
#> Earthy 0.10320696 -0.38491450 0.16630916 0.224807884 0.34704330
#> Musty-earthy 0.19107856 0.69521631 0.08844447 -0.287450699 -0.20907544
#> Salt -0.16687857 -0.01492094 -0.27068645 -0.222978063 -0.09958524
#> Metallic -0.27589659 0.44998671 -0.56101762 0.411980596 -0.14244891
#> Bitter -0.63033109 0.22172875 -0.73861639 0.266470845 -0.37255225
#> Astringent -0.15070666 0.17942999 -0.63956653 -0.022160296 -0.14845665
#> Nutty -0.38606803 0.18787879 -0.52136511 -0.008233591 -0.11399447
#> O Sweet Impr -0.75140857 -0.27865222 -0.37061108 -0.240902370 -0.56869262
#> Vegetable complx 0.09725378 -0.20000000 -0.05320052 0.024700772 0.16547585
#> Raw potato peel -0.03516632 -0.29308090 0.02895668 0.036196622 0.35334119
#> Cauliflower -0.16652822 0.08956677 -0.33607258 0.164626238 0.43155666
#> Beany Cooked Earthy Musty-earthy Salt
#> O Potato ID -0.03037446 0.53128565 0.103206962 0.19107856 -0.16687857
#> Raw Potato 0.06246432 -0.59414455 -0.384914499 0.69521631 -0.01492094
#> Starchy -0.14099585 0.55906263 0.166309156 0.08844447 -0.27068645
#> Cardboard 0.25458130 -0.21835314 0.224807884 -0.28745070 -0.22297806
#> Umami 0.26529931 0.82508224 0.347043299 -0.20907544 -0.09958524
#> Beany 1.00000000 0.06647436 0.485523073 -0.31153590 0.30512773
#> Cooked 0.06647436 1.00000000 0.159000838 -0.21916636 -0.18525296
#> Earthy 0.48552307 0.15900084 1.000000000 -0.54422275 0.57567058
#> Musty-earthy -0.31153590 -0.21916636 -0.544222749 1.00000000 0.02480567
#> Salt 0.30512773 -0.18525296 0.575670579 0.02480567 1.00000000
#> Metallic 0.38408604 -0.39836816 -0.009677319 0.05691998 0.40139392
#> Bitter 0.08370961 -0.59293297 0.236221743 -0.17722030 0.55988337
#> Astringent 0.12008542 -0.36453882 0.403418256 0.00000000 0.73624773
#> Nutty 0.28257670 -0.07524627 -0.029977765 -0.19143638 0.17407766
#> O Sweet Impr -0.18649095 -0.23902996 -0.075180336 -0.29479673 0.24253563
#> Vegetable complx -0.13385212 0.04514776 0.413693153 -0.21158757 0.07460471
#> Raw potato peel -0.19614750 0.15689308 0.425489084 -0.31006138 0.10932608
#> Cauliflower 0.58180468 0.12821032 0.192847013 -0.30656379 0.10809284
#> Metallic Bitter Astringent Nutty O Sweet Impr
#> O Potato ID -0.275896593 -0.63033109 -0.1507067 -0.386068031 -0.75140857
#> Raw Potato 0.449986709 0.22172875 0.1794300 0.187878788 -0.27865222
#> Starchy -0.561017624 -0.73861639 -0.6395665 -0.521365110 -0.37061108
#> Cardboard 0.411980596 0.26647085 -0.0221603 -0.008233591 -0.24090237
#> Umami -0.142448914 -0.37255225 -0.1484567 -0.113994473 -0.56869262
#> Beany 0.384086045 0.08370961 0.1200854 0.282576699 -0.18649095
#> Cooked -0.398368161 -0.59293297 -0.3645388 -0.075246270 -0.23902996
#> Earthy -0.009677319 0.23622174 0.4034183 -0.029977765 -0.07518034
#> Musty-earthy 0.056919976 -0.17722030 0.0000000 -0.191436376 -0.29479673
#> Salt 0.401393919 0.55988337 0.7362477 0.174077656 0.24253563
#> Metallic 1.000000000 0.64236405 0.4388104 0.423900523 0.13629326
#> Bitter 0.642364055 1.00000000 0.7650921 0.397974671 0.47527082
#> Astringent 0.438810389 0.76509206 1.0000000 0.190304537 0.11363310
#> Nutty 0.423900523 0.39797467 0.1903045 1.000000000 0.54886043
#> O Sweet Impr 0.136293255 0.47527082 0.1136331 0.548860430 1.00000000
#> Vegetable complx -0.489115988 0.17056057 0.4077954 -0.212121212 -0.12666010
#> Raw potato peel -0.225265157 0.24994023 0.3244034 -0.310843380 -0.18560828
#> Cauliflower 0.396853838 0.24712083 0.1181689 0.324899078 -0.18351456
#> Vegetable complx Raw potato peel Cauliflower
#> O Potato ID 0.09725378 -0.03516632 -0.16652822
#> Raw Potato -0.20000000 -0.29308090 0.08956677
#> Starchy -0.05320052 0.02895668 -0.33607258
#> Cardboard 0.02470077 0.03619662 0.16462624
#> Umami 0.16547585 0.35334119 0.43155666
#> Beany -0.13385212 -0.19614750 0.58180468
#> Cooked 0.04514776 0.15689308 0.12821032
#> Earthy 0.41369315 0.42548908 0.19284701
#> Musty-earthy -0.21158757 -0.31006138 -0.30656379
#> Salt 0.07460471 0.10932608 0.10809284
#> Metallic -0.48911599 -0.22526516 0.39685384
#> Bitter 0.17056057 0.24994023 0.24712083
#> Astringent 0.40779544 0.32440343 0.11816886
#> Nutty -0.21212121 -0.31084338 0.32489908
#> O Sweet Impr -0.12666010 -0.18560828 -0.18351456
#> Vegetable complx 1.00000000 0.55190559 -0.13171584
#> Raw potato peel 0.55190559 1.00000000 0.33640104
#> Cauliflower -0.13171584 0.33640104 1.00000000
#p-value check in correlation matrix
res2 <- cor(as.matrix(df2))
res2
#> O Potato ID Raw Potato Starchy Cardboard Umami
#> O Potato ID 1.00000000 -0.06188877 0.50394290 0.031229164 0.63407017
#> Raw Potato -0.06188877 1.00000000 -0.32133115 0.232187255 -0.29785653
#> Starchy 0.50394290 -0.32133115 1.00000000 0.014455033 0.43770512
#> Cardboard 0.03122916 0.23218725 0.01445503 1.000000000 0.17085253
#> Umami 0.63407017 -0.29785653 0.43770512 0.170852532 1.00000000
#> Beany -0.03037446 0.06246432 -0.14099585 0.254581303 0.26529931
#> Cooked 0.53128565 -0.59414455 0.55906263 -0.218353139 0.82508224
#> Earthy 0.10320696 -0.38491450 0.16630916 0.224807884 0.34704330
#> Musty-earthy 0.19107856 0.69521631 0.08844447 -0.287450699 -0.20907544
#> Salt -0.16687857 -0.01492094 -0.27068645 -0.222978063 -0.09958524
#> Metallic -0.27589659 0.44998671 -0.56101762 0.411980596 -0.14244891
#> Bitter -0.63033109 0.22172875 -0.73861639 0.266470845 -0.37255225
#> Astringent -0.15070666 0.17942999 -0.63956653 -0.022160296 -0.14845665
#> Nutty -0.38606803 0.18787879 -0.52136511 -0.008233591 -0.11399447
#> O Sweet Impr -0.75140857 -0.27865222 -0.37061108 -0.240902370 -0.56869262
#> Vegetable complx 0.09725378 -0.20000000 -0.05320052 0.024700772 0.16547585
#> Raw potato peel -0.03516632 -0.29308090 0.02895668 0.036196622 0.35334119
#> Cauliflower -0.16652822 0.08956677 -0.33607258 0.164626238 0.43155666
#> Beany Cooked Earthy Musty-earthy Salt
#> O Potato ID -0.03037446 0.53128565 0.103206962 0.19107856 -0.16687857
#> Raw Potato 0.06246432 -0.59414455 -0.384914499 0.69521631 -0.01492094
#> Starchy -0.14099585 0.55906263 0.166309156 0.08844447 -0.27068645
#> Cardboard 0.25458130 -0.21835314 0.224807884 -0.28745070 -0.22297806
#> Umami 0.26529931 0.82508224 0.347043299 -0.20907544 -0.09958524
#> Beany 1.00000000 0.06647436 0.485523073 -0.31153590 0.30512773
#> Cooked 0.06647436 1.00000000 0.159000838 -0.21916636 -0.18525296
#> Earthy 0.48552307 0.15900084 1.000000000 -0.54422275 0.57567058
#> Musty-earthy -0.31153590 -0.21916636 -0.544222749 1.00000000 0.02480567
#> Salt 0.30512773 -0.18525296 0.575670579 0.02480567 1.00000000
#> Metallic 0.38408604 -0.39836816 -0.009677319 0.05691998 0.40139392
#> Bitter 0.08370961 -0.59293297 0.236221743 -0.17722030 0.55988337
#> Astringent 0.12008542 -0.36453882 0.403418256 0.00000000 0.73624773
#> Nutty 0.28257670 -0.07524627 -0.029977765 -0.19143638 0.17407766
#> O Sweet Impr -0.18649095 -0.23902996 -0.075180336 -0.29479673 0.24253563
#> Vegetable complx -0.13385212 0.04514776 0.413693153 -0.21158757 0.07460471
#> Raw potato peel -0.19614750 0.15689308 0.425489084 -0.31006138 0.10932608
#> Cauliflower 0.58180468 0.12821032 0.192847013 -0.30656379 0.10809284
#> Metallic Bitter Astringent Nutty O Sweet Impr
#> O Potato ID -0.275896593 -0.63033109 -0.1507067 -0.386068031 -0.75140857
#> Raw Potato 0.449986709 0.22172875 0.1794300 0.187878788 -0.27865222
#> Starchy -0.561017624 -0.73861639 -0.6395665 -0.521365110 -0.37061108
#> Cardboard 0.411980596 0.26647085 -0.0221603 -0.008233591 -0.24090237
#> Umami -0.142448914 -0.37255225 -0.1484567 -0.113994473 -0.56869262
#> Beany 0.384086045 0.08370961 0.1200854 0.282576699 -0.18649095
#> Cooked -0.398368161 -0.59293297 -0.3645388 -0.075246270 -0.23902996
#> Earthy -0.009677319 0.23622174 0.4034183 -0.029977765 -0.07518034
#> Musty-earthy 0.056919976 -0.17722030 0.0000000 -0.191436376 -0.29479673
#> Salt 0.401393919 0.55988337 0.7362477 0.174077656 0.24253563
#> Metallic 1.000000000 0.64236405 0.4388104 0.423900523 0.13629326
#> Bitter 0.642364055 1.00000000 0.7650921 0.397974671 0.47527082
#> Astringent 0.438810389 0.76509206 1.0000000 0.190304537 0.11363310
#> Nutty 0.423900523 0.39797467 0.1903045 1.000000000 0.54886043
#> O Sweet Impr 0.136293255 0.47527082 0.1136331 0.548860430 1.00000000
#> Vegetable complx -0.489115988 0.17056057 0.4077954 -0.212121212 -0.12666010
#> Raw potato peel -0.225265157 0.24994023 0.3244034 -0.310843380 -0.18560828
#> Cauliflower 0.396853838 0.24712083 0.1181689 0.324899078 -0.18351456
#> Vegetable complx Raw potato peel Cauliflower
#> O Potato ID 0.09725378 -0.03516632 -0.16652822
#> Raw Potato -0.20000000 -0.29308090 0.08956677
#> Starchy -0.05320052 0.02895668 -0.33607258
#> Cardboard 0.02470077 0.03619662 0.16462624
#> Umami 0.16547585 0.35334119 0.43155666
#> Beany -0.13385212 -0.19614750 0.58180468
#> Cooked 0.04514776 0.15689308 0.12821032
#> Earthy 0.41369315 0.42548908 0.19284701
#> Musty-earthy -0.21158757 -0.31006138 -0.30656379
#> Salt 0.07460471 0.10932608 0.10809284
#> Metallic -0.48911599 -0.22526516 0.39685384
#> Bitter 0.17056057 0.24994023 0.24712083
#> Astringent 0.40779544 0.32440343 0.11816886
#> Nutty -0.21212121 -0.31084338 0.32489908
#> O Sweet Impr -0.12666010 -0.18560828 -0.18351456
#> Vegetable complx 1.00000000 0.55190559 -0.13171584
#> Raw potato peel 0.55190559 1.00000000 0.33640104
#> Cauliflower -0.13171584 0.33640104 1.00000000
#as a table
corrplot(res, type = "upper", order = "hclust", tl.col = "black", tl.srt = 45, sig.level = 0.1)
#> Error in corrplot(res, type = "upper", order = "hclust", tl.col = "black", : could not find function "corrplot"
# Insignificant correlation are crossed
tiff(file = "corr_plot.tiff", width = 2200, height = 2200, pointsize = 10, units = "px", res = 400)
corrplot(res2$r, type="upper", tl.col = "black", order="original", p.mat = res2$P, sig.level = 0.1, insig = "label_sig")
#> Error in corrplot(res2$r, type = "upper", tl.col = "black", order = "original", : could not find function "corrplot"
dev.off()
#> png
#> 2
# Insignificant correlations are leaved blank
corrplot(res2$r, type="upper", order="FPC", p.mat = res2$P, sig.level = 0.01, insig = "blank")
#> Error in corrplot(res2$r, type = "upper", order = "FPC", p.mat = res2$P, : could not find function "corrplot"
#eigenvalue
eig.val <- get_eigenvalue(res.pca2)
#> Error in get_eigenvalue(res.pca2): could not find function "get_eigenvalue"
eig.val
#> Error in eval(expr, envir, enclos): object 'eig.val' not found
#Compute coordinates (loadings * the component standard deviation)
var_coord_func <- function(loadings, comp.sdev){
loadings*comp.sdev
}
loadings <- res.pca2$rotation
sdev <- res.pca2$sdev
var.coord <- t(apply(loadings, 1, var_coord_func, sdev))
head(var.coord[, 1:4])
#> PC1 PC2 PC3 PC4
#> O Potato ID 0.7070199 -0.125067956 0.308188060 -0.35274371
#> Raw Potato -0.3892111 0.461744484 0.515402596 -0.50361256
#> Starchy 0.8404237 0.004810249 -0.002590228 0.02857434
#> Cardboard -0.1228686 -0.225255288 0.448447069 -0.10501836
#> Umami 0.5809619 -0.611637866 0.387202644 0.01198671
#> Beany -0.2114108 -0.475879502 0.595929333 0.22495390
View(loadings) #View loadings
View(sdev) #view std deviation
View(var.coord) #View coordinates
#PCA
fviz_eig(res.pca2)
#> Error in fviz_eig(res.pca2): could not find function "fviz_eig"
rownames(res.pca2$x) <- df1$sample_name
tiff(file = "Flavor_pc2_corr.tiff", width = 4000, height = 2000, pointsize = 10,
units = "px", res = 400)
p1 <- fviz_pca_biplot(res.pca2, labelsize = 4, axes = c(1,2),
select.var = list(cos2 = 0.2),
col.var = "#879998",
col.ind = "cos2", gradient.cols = c("#FFCCFF", "#CC0066", "#000000"),
repel = TRUE,
title = "") -> fpi
#> Error in fviz_pca_biplot(res.pca2, labelsize = 4, axes = c(1, 2), select.var = list(cos2 = 0.2), : could not find function "fviz_pca_biplot"
fpi + theme(legend.position = c(0.9, 0.19))
#> Error in eval(expr, envir, enclos): object 'fpi' not found
p2 <- fviz_pca_biplot(res.pca2, labelsize = 4, axes = c(1,3),
select.var = list(cos2 = 0.2),
col.var = "#879998",
col.ind = "cos2", gradient.cols = c("#FFCCFF", "#CC0066", "#000000"),
repel = TRUE,
title = "") -> fpi
#> Error in fviz_pca_biplot(res.pca2, labelsize = 4, axes = c(1, 3), select.var = list(cos2 = 0.2), : could not find function "fviz_pca_biplot"
fpi + theme(legend.position = c(0.9, 0.19))
#> Error in eval(expr, envir, enclos): object 'fpi' not found
ggpubr::ggarrange(p1,p2, ncol = 2, nrow = 1, common.legend = TRUE, legend = "bottom")
#> Error in eval(expr, envir, enclos): object 'p1' not found
Created on 2023-12-28 with reprex v2.0.2
`