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/09/18 17:58] – chkuo | 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:// | + | |
| ==== One batch with replicates ==== | ==== One batch with replicates ==== | ||
| + | |||
| + | === Single batch boxplot tutorials === | ||
| + | * {{single-batch_boxplot_v01.pdf}} | ||
| + | |||
| === Sample input === | === Sample input === | ||
| * {{sample1.xlsx}} | * {{sample1.xlsx}} | ||
| === R script === | === R script === | ||
| - | * Please download the R script using the link below. | ||
| - | * [[ https:// | ||
| <file r single-batch_boxplot.R> | <file r single-batch_boxplot.R> | ||
| - | rm(list=ls()) | + | # Aim: This R script is about making single batch boxplot. |
| + | |||
| + | # Data Description: | ||
| + | # - sample1.xlsx | ||
| + | # - Sheet1: Contains raw data across two columns: `Strain` and `Value`. Each strain includes 20 replicates. | ||
| + | # - Sheet2: Contains the statistical test result | ||
| + | # | ||
| + | # Output | ||
| + | # - Version 1: Displays all data points using a single color. | ||
| + | # - Version 2: Colors the data points by strain 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) { | ||
| + | if (!requireNamespace(pkg, | ||
| + | install.packages(pkg, | ||
| + | } | ||
| + | } | ||
| + | |||
| + | # | ||
| + | |||
| + | ### Step 1. Configuration | ||
| + | |||
| + | ## Set up a dedicated working directory to store your raw files and output plots. | ||
| # working directory | # working directory | ||
| - | setwd("/home/hychang/R_pwd/2026-09-14_R-plots/ | + | working_path = "/scratch/hychang/R_wd/Rana001/ |
| - | # load packages | + | # input data located directory |
| - | library(readxl) | + | input_path = "/ |
| - | library(ggplot2) | + | |
| - | ### Step 1. import data, choose one of them to import file | + | # output folder for storing plots |
| - | # (1) fill in full path of your file and the sheet name | + | output_path |
| - | df1= read_xlsx("/home/hychang/R_pwd/2026-09-14_R-plots/Box_plots/ | + | |
| - | # (2) If you don't know the full path, use file.choose to select your file, | + | ## Set Input and output |
| - | # remember fill in the sheet name | + | |
| - | df1= read_xlsx(file.choose(), | + | |
| + | # File name of the raw data table, including the file extension | ||
| + | input_file_raw = " | ||
| - | ### Step 2. import | + | # Worksheet name of the raw data table |
| + | input_sheet_raw = " | ||
| - | df2= read_xlsx("/ | + | # (Optional) File name of the statistical test result table, including the file extension |
| + | input_file_stat | ||
| + | # (Optional) Worksheet name of the statistical test result table | ||
| + | input_sheet_stat = " | ||
| - | ### Step 3. Inspect | + | # Column names in the input data |
| + | x_column = " | ||
| + | y_column = " | ||
| - | # dimension | + | # Column name of statistical significance group letters, skip if the statistical test not available |
| - | df1; dim(df1) | + | stat_column = " |
| - | df2; dim(df2) | + | |
| - | # counts | + | # Output file names of the figures, including the file extension |
| - | table(df1$"Strain") | + | output_file_1 = "single-batch_v1.pdf" |
| - | table(df1$"Strain", useNA = "always") | + | output_file_2 = "single-batch_v2.pdf" |
| + | output_file_3 = " | ||
| + | output_file_4 | ||
| - | # range of value | + | # Set up a dedicated working directory to store your raw files and output plots. |
| - | range(df1$" | + | dir.create(working_path, |
| + | dir.create(input_path, | ||
| + | dir.create(output_path, | ||
| + | ## Setting figure parameters | ||
| - | ### Step 4. setting sample order | + | # the names of x and y axis |
| + | x_lab = " | ||
| + | y_lab = " | ||
| - | # set your ordering in levels | + | # y axis range and breaks |
| - | df1$" | + | y_range |
| - | | + | y_breaks |
| + | # 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 | ||
| + | |||
| + | # use a single color for all data points | ||
| + | color_single = c("# | ||
| + | |||
| + | # use multiple colors for the data points; the number of colors should match the number of samples | ||
| + | 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 and if there is NA value. | ||
| + | table(df1[[x_column]], | ||
| + | |||
| + | # range of value | ||
| + | range(df1[[y_column]]) | ||
| + | # | ||
| - | ### Step 5. plot | + | #### Step 3. Generate Plots |
| - | # version | + | # Version |
| # all data points using a single, uniform color. | # all data points using a single, uniform color. | ||
| - | p1= ggplot(data = df1, aes(x= | + | p1 = ggplot(data = df1, aes(x = .data[[x_column]], y = .data[[y_column]])) + |
| - | geom_boxplot(width = 0.4, outlier.shape = NA)+ | + | geom_boxplot(width = box_size, |
| - | geom_point(color= | + | outlier.shape = NA) + |
| - | | + | geom_point(color = color_single, |
| - | scale_y_continuous(limits = c(0, NA))+ | + | size = point_size, |
| - | labs(x= | + | |
| - | theme_classic()+ | + | scale_y_continuous(limits = y_range, |
| - | theme(axis.title = element_text(size = 16), | + | |
| - | axis.text = element_text(size = 14)) | + | 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 | # show plot | ||
| Line 81: | Line 181: | ||
| - | # version | + | # Version |
| - | # Colors | + | # colors |
| - | p2= ggplot(data = df1, aes(x= | + | p2 = ggplot(data = df1, aes(x = .data[[x_column]], y = .data[[y_column]])) + |
| - | geom_boxplot(width = 0.4, outlier.shape = NA)+ | + | geom_boxplot(width = box_size, outlier.shape = NA) + |
| - | geom_point(aes(color= | + | geom_point(aes(color = .data[[x_column]]), |
| - | | + | size = point_size, |
| - | geom_text(data = df2, aes(x= | + | |
| - | scale_color_manual(values = c("# | + | geom_text(data = df2, |
| - | scale_y_continuous(limits = c(0, NA))+ | + | |
| - | labs(x= | + | |
| - | theme_classic()+ | + | scale_color_manual(values = c(color_multiple)) + |
| - | theme(axis.title = element_text(size = 16), | + | scale_y_continuous(limits = y_range, |
| - | axis.text = element_text(size = 14)) | + | |
| + | 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 | # show plot | ||
| Line 99: | Line 204: | ||
| - | ### Step 6. Export | + | # |
| + | #### Step 4. Export | ||
| + | |||
| + | ## Export PDF | ||
| # version 1 PDF export | # version 1 PDF export | ||
| - | pdf(" | + | pdf(file.path(output_path, |
| p1 | p1 | ||
| dev.off() | dev.off() | ||
| # version 2 PDF export | # version 2 PDF export | ||
| - | pdf("/ | + | pdf(file.path(output_path, |
| p2 | p2 | ||
| dev.off() | dev.off() | ||
| - | # change the width and height to 6 and 4 in inch | ||
| - | pdf(" | ||
| - | p1 | ||
| - | dev.off() | ||
| - | pdf(" | ||
| - | p2 | ||
| - | dev.off() | ||
| + | ## Export PNG | ||
| - | # version | + | # Version |
| - | png(" | + | png(file.path(output_path, |
| p1 | p1 | ||
| dev.off() | dev.off() | ||
| - | # version | + | # Version |
| - | png(" | + | png(file.path(output_path, |
| p2 | p2 | ||
| dev.off() | dev.off() | ||
| - | # change width and height to 600 and 400 in pixels | ||
| - | png(" | ||
| - | p1 | ||
| - | dev.off() | ||
| - | png(" | + | ### END this script |
| - | p2 | + | |
| - | dev.off() | + | |
| </ | </ | ||
| Line 148: | Line 245: | ||
| * **Version 1**: Displays all data points using a single, uniform color. | * **Version 1**: Displays all data points using a single, uniform color. | ||
| * PNG: setting width 600 and height 400. | * PNG: setting width 600 and height 400. | ||
| - | * {{ simple_boxplot1_w600h400.png }} | + | * {{ single-batch_v1.png }} |
| - | * PDF: width 6 inch and height 4 inch. {{ simple_boxplot1_w6h4.pdf }} | + | * PDF: width 6 inch and height 4 inch. {{ single-batch_v1.pdf }} |
| * **Version 2**: Colors data points by Strain and adds statistical significance group labels at the top. | * **Version 2**: Colors data points by Strain and adds statistical significance group labels at the top. | ||
| * PNG: setting width 600 and height 400. | * PNG: setting width 600 and height 400. | ||
| - | * {{ simple_boxplot2_w600h400.png }} | + | * {{ single-batch_v2.png }} |
| - | * PDF: width 6 inch and height 4 inch. {{ simple_boxplot2_w6h4.pdf }} | + | * PDF: width 6 inch and height 4 inch. {{ single-batch_v2.pdf }} |
| \\ | \\ | ||
| ==== 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 168: | 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.1789725527.txt.gz · Last modified: by chkuo