I have done a PCA, but I tried to plot the points in bubbleplot format, with different sizes depending on a variable value:
Here is my code:
library("FactoMineR")
library("factoextra")
ulva<-c(1,1,0,2,3,2)
sediment<-c(1,1,1,3,3,4)
deep<-c(3,3,2,1,1,1)
site<-c("CH","CH","CH","AA","AA","AA")
ulva2<-c("1","1","0","2","3","2")
data<-data.frame(site,sediment,deep,ulva2)
#Then I perform the PCA
s.pca <- PCA(data[,2:3], graph = FALSE)
#And I plot it:
a<-fviz_pca_biplot(s.pca, repel = TRUE, # Variables color
col.ind = "#00FF00", # Individuals color
pointsize=ulva2,fill.ind = "#00FF00",
pointshape = 21,
)
a
The problem is that I cannot change the "sizepoint" title, I would like to write "Ulva" instead of "sizepoint". Furthermore, I would like to write the site (CH or AA) in the point and not the number of the sample.
I would appreciate any help, please.
Thank you in advance.
CodePudding user response:
Since factoextra
as a plotting library wraps ggplot2
, we can use standard additional plot components to add the labels you want and change the legend.
First, make sure when plotting that you set label = "none"
in fviz_pca_biplot()
to ensure that the sample number isn't plotted next to the points.
a<-fviz_pca_biplot(s.pca, repel = TRUE, # Variables color
col.ind = "#00FF00", # Individuals color
pointsize=ulva2,fill.ind = "#00FF00",
pointshape = 21,
label = "none"
)
Now, we want to get a data frame plotting the labels, so let's pull out the new coordinate system and attach the old labels to it.
# get label per point
df_label <- as.data.frame(s.pca$ind$coord)
names(df_label) <- c("x", "y")
df_label$label <- site
df_label
#> x y label
#> 1 -1.5990451 0.2392449 CH
#> 2 -1.5990451 0.2392449 CH
#> 3 -0.8112065 -0.5485937 CH
#> 4 1.1421751 -0.1708892 AA
#> 5 1.1421751 -0.1708892 AA
#> 6 1.7249466 0.4118823 AA
Now with this, we can use geom_text()
to plot those points, at the same time using labs()
to change the legend to say "Ulva"
.
a
geom_text(
data = df_label,
aes(label = label),
check_overlap = TRUE
)
labs(
size = "Ulva"
)
If overlapping points might have multiple sites, so that you would want to show them individually, use ggrepel::geom_text_repel()
instead of ggplot2::geom_text()
.