xia-lab / MetaboAnalystR

R package for MetaboAnalyst
Other
318 stars 157 forks source link

How to plot figures? #277

Open RemyMG opened 1 year ago

RemyMG commented 1 year ago

Hello,

I'm running MetaboAnalystR 4.0.0 on R 4.3.1 in RStudio 2023.06.0 Build 421 and I don't know how to plot any figures. The code lines mSet <- PlotNormSummary(mSet, "norm_0_", "png", 72, width=NA) or mSet <- PlotFC(mSet, "fc_0_", "png", 72, width=NA) don't display any figure and this single code line PlotNormSummary(mSet, "norm_0_", "png", 72, width=NA) displays the whole mSet object in the console. So how can I get the plots in the RStudio "Plot" pane? Is it a bug from the MetaboAnalystR functions or is there something I'm not doing correctly?

I really thank you for your help.

Here are the codes lines from my console, with data from the tutorial:

`library(googledrive); temp <- tempfile(fileext = ".csv")

Download data

dl1 <- drive_download(as_id("1wEh2P81J_xFWJs5y4mq98-FsjxJ5wmBO"), path = temp, overwrite = TRUE) File downloaded: metaboanalyst_input.csv Saved locally as: /tmp/RtmpIBzO2x/file41dc1f662ed1.csv

Load data

mSet<-Read.TextData(mSet, "file41dc1f662ed1.csv", "colu", "disc") mSet<-SanityCheckData(mSet) [1] "Successfully passed sanity check!"
[2] "Samples are not paired."
[3] "3 groups were detected in samples."
[4] "Only English letters, numbers, underscore, hyphen and forward slash (/) are allowed."
[5] "<font color=\"orange\">Other special characters or punctuations (if any) will be stripped off."
[6] "All data values are numeric."
[7] "A total of 744120 (14.4%) missing values were detected."
[8] "By default, missing values will be replaced by 1/5 of min positive values of their corresponding variables" [9] "Click the Proceed button if you accept the default practice;"
[10] "Or click the Missing Values button to use other methods."
mSet<-ReplaceMin(mSet); mSet<-PreparePrenormData(mSet) #Prepare data for normalization

Normalize

mSet<-Normalization(mSet, rowNorm="GroupPQN", transNorm="NULL", scaleNorm="NULL", ref="QC", ratio=FALSE, ratioNum=20) [1] 604 8540 mSet<-PlotNormSummary(mSet, "norm0", "png", 72, width=NA)

Fold change analysis

mSet <- FC.Anal(mSet, 2.0, 0) mSet <- PlotFC(mSet, "fc0", "png", 72, width=NA)`

jessieolough commented 1 year ago

Hi! Yes, it appears that MetaboAnalystR functions only provide images as outputs. This stackoverflow thread describes how you can plot images in the plot panel in RStudio.

One thing I have been doing is incorporating the images into a pdf output using RMarkdown. Below is a shortened example of how I have done this in a .Rmd file in RStudio, but here is also a link to a definitive guide to making pdf documents using RStudio.

---
title: "Metabolomics Report"
author: "jessieolough"
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)```

```{r Code-from-MetaboAnalyst, message=FALSE, warning=FALSE, include=FALSE}
library(MetaboAnalystR)
# Construct mSetObj
mSet<-InitDataObjects("pktable", "stat", FALSE)
# Read raw csv file
mSet<-Read.TextData(mSet, "annotations.csv", "colu", "disc");
# Check that the data is in the correct format for further analysis
mSet<-SanityCheckData(mSet)
# Replace zero/missing values with half of the smallest positive values in the original dataset
mSet<-ReplaceMin(mSet);
mSet<-SanityCheckData(mSet)
# Filter dataset (filter out 5% based on Interquantile range, IQR)
mSet<-FilterVariable(mSet, "none", -1, "F", 25, F)
# Normalise data
mSet<-PreparePrenormData(mSet)
mSet<-Normalization(mSet, "NULL", "LogNorm", "ParetoNorm", ratio=FALSE, ratioNum=20)
# Make a two-plot summary plot (exported as .png into directory)
mSet<-PlotNormSummary(mSet, "norm_0_", "png", 72, width=NA)```

```{r Insert-MetaboAnalyst-boxplots, echo=FALSE, fig.align="center", message=FALSE, warning=FALSE, out.width = "100%"}
library(rmarkdown)
library(knitr)
knitr::include_graphics("norm_0_dpi72.png")```