How to extract graph object from plotVar()?

Dear community,

I want to compile the mixOmics graphical outputs via ggpubr::ggarrange() - to do this, I need to convert the graphical outputs into ggplot objects first. I managed it well with plotInd() by choosing style = "ggplot2" and extracting the plot via sPLS2.plot.ggplot <- sPLS2.plot$graph.

However, this does not work with plotVar() as this function returns a data frame. Is there a way to save the variable plot as a ggplot object?

Thank you in advance.


Edit: I also face the same difficulties with the plotLoadings() function.

Hi @l.bel

You can just save your object

X <- nutrimouse$lipid
Y <- nutrimouse$gene
nutri.res <- rcc(X, Y, ncomp = 3, lambda1 = 0.064, lambda2 = 0.008)

result.plotVar <- plotVar(nutri.res)

Then refer to ?plotVar to understand what are those outputs. Same for plotLoadings


Hi @kimanh.lecao ,

I apologise for my inconcrete question.
I want to save / convert the plot generated via plotVar() and plotLoadings() in a ggplot2-format, so I can then include this plot in my figure panel, generated via ggarrange().

This is the code I used:

sPLS2.plot.var <- plotVar(final.spls.bact, 
        cex = c(2,1.5), 
        var.names = c(FALSE, FALSE), 
        col = c("seagreen", "skyblue3"), 
        style = 'ggplot2')

When I look at the plotVar (or the plotLoadings) object, I get a data frame.

> class(sPLS2.plot.var)
[1] "data.frame"
> sPLS2.plot.var
                                                         x           y Block names pch cex      col font                 Overlap
Lachnospira                                   -0.657657899  0.12926272     X     1   1 2.0 seagreen    1 Correlation Circle Plot -0.422280165 -0.02878827     X     1   1 2.0 seagreen    1 Correlation Circle Plot
Victivallis                                   -0.146332173 -0.31979434     X     1   1 2.0 seagreen    1 Correlation Circle Plot
Lachnospiraceae.Family                        -0.431441516 -0.14506771     X     1   1 2.0 seagreen    1 Correlation Circle Plot                 -0.586063000  0.03523769     X     1   1 2.0 seagreen    1 Correlation Circle Plot

Is there a neat way to convert /plot this plotVar object into a ggplot2 object, so I can include the figure below (generated solely by running plotVar()) in my ggarrange() panel?


Hi @l.bel ,
a nice workaround is to export the data from the mixomics object using$graph at the end of the prompt and use the data to create your own ggplot graph.
Here is an example form the pca data but it works with any object.
Let me know if this works for you :slightly_smiling_face:

get the data

dat.pca.moxL_mrn ← plotIndiv(final.pca.moxL_mrn,
comp = c(1, 2), # Specify components to plot
ind.names = F, # Show row names of samples
group = san_moxL_mrn$pop_gen, # your grouping
title = ‘mRNA’, # doent actually matter
legend = TRUE,
legend.title = ‘Stage’)$graph # this is the $graph prompt that gives you the data

get the axis labels with the variance

labX.pca_mrn ← dat.pca.moxL_mrn$labels$x
labY.pca_mrn ← dat.pca.moxL_mrn$labels$y

plot using ggplot2

pl.pca.moxL_mrn ← dat.pca.moxL_mrn$data %>%
ggplot(aes(x = x, y = y, color = group)) +
geom_point(alpha = 0.8, size = 4) +
scale_x_reverse() +
scale_color_manual(values = colors_pop_gen) + # own color scheme
ggtitle(“Transcriptome”) +
xlab(labX.pca_mrn) +