π€ The Problem
Phoenix, by default, uses the 75th percentile for whiskers in a box plot. Is it possible to change this so that the 10th and 90th percentiles are used? Yes, using an R script and the R Shell operational object.
β‘ The Solution
Assuming that you have R properly installed and set up in Phoenix (refer to the Configuring R to launch from Phoenix article):
In your project, insert the R Shell Operational Object.
Select R Shell in the Setup tab and copy/paste the following into the field:
βattach(data) #WNL_IN group y#
require(ggplot2)
require(dplyr)
library(ggplot2)
library(dplyr)
########################################################
# USER Input Label your plot
x.label<- "Dose Group"
y.label<-"AUCinf_D"
# NOTE: This script doesn't handle missing data
########################################################
#######################################################
# Factoring and preparing data for plotting
#data$group <- as.factor(data$group)
data$y <- as.numeric(data$y)
##############################################################################################
# Potential USER action
# Ensure groups are ordered in ascending order - Uncomment the statement below if applicable
#data$group <- factor(data$group, levels = sort(unique(data$group)), ordered = TRUE)
# Ensure groups are ordered in ascending order for this specific example where mg is attached to data.
data$group <- factor(data$group, levels = unique(data$group)[order(as.numeric(gsub("mg", "", unique(data$group))))], ordered = TRUE)
###############################################################################################
#Supply a folder path for the plot to be stored
output.path <- ""
# Function to calculate 10th and 90th percentiles
custom_stats <- function(x) {
x <- na.omit(x) # Remove missing values
data.frame(
ymin = quantile(x, 0.1,na.rm = TRUE),
lower = quantile(x, 0.25,na.rm = TRUE),
middle = median(x,na.rm = TRUE),
upper = quantile(x, 0.75,na.rm = TRUE),
ymax = quantile(x, 0.9,na.rm = TRUE)
)
}
# Create box plot regular
pp <- ggplot(data, aes(x = group, y = y)) +
geom_boxplot(fill = "lightblue", color = "black") +
labs(title = "Box Plot Standard", x = x.label, y = y.label) +
stat_summary(fun.y = mean, geom = "errorbar", aes(ymax = ..y.., ymin = ..y..),
width = .75, linetype = "dashed")
# Create box plot with custom whiskers
p3 <- ggplot(data, aes(x = group, y = y)) +
stat_summary(fun.data = custom_stats, geom = "boxplot", fill = "lightblue", color = "black", width = 0.5, na.rm = TRUE) +
stat_summary(fun.y = mean, geom = "errorbar", aes(ymax = ..y.., ymin = ..y..),
width = .5, linetype = "dashed", na.rm = TRUE)+
geom_jitter(width = 0.2, color = "darkred", alpha = 0.5, na.rm = TRUE) +
labs(title = "Box Plot Custom Whiskers", x =x.label, y = y.label) +
theme_gray() +
theme(
axis.line = element_line(color = "black"),
axis.ticks = element_line(color = "black"),
axis.text = element_text(size = 12),
axis.title = element_text(size = 14, face = "bold"),
plot.title = element_text(size = 16, face = "bold", hjust = 0.5)
)
# Create summary statistics for labeling
summary_data <- data %>%
group_by(group) %>%
summarise(
P10 = quantile(y, 0.1, na.rm = TRUE),
P90 = quantile(y, 0.9, na.rm = TRUE)
)
# Create box plot with custom whiskers and labels
p4 <- ggplot(data, aes(x = group, y = y)) +
stat_summary(fun.data = custom_stats, geom = "boxplot", fill = "lightblue", color = "black", width = 0.5, na.rm = TRUE) +
stat_summary(fun.y = mean, geom = "errorbar", aes(ymax = ..y.., ymin = ..y..),
width = .5, linetype = "dashed", na.rm = TRUE) +
geom_jitter(width = 0.2, color = "darkred", alpha = 0.5, na.rm = TRUE) +
geom_text(data = summary_data, aes(x = group, y = P10, label = "P10"), vjust = 1.5, color = "red", size = 4, na.rm = TRUE) +
geom_text(data = summary_data, aes(x = group, y = P90, label = "P90"), vjust = -0.5, color = "red", size = 4, na.rm = TRUE) +
labs(title = "Box Plot Custom Wiskers Labelled", x = x.label, y = y.label) +
theme_gray() +
theme(
axis.line = element_line(color = "black"),
axis.ticks = element_line(color = "black"),
axis.text = element_text(size = 12),
axis.title = element_text(size = 14, face = "bold"),
plot.title = element_text(size = 16, face = "bold", hjust = 0.5)
)
# Save the plots
ggsave(filename = paste0(output.path, "Boxplot standard.png"), plot = pp)
ggsave(filename = paste0(output.path, "Boxplot modified whiskers.png"), plot = p3)
ggsave(filename = paste0(output.path, "Boxplot modified whiskers labelled.png"), plot = p4)Click the Apply button.
Applying the script adds 'data' in the Setup tab for specifying the input dataset.Map your input dataset to 'data' and assign a column for the group and for the y axis.
Add a Box Plot object to your workflow.
Map the same input dataset to the Box Plot object.
Execute the workflow.
