Closed JauntyJJS closed 3 years ago
Can you provide session information and a reproducible example?
Sorry for the late reply. This is what I have
#Load the necessary libraries
suppressPackageStartupMessages(library("here"))
suppressPackageStartupMessages(library("usethis"))
suppressPackageStartupMessages(library("fs"))
suppressPackageStartupMessages(library("dplyr"))
suppressPackageStartupMessages(library("tidyr"))
suppressPackageStartupMessages(library("magrittr"))
suppressPackageStartupMessages(library("purrr"))
suppressPackageStartupMessages(library("tibble"))
suppressPackageStartupMessages(library("plotly"))
suppressPackageStartupMessages(library("broom"))
suppressPackageStartupMessages(library("trelliscopejs"))
# Functions Used ####
#' Function used to create a Dilution table by merging two tables
#'
#' @param Dilution_Annot Input Dilution Annotation. Must have column "Sample_Name", "Dilution_Batch", "Dilution_Percent"
#' @param Lipid_Data_Area Input Lipid area data frame with "Sample_Name" as the first column followed by Transition Name
#' @param pal Input palette for "Dilution_Batch". It is a named char vector where each value is a color and name is a dilution batch name
#' @return Output a lipid dilution table for each transition and dilution batch
#' @export
#' @importFrom magrittr %>%
#' @importFrom rlang .data
get_lipid_dil <- function(Dilution_Annot,Lipid_Data_Area,pal){
pal_df <- pal %>%
tibble::enframe() %>%
dplyr::rename(Dilution_Batch = .data$name) %>%
dplyr::mutate(Dilution_Batch = as.integer(.data$Dilution_Batch),
Dilution_Batch_Name = factor(.data$Dilution_Batch)) %>%
dplyr::rename(levelFill = .data$value) %>%
dplyr::select(.data$Dilution_Batch,.data$Dilution_Batch_Name,.data$levelFill)
Lipid_Dil <- dplyr::inner_join(Dilution_Annot,Lipid_Data_Area,by="Sample_Name") %>%
tidyr::pivot_longer(-c("Sample_Name","Dilution_Batch","Dilution_Percent"),
names_to = "Transition_Name", values_to = "Area") %>%
dplyr::inner_join(pal_df,by="Dilution_Batch")
return(Lipid_Dil)
}
#' Function used to add cognostics relevant to the dilution plot
#'
#' @param Dilution_Plot_Data Input Dilution Plot data
#' @return Output a data frame with added cognostics relevant to the dilution p
#' @export
#' @importFrom rlang .data
get_dil_cognostics <- function(Dilution_Plot_Data){
#Create cognostics
Dilution_Plot_Data <- dplyr::mutate(Dilution_Plot_Data,
Transition_Name = trelliscopejs::cog(.data$Transition_Name,
desc = "Transition_Name",
type = "factor",
default_label = TRUE),
Lipid_Name = trelliscopejs::cog(.data$Lipid_Name,
desc = "Lipid Name",
type = "factor",
default_label = FALSE),
Transition_Name_Class = trelliscopejs::cog(.data$Transition_Name_Class,
desc = "Lipid Class",
type = "factor",
default_label = FALSE),
Transition_Name_Species = trelliscopejs::cog(.data$Transition_Name_Species,
desc = "Lipid Species",
type = "factor",
default_label = FALSE),
Precursor_Ion = trelliscopejs::cog(.data$Precursor_Ion,
desc = "Precursor Ion",
type = "numeric",
default_label = FALSE),
Product_Ion = trelliscopejs::cog(.data$Product_Ion,
desc = "Product Ion",
type = "numeric",
default_label = FALSE),
isISTD = trelliscopejs::cog(.data$isISTD,
desc = "is an ISTD or not",
type = "factor",
default_label = FALSE),
isQualifier = trelliscopejs::cog(.data$isQualifier,
desc = "is a Qualifier or not",
type = "factor",
default_label = FALSE)
)
return(Dilution_Plot_Data)
}
#' Function used to create dilution plot
#'
#'
#' @param Dilution_Data Input Dilution Data with columns Dilution Percent,Area,levelFill and Dilution_Batch Name of one transition
#' @param pal Input palette for Dilution Batch. It is a named char vector where each value is a color and name is the Dilution Batch Name
#' @return Output Dilution Plot data of one transition
#' @export
#' @importFrom magrittr %>%
#' @importFrom rlang .data
dilution_plot <- function(Dilution_Data,pal){
Dilution_Data = tidyr::drop_na(Dilution_Data,.data$Area)
p <- plotly::plot_ly() %>%
plotly::add_trace(data = Dilution_Data,x = ~Dilution_Percent, y = ~Area,
type = 'scatter', mode = 'markers',
marker=list(size=10 , opacity=1, line = list(color = 'black',width = 1.5)),
#name = unique(data$Dilution_Batch_Name),
name = ~Dilution_Batch_Name,
color = ~Dilution_Batch_Name,
colors = pal,
hoverinfo = 'text',
text = ~Sample_Name,
hovertemplate = paste(
"<b>%{text}</b><br>",
"%{xaxis.title.text}: %{x}<br>",
"%{yaxis.title.text}: %{y:,0f}<br>",
"<extra></extra>"),
inherit = FALSE)
Dilution_Batch_1 <- Dilution_Data %>%
filter(.data$Dilution_Batch == 1)
Dilution_Batch_2 <- Dilution_Data %>%
filter(.data$Dilution_Batch == 2)
if(nrow(Dilution_Batch_1) > 3){
fit <- stats::lm(Area ~ Dilution_Percent,data = Dilution_Batch_1)
Dilution <- seq(min(Dilution_Batch_1$Dilution_Percent), max(Dilution_Batch_1$Dilution_Percent), length.out = 15)
fit_aug <- broom::augment(fit)
p <- p %>%
plotly::add_trace(data = Dilution_Batch_1,x = Dilution, y = stats::predict(fit,data.frame(Dilution_Percent = Dilution)) ,
type = 'scatter' , mode = 'lines', name = 'batch 1',
line = list(color = 'black', width = 3),
inherit = FALSE)
}
if(nrow(Dilution_Batch_2) > 3){
fit <- stats::lm(Area ~ Dilution_Percent,data = Dilution_Batch_2)
Dilution <- seq(min(Dilution_Batch_2$Dilution_Percent), max(Dilution_Batch_2$Dilution_Percent), length.out = 15)
fit_aug <- broom::augment(fit)
p <- p %>%
plotly::add_trace(data = Dilution_Batch_2,x = Dilution, y = stats::predict(fit,data.frame(Dilution_Percent = Dilution)) ,
type = 'scatter' , mode = 'lines', name = 'batch 2',
line = list(color = 'grey', width = 3),
inherit = FALSE)
}
p <- p %>%
plotly::layout(xaxis = list(title = "Dilution_Percent", titlefont = list(size = 10),
gridcolor = 'rgb(255,255,255)',
showgrid = TRUE,
showline = FALSE,
showticklabels = TRUE,
tickcolor = 'rgb(127,127,127)',
ticks = 'outside',
zeroline = FALSE,
tickfont = list(size = 10),
showspikes = TRUE, spikemode = "toaxis+marker", spikesnap = "data"),
yaxis = list(title = "Area", autorange = TRUE, fixedrange= FALSE,titlefont = list(size = 10),
gridcolor = 'rgb(255,255,255)',
showgrid = TRUE,
showline = FALSE,
showticklabels = TRUE,
tickcolor = 'rgb(127,127,127)',
ticks = 'outside',
zeroline = FALSE,
tickfont = list(size = 10),
showspikes = TRUE, spikemode = "toaxis+marker", spikesnap = "data"),
hovermode = "x",
legend = list(orientation = 'v', font = list(size = 10)),
paper_bgcolor='rgb(255,255,255)', plot_bgcolor='rgb(229,229,229)',
showlegend = TRUE
)
return(p)
}
# Step 1: Create directories to store the results ####
usethis::use_directory(fs::path("reports"))
# Step 2: Read the data ####
Dilution_Annot <- tibble::tibble(
Sample_Name = c("dil1_020","dil1_040","dil1_060","dil1_080","dil1_100","dil1_150",
"dil2_020","dil2_040","dil2_060","dil2_080","dil2_100","dil2_150"),
Dilution_Batch = c(1,1,1,1,1,1,2,2,2,2,2,2),
Dilution_Percent = c(20,40,60,80,100,150,20,40,60,80,100,150)
)
Lipid_Data <- tibble::tibble(
Sample_Name = c("dil1_020","dil1_040","dil1_060","dil1_080","dil1_100","dil1_150",
"dil2_020","dil2_040","dil2_060","dil2_080","dil2_100","dil2_150"),
`AcylCarnitine 12:0` = c(34233,62929,78112,94095,115251,180009,
20109,40560,61090,76704,106110,151130),
`AcylCarnitine 13:0` = c(18302,36220,44115,53939,66270,101338,
11308,23125,33976,44246,61553,88845)
)
Transition_Table <- tibble::tibble(
Transition_Name_Class = c("AcylCarn","AcylCarn"),
Transition_Name_Species = c("AcylCarn","AcylCarn"),
Transition_Name = c("AcylCarnitine 12:0","AcylCarnitine 13:0"),
Precursor_Ion = c(344.3,358.3),
Product_Ion = c(85.1,85.1),
Lipid_Name = c("AcylCarnitine 12:0","AcylCarnitine 13:0"),
isISTD = c("FALSE","FALSE"),
isQualifier = c("FALSE","FALSE")
)
# Step 3: Create legend annotation for plot ####
Dilution_Batch <- c("1","2")
levelFill <- c("#377eb8", "#4daf4a")
pal <- setNames(levelFill,Dilution_Batch)
# Step 4: Create the title for the Trellisope Plot and where to output the html files ####
Trelliscope_Name <- "Dilution Plot"
Trelliscope_Path <- here::here("reports","Dilution Plot")
# Step 5 Merge Lipid_Data, dilution annotation and pal ####
Lipid_Dil <- get_lipid_dil(Dilution_Annot,Lipid_Data,pal)
# Step 6 Nest data used for plotting per transition ####
nested <- Lipid_Dil %>%
dplyr::group_by(.data$Transition_Name) %>%
tidyr::nest() %>%
dplyr::left_join(Transition_Table,by = "Transition_Name") %>%
dplyr::arrange(.data$Transition_Name) %>%
get_dil_cognostics() %>%
dplyr::mutate(panel = trelliscopejs::map_plot(.data$data, dilution_plot,pal)) %>%
trelliscopejs::trelliscope(name = Trelliscope_Name,path = Trelliscope_Path,
state = list(sort = list(trelliscopejs::sort_spec("Transition_Name",dir = "asc"),
trelliscopejs::sort_spec("Dilution_Batch",dir = "asc")),
labels = c("Transition_Name", "Dilution_Batch")),
nrow = 2, ncol = 2,
height = 500,width = 800,
self_contained = FALSE,
thumb = FALSE,
auto_cog = FALSE)
@JauntyJJS - something happened with dplyr in the most recent update. Try adding a %>% ungroup() %>%
call before mutate()
. That seemed to fix this issue for me; I was getting the same error.
@hafen - probably need to change documentation to include the ungroup step. Took me several hours to figure out that was the root of the issue.
@lmbramer Thank you for the assistance. I have tried to add the ungroup()
function before mutate()
function is used in get_dil_cognostics
.
nested <- Lipid_Dil %>%
dplyr::group_by(.data$Transition_Name) %>%
tidyr::nest() %>%
dplyr::left_join(Transition_Table,by = "Transition_Name") %>%
dplyr::arrange(.data$Transition_Name) %>%
dplyr::ungroup() %>%
get_dil_cognostics() %>%
dplyr::mutate(panel = trelliscopejs::map_plot(.data$data, dilution_plot,pal)) %>%
trelliscopejs::trelliscope(name = Trelliscope_Name,path = Trelliscope_Path,
nrow = 2, ncol = 2,
height = 500,width = 800,
self_contained = FALSE,
thumb = FALSE,
auto_cog = FALSE)
The good new is that the code runs. The bad news is that only two panels were created for my case.
I think the problem is that the panel keys columns is no longer automatically added when the latest version of dplyr's group
and ungroup
are used
@hafen Is there a function in this package that is able to convert a certain column to a panel key column ?
Hi,
I apologise for taking a long time in this.
After several trial and errors, I managed to resolve this by using the function trelliscopejs::as_cognostics
and not using dplyr::group_by
. What I did was after nesting my data
I split the dataset into two sections, one to get the cognostics
# Step 8 Create the cognositcs ####
cog_df <- nested %>%
dplyr::select(-data) %>%
dplyr::left_join(Transition_Table,by = "Transition_Name") %>%
dplyr::arrange(.data$Transition_Name) %>%
get_dil_cognostics() %>%
trelliscopejs::as_cognostics(c("Transition_Name"))
and the other to collect the plots
# Step 9 Create the dilution plot per transition ####
plots <- nested %>%
dplyr::mutate(panel = trelliscopejs::map_plot(.data$data, dilution_plot,pal)) %>%
dplyr::select(panel)
and then I combine them with dplyr::bind_cols
# Step 10 Combine the cognostics df and the plot together ####
combined_df <- dplyr::bind_cols(cog_df,plots)
and perform the plot
# Step 11 Put plot in trelliscope ####
trelliscopejs::trelliscope(x = combined_df,
name = Trelliscope_Name,path = Trelliscope_Path,
nrow = 2, ncol = 2,
height = 500,width = 800,
self_contained = FALSE,
thumb = FALSE,
auto_cog = FALSE)
to get this
I hope this is able to assist any who has faced a similar issue as mine. I will post the code again in another comment. Thank you @lmbramer for your kind assistant
#Load the necessary libraries
suppressPackageStartupMessages(library("here"))
suppressPackageStartupMessages(library("usethis"))
suppressPackageStartupMessages(library("fs"))
suppressPackageStartupMessages(library("dplyr"))
suppressPackageStartupMessages(library("tidyr"))
suppressPackageStartupMessages(library("magrittr"))
suppressPackageStartupMessages(library("purrr"))
suppressPackageStartupMessages(library("tibble"))
suppressPackageStartupMessages(library("plotly"))
suppressPackageStartupMessages(library("broom"))
suppressPackageStartupMessages(library("trelliscopejs"))
# Functions Used ####
#' Function used to create a Dilution table by merging two tables
#'
#' @param Dilution_Annot Input Dilution Annotation. Must have column "Sample_Name", "Dilution_Batch", "Dilution_Percent"
#' @param Lipid_Data_Area Input Lipid area data frame with "Sample_Name" as the first column followed by Transition Name
#' @param pal Input palette for "Dilution_Batch". It is a named char vector where each value is a color and name is a dilution batch name
#' @return Output a lipid dilution table for each transition and dilution batch
#' @export
#' @importFrom magrittr %>%
#' @importFrom rlang .data
get_lipid_dil <- function(Dilution_Annot,Lipid_Data_Area,pal){
pal_df <- pal %>%
tibble::enframe() %>%
dplyr::rename(Dilution_Batch = .data$name) %>%
dplyr::mutate(Dilution_Batch = as.integer(.data$Dilution_Batch),
Dilution_Batch_Name = factor(.data$Dilution_Batch)) %>%
dplyr::rename(levelFill = .data$value) %>%
dplyr::select(.data$Dilution_Batch,.data$Dilution_Batch_Name,.data$levelFill)
Lipid_Dil <- dplyr::inner_join(Dilution_Annot,Lipid_Data_Area,by="Sample_Name") %>%
tidyr::pivot_longer(-c("Sample_Name","Dilution_Batch","Dilution_Percent"),
names_to = "Transition_Name", values_to = "Area") %>%
dplyr::inner_join(pal_df,by="Dilution_Batch")
return(Lipid_Dil)
}
#' Function used to add cognostics relevant to the dilution plot
#'
#' @param Dilution_Plot_Data Input Dilution Plot data
#' @return Output a data frame with added cognostics relevant to the dilution p
#' @export
#' @importFrom rlang .data
get_dil_cognostics <- function(Dilution_Plot_Data){
#Create cognostics
Dilution_Plot_Data <- dplyr::mutate(Dilution_Plot_Data,
Transition_Name = trelliscopejs::cog(.data$Transition_Name,
desc = "Transition_Name",
type = "factor",
default_label = TRUE),
Lipid_Name = trelliscopejs::cog(.data$Lipid_Name,
desc = "Lipid Name",
type = "factor",
default_label = FALSE),
Transition_Name_Class = trelliscopejs::cog(.data$Transition_Name_Class,
desc = "Lipid Class",
type = "factor",
default_label = FALSE),
Transition_Name_Species = trelliscopejs::cog(.data$Transition_Name_Species,
desc = "Lipid Species",
type = "factor",
default_label = FALSE),
Precursor_Ion = trelliscopejs::cog(.data$Precursor_Ion,
desc = "Precursor Ion",
type = "numeric",
default_label = FALSE),
Product_Ion = trelliscopejs::cog(.data$Product_Ion,
desc = "Product Ion",
type = "numeric",
default_label = FALSE),
isISTD = trelliscopejs::cog(.data$isISTD,
desc = "is an ISTD or not",
type = "factor",
default_label = FALSE),
isQualifier = trelliscopejs::cog(.data$isQualifier,
desc = "is a Qualifier or not",
type = "factor",
default_label = FALSE)
)
return(Dilution_Plot_Data)
}
#' Function used to create dilution plot
#'
#'
#' @param Dilution_Data Input Dilution Data with columns Dilution Percent,Area,levelFill and Dilution_Batch Name of one transition
#' @param pal Input palette for Dilution Batch. It is a named char vector where each value is a color and name is the Dilution Batch Name
#' @return Output Dilution Plot data of one transition
#' @export
#' @importFrom magrittr %>%
#' @importFrom rlang .data
dilution_plot <- function(Dilution_Data,pal){
Dilution_Data = tidyr::drop_na(Dilution_Data,.data$Area)
p <- plotly::plot_ly() %>%
plotly::add_trace(data = Dilution_Data,x = ~Dilution_Percent, y = ~Area,
type = 'scatter', mode = 'markers',
marker=list(size=10 , opacity=1, line = list(color = 'black',width = 1.5)),
#name = unique(data$Dilution_Batch_Name),
name = ~Dilution_Batch_Name,
color = ~Dilution_Batch_Name,
colors = pal,
hoverinfo = 'text',
text = ~Sample_Name,
hovertemplate = paste(
"<b>%{text}</b><br>",
"%{xaxis.title.text}: %{x}<br>",
"%{yaxis.title.text}: %{y:,0f}<br>",
"<extra></extra>"),
inherit = FALSE)
Dilution_Batch_1 <- Dilution_Data %>%
filter(.data$Dilution_Batch == 1)
Dilution_Batch_2 <- Dilution_Data %>%
filter(.data$Dilution_Batch == 2)
if(nrow(Dilution_Batch_1) > 3){
fit <- stats::lm(Area ~ Dilution_Percent,data = Dilution_Batch_1)
Dilution <- seq(min(Dilution_Batch_1$Dilution_Percent), max(Dilution_Batch_1$Dilution_Percent), length.out = 15)
fit_aug <- broom::augment(fit)
p <- p %>%
plotly::add_trace(data = Dilution_Batch_1,x = Dilution, y = stats::predict(fit,data.frame(Dilution_Percent = Dilution)) ,
type = 'scatter' , mode = 'lines', name = 'batch 1',
line = list(color = 'black', width = 3),
inherit = FALSE)
}
if(nrow(Dilution_Batch_2) > 3){
fit <- stats::lm(Area ~ Dilution_Percent,data = Dilution_Batch_2)
Dilution <- seq(min(Dilution_Batch_2$Dilution_Percent), max(Dilution_Batch_2$Dilution_Percent), length.out = 15)
fit_aug <- broom::augment(fit)
p <- p %>%
plotly::add_trace(data = Dilution_Batch_2,x = Dilution, y = stats::predict(fit,data.frame(Dilution_Percent = Dilution)) ,
type = 'scatter' , mode = 'lines', name = 'batch 2',
line = list(color = 'grey', width = 3),
inherit = FALSE)
}
p <- p %>%
plotly::layout(xaxis = list(title = "Dilution_Percent", titlefont = list(size = 10),
gridcolor = 'rgb(255,255,255)',
showgrid = TRUE,
showline = FALSE,
showticklabels = TRUE,
tickcolor = 'rgb(127,127,127)',
ticks = 'outside',
zeroline = FALSE,
tickfont = list(size = 10),
showspikes = TRUE, spikemode = "toaxis+marker", spikesnap = "data"),
yaxis = list(title = "Area", autorange = TRUE, fixedrange= FALSE,titlefont = list(size = 10),
gridcolor = 'rgb(255,255,255)',
showgrid = TRUE,
showline = FALSE,
showticklabels = TRUE,
tickcolor = 'rgb(127,127,127)',
ticks = 'outside',
zeroline = FALSE,
tickfont = list(size = 10),
showspikes = TRUE, spikemode = "toaxis+marker", spikesnap = "data"),
hovermode = "x",
legend = list(orientation = 'v', font = list(size = 10)),
paper_bgcolor='rgb(255,255,255)', plot_bgcolor='rgb(229,229,229)',
showlegend = TRUE
)
return(p)
}
# Step 1: Create directories to store the results ####
usethis::use_directory(fs::path("reports"))
# Step 2: Read the data ####
Dilution_Annot <- tibble::tibble(
Sample_Name = c("dil1_020","dil1_040","dil1_060","dil1_080","dil1_100","dil1_150",
"dil2_020","dil2_040","dil2_060","dil2_080","dil2_100","dil2_150"),
Dilution_Batch = c(1,1,1,1,1,1,2,2,2,2,2,2),
Dilution_Percent = c(20,40,60,80,100,150,20,40,60,80,100,150)
)
Lipid_Data <- tibble::tibble(
Sample_Name = c("dil1_020","dil1_040","dil1_060","dil1_080","dil1_100","dil1_150",
"dil2_020","dil2_040","dil2_060","dil2_080","dil2_100","dil2_150"),
`AcylCarnitine 12:0` = c(34233,62929,78112,94095,115251,180009,
20109,40560,61090,76704,106110,151130),
`AcylCarnitine 13:0` = c(18302,36220,44115,53939,66270,101338,
11308,23125,33976,44246,61553,88845)
)
Transition_Table <- tibble::tibble(
Transition_Name_Class = c("AcylCarn","AcylCarn"),
Transition_Name_Species = c("AcylCarn","AcylCarn"),
Transition_Name = c("AcylCarnitine 12:0","AcylCarnitine 13:0"),
Precursor_Ion = c(344.3,358.3),
Product_Ion = c(85.1,85.1),
Lipid_Name = c("AcylCarnitine 12:0","AcylCarnitine 13:0"),
isISTD = c("FALSE","FALSE"),
isQualifier = c("FALSE","FALSE")
)
# Step 3: Create legend annotation for plot ####
Dilution_Batch <- c("1","2")
levelFill <- c("#377eb8", "#4daf4a")
pal <- setNames(levelFill,Dilution_Batch)
# Step 4: Create the title for the Trellisope Plot and where to output the html files ####
Trelliscope_Name <- "Dilution Plot"
Trelliscope_Path <- here::here("reports","Dilution Plot")
# Step 5 Merge Lipid_Data, dilution annotation and pal ####
Lipid_Dil <- get_lipid_dil(Dilution_Annot,Lipid_Data,pal)
# Step 7 Nest data used for plotting per transition ####
nested <- Lipid_Dil %>%
tidyr::nest(data = -Transition_Name)
# Step 8 Create the cognositcs ####
cog_df <- nested %>%
dplyr::select(-data) %>%
dplyr::left_join(Transition_Table,by = "Transition_Name") %>%
dplyr::arrange(.data$Transition_Name) %>%
get_dil_cognostics() %>%
trelliscopejs::as_cognostics(c("Transition_Name"))
# Step 9 Create the dilution plot per transition ####
plots <- nested %>%
dplyr::mutate(panel = trelliscopejs::map_plot(.data$data, dilution_plot,pal)) %>%
dplyr::select(panel)
# Step 10 Combine the cognostics df and the plot together ####
combined_df <- dplyr::bind_cols(cog_df,plots)
# Step 11 Put plot in trelliscope ####
trelliscopejs::trelliscope(x = combined_df,
name = Trelliscope_Name,path = Trelliscope_Path,
nrow = 2, ncol = 2,
height = 500,width = 800,
self_contained = FALSE,
thumb = FALSE,
auto_cog = FALSE)
Not sure if there is a better way but this is what I managed to get
Hi,
I managed to create convert some columns of my data to "cog" objects using the cog function.
The good news is that the function trelliscope is able to plot the graph. Unfortunately I have this strange error saying that "cogdf must be a cognostics object - call as_cognostics() to cast it as such"
May I know what is the cause ?