tutorials:r_plots
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| tutorials:r_plots [2026/10/12 12:13] – hychang | tutorials:r_plots [2026/10/13 10:44] (current) – [Box plots] hychang | ||
|---|---|---|---|
| Line 4: | Line 4: | ||
| An user guide to walk through data visualization by box plot in R.\\ | An user guide to walk through data visualization by box plot in R.\\ | ||
| - | Please | + | Please |
| - | + | ||
| - | [[https:// | + | |
| - | + | ||
| Line 163: | Line 159: | ||
| # | # | ||
| - | #### Step 3. Generating | + | #### Step 3. Generate |
| # Version 1 | # Version 1 | ||
| Line 209: | Line 205: | ||
| # | # | ||
| - | #### Step 4. Exporting | + | #### Step 4. Export |
| ## Export PDF | ## Export PDF | ||
| Line 227: | Line 223: | ||
| ## Export PNG | ## Export PNG | ||
| - | # Version 1 PNG export | + | # Version 1 PNG export |
| png(file.path(output_path, | png(file.path(output_path, | ||
| p1 | p1 | ||
| dev.off() | dev.off() | ||
| - | # Version 2 PNG export | + | # Version 2 PNG export |
| png(file.path(output_path, | png(file.path(output_path, | ||
| p2 | p2 | ||
| Line 258: | Line 254: | ||
| ==== Multiple batches with replicates ==== | ==== Multiple batches with replicates ==== | ||
| + | |||
| + | === Multiple batches boxplot tutorials === | ||
| + | * {{multiple-batches_boxplot_v01.pdf}} | ||
| + | |||
| === Sample input === | === Sample input === | ||
| * {{sample2.xlsx}} | * {{sample2.xlsx}} | ||
| + | |||
| === R script === | === R script === | ||
| - | * Please download the R script | + | |
| - | | + | |
| + | <file r multiple-batches_boxplot.R> | ||
| + | |||
| + | # Aim: This R script | ||
| + | |||
| + | # Data Description: | ||
| + | # - sample1.xlsx | ||
| + | # - Sheet1: Contains raw data across three columns: `Strain`, `Batch`, `Value`. Each strain has three batches. | ||
| + | # - Sheet2: Contains | ||
| + | # | ||
| + | # Output (both pdf and png format): | ||
| + | # - Version 1: Displays data with batches shown in distinct colors. | ||
| + | # - Version 2: Displays batches in distinct colors and adds statistical significance group labels at the top. | ||
| + | # | ||
| + | |||
| + | ### Step 0, Check packages | ||
| + | |||
| + | # Required packages | ||
| + | packages <- c(" | ||
| + | |||
| + | # Check and install required packages | ||
| + | for (pkg in packages) { | ||
| + | | ||
| + | install.packages(pkg, | ||
| + | } | ||
| + | } | ||
| + | |||
| + | # | ||
| + | |||
| + | ### Step 1. Configuration | ||
| + | |||
| + | ## Set up a dedicated working directory to store your raw files and output plots. | ||
| + | |||
| + | # working directory | ||
| + | working_path = "/scratch/hychang/R_wd/Rana001/ | ||
| + | |||
| + | # input data located directory | ||
| + | input_path = "/ | ||
| + | |||
| + | # output folder for storing plots | ||
| + | output_path = "/ | ||
| + | |||
| + | ## Set Input and output | ||
| + | |||
| + | # File name of the raw data table, including the file extension | ||
| + | input_file_raw = " | ||
| + | |||
| + | # Worksheet name of the raw data table | ||
| + | input_sheet_raw = " | ||
| + | |||
| + | # File name of the statistical test result table, including the file extension | ||
| + | input_file_stat = " | ||
| + | |||
| + | # Worksheet name of the statistical test result table | ||
| + | input_sheet_stat = " | ||
| + | |||
| + | # Column names in the input data | ||
| + | x_column = " | ||
| + | y_column = " | ||
| + | batch_column = " | ||
| + | |||
| + | # Column name of statistical significance group letters, | ||
| + | # skip if the statistical test not available | ||
| + | stat_column = " | ||
| + | |||
| + | # Output file names of the figures, including the file extension | ||
| + | output_file_1 = " | ||
| + | output_file_2 = " | ||
| + | output_file_3 = " | ||
| + | output_file_4 = " | ||
| + | |||
| + | # Set up a dedicated working directory to store your raw files and output plots. | ||
| + | dir.create(working_path, | ||
| + | dir.create(input_path, | ||
| + | dir.create(output_path, | ||
| + | |||
| + | ## Setting figure parameters | ||
| + | |||
| + | # the names of x and y axis | ||
| + | x_lab = " | ||
| + | y_lab = " | ||
| + | |||
| + | # y axis range and breaks | ||
| + | y_range = c(0, 6) | ||
| + | y_breaks = c(0, 2, 4, 6) | ||
| + | |||
| + | # font size of x and y axis title (axis label) | ||
| + | axis_title_size = 16 | ||
| + | |||
| + | # font size of x and y axis text (tick label) | ||
| + | axis_text_size = 14 | ||
| + | |||
| + | # sample ordering | ||
| + | x_order = c(" | ||
| + | |||
| + | # size of data points | ||
| + | point_size= 2 | ||
| + | |||
| + | # box size | ||
| + | box_size= 0.4 | ||
| + | |||
| + | # space between batches (points) | ||
| + | space_batch = 0.3 | ||
| + | |||
| + | # use multiple colors for the data points; | ||
| + | # the number of colors should match the number of batches | ||
| + | color_multiple = c(c("# | ||
| + | |||
| + | ## Export setting | ||
| + | pdf_width = 6 | ||
| + | pdf_height = 4 | ||
| + | png_width = 600 | ||
| + | png_height = 400 | ||
| + | |||
| + | # | ||
| + | |||
| + | ### Step 2. Setup and Import data | ||
| + | |||
| + | # Set working directory | ||
| + | setwd(working_path) | ||
| + | |||
| + | # load packages | ||
| + | library(readxl) | ||
| + | library(ggplot2) | ||
| + | |||
| + | # Import raw data | ||
| + | df1= read_xlsx(file.path(input_path, | ||
| + | |||
| + | # (Optional) Import statistical test result. Skip this step if you don't have the results. | ||
| + | df2= read_xlsx(file.path(input_path, | ||
| + | |||
| + | # Sample ordering | ||
| + | df1[[x_column]] = factor(df1[[x_column]], | ||
| + | df2[[x_column]] = factor(df2[[x_column]], | ||
| + | |||
| + | # Display the data frame and view its dimensions (number of rows and columns). | ||
| + | df1; dim(df1) | ||
| + | df2; dim(df2) # skip this if df2 not available | ||
| + | |||
| + | # Show sample counts, including NA values | ||
| + | table(df1[[x_column]], | ||
| + | |||
| + | # Show the range of values | ||
| + | range(df1[[y_column]]) | ||
| + | |||
| + | # check batch counts, including NA values | ||
| + | table(df1[[batch_column]], | ||
| + | #--------------------------------------------------------------------------------------------------------------- | ||
| + | |||
| + | #### Step 3. Generate Plots | ||
| + | |||
| + | # Version 1 | ||
| + | # all data points using a single, uniform color. | ||
| + | p1= ggplot(data= df1, aes(x= .data[[x_column]], y= .data[[y_column]]))+ | ||
| + | geom_boxplot(width = box_size, | ||
| + | | ||
| + | geom_point(data= df1, | ||
| + | | ||
| + | shape = 21, | ||
| + | size = point_size, | ||
| + | | ||
| + | scale_fill_manual(values = color_multiple)+ | ||
| + | scale_y_continuous(limits = y_range, | ||
| + | | ||
| + | labs(x= x_lab, y= y_lab)+ | ||
| + | theme_classic()+ | ||
| + | theme(axis.title = element_text(size = axis_title_size), | ||
| + | axis.text = element_text(size = axis_text_size)) | ||
| + | |||
| + | # show plot | ||
| + | p1 | ||
| + | |||
| + | |||
| + | # Version 2 | ||
| + | # colors data points by Strain and adds statistical significance group labels at the top. | ||
| + | p2= ggplot(data = df1, aes(x= .data[[x_column]], | ||
| + | geom_boxplot(width = box_size, outlier.shape = NA)+ | ||
| + | geom_point(aes(fill= .data[[batch_column]]), | ||
| + | shape = 21, | ||
| + | size = point_size, | ||
| + | | ||
| + | geom_text(data = df2, | ||
| + | aes(x= .data[[x_column]], | ||
| + | vjust= " | ||
| + | scale_fill_manual(values = color_multiple)+ | ||
| + | scale_y_continuous(limits = y_range, | ||
| + | | ||
| + | labs(x= x_lab, y= y_lab)+ | ||
| + | theme_classic()+ | ||
| + | theme(axis.title = element_text(size = axis_title_size), | ||
| + | axis.text = element_text(size = axis_text_size)) | ||
| + | |||
| + | # show plot | ||
| + | p2 | ||
| + | |||
| + | |||
| + | # | ||
| + | #### Step 4. Export Plots | ||
| + | |||
| + | ## Export PDF | ||
| + | |||
| + | # version 1 PDF export | ||
| + | pdf(file.path(output_path, | ||
| + | p1 | ||
| + | dev.off() | ||
| + | |||
| + | # version 2 PDF export | ||
| + | pdf(file.path(output_path, | ||
| + | p2 | ||
| + | dev.off() | ||
| + | |||
| + | |||
| + | |||
| + | ## Export PNG | ||
| + | |||
| + | # Version 1 PNG export | ||
| + | png(file.path(output_path, | ||
| + | p1 | ||
| + | dev.off() | ||
| + | |||
| + | # Version 2 PNG export | ||
| + | png(file.path(output_path, | ||
| + | p2 | ||
| + | dev.off() | ||
| + | |||
| + | |||
| + | ### END this script | ||
| + | |||
| + | </ | ||
| === Sample output === | === Sample output === | ||
| Line 269: | Line 499: | ||
| * **Version 1**: Displays data with batches shown in distinct colors. | * **Version 1**: Displays data with batches shown in distinct colors. | ||
| * PNG: setting width 600 and height 400. | * PNG: setting width 600 and height 400. | ||
| - | * {{ mult_boxplot1_w600h400.png }} | + | * {{ mult-batches_v01.png }} |
| - | * PDF: width 6 inch and height 4 inch. {{ mult_boxplot1_w6h4.pdf }} | + | * PDF: width 6 inch and height 4 inch. {{ mult-batches_v01.pdf }} |
| * **Version 2**: Displays batches in distinct colors and adds statistical significance group labels at the top. | * **Version 2**: Displays batches in distinct colors and adds statistical significance group labels at the top. | ||
| * PNG: setting width 600 and height 400. | * PNG: setting width 600 and height 400. | ||
| - | * {{ mult_boxplot2_w600h400.png }} | + | * {{ mult-batches_v02.png }} |
| - | * PDF: width 6 inch and height 4 inch. {{ mult_boxplot2_w6h4.pdf }} | + | * PDF: width 6 inch and height 4 inch. {{ mult-batches_v02.pdf }} |
tutorials/r_plots.1791778403.txt.gz · Last modified: by hychang