#' @title plots lipidonomic data based on specifications set by parse_raw and summarise_lipids functions
#'
#' @description This combines head with a number of columns
#' @param object takes an object generated by summarise_lipids function
#' @import ggplot2
#' @import tidyverse
#' @import purrr
#' @import magrittr
#' @export lipidPlot
#' @return NULL
lipidPlot = function(object){
add_scale = function(scaling_factor=1.35){
if (object@yvariable == "mean_sum"){
ymin = 0; ymax = max(object@treatSummary$mean_sum) * scaling_factor
scale_y_continuous(expand = c(0, 0), limits = c(0,ymax))
} else if (object@yvariable == "diff"){
scale_y_continuous()
} else if (object@yvariable == "percent"){
scale_y_continuous(expand = c(0, 0),limits = c(0, 1))
}
}
add_errorbars = function(){
geom_errorbar(
data = object@treatSummary,
aes(ymin = mean_sum - se_sum, ymax = mean_sum + se_sum),
width = 0.3,
size = 0.5)
}
add_lines = function(){
len = length(object@groupSummary$treat %>% unique)
if(len%%2 == 0){
lapply(seq(len/6 + 0.5, len*2 + 0.5, len*2 / 12),
function(x) geom_vline(aes(xintercept=x), linetype=2, alpha=0.2) )
} else {
return(NULL)
}
}
add_statistics = function(){
if(length(object@treatSummary$TREATMENT %>% unique) == 2){
(object@treatSummary %>%
group_by(REGION) %>%
filter(mean_sum == max(mean_sum)) %>%
left_join(object@results, by = 'REGION') %>%
cbind(xposition = seq(1, nrow(object@treatSummary),2)) %>%
dplyr::mutate(geom = map2(xposition, stars, function(i,s) {
yposition = (mean_sum + se_sum)*1.1
list(
geom_path(data = data.frame(x=c(rep(i,2), rep(i+1,2)),
y=c(rep(yposition, 4))),
size = 1,
aes(x=x,y=y)),
annotate("text",
x=xposition + 0.5,
y=yposition + 35,
label=s)
)
})))$geom
} else {
NULL
}
}
saturation_legend = function(position = c(0.9,0.8), justification = c(1,-0.1)){
theme(legend.justification=justification,
legend.position=position,
axis.text.x=element_blank(),
axis.text.y=element_blank(),
axis.ticks=element_blank(),
axis.title.x=element_blank(),
axis.title.y=element_blank())
}
generate_plot = function(){
object@treatSummary %>%
ggplot(aes(get(object@xvariable), mean_sum)) +
geom_bar(
data = object@groupSummary,
aes(x = get(object@xvariable), y = get(object@yvariable), fill = reorder(FILL_BY, ORDER)),
stat = "identity"
) +
theme_bw() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_rect(colour = "black", size=1),
legend.title=element_blank()
) +
labs(x = '', y = object@ytitle) +
scale_x_discrete(limits = object@xorder) +
scale_fill_manual(values = object@colours) +
add_scale() +
{ if(object@yvariable == 'mean_sum'){ # START mean_sum block
if(object@grouping == "CHAIN"){
list(
add_errorbars(),
add_lines(),
add_statistics()
)
} else if (object@grouping == "SATURATION"){
list(
add_scale(),
add_errorbars(),
saturation_legend()
)
}
} else if(object@yvariable == 'diff' & object@grouping == "SATURATION"){
saturation_legend()
}
}
}
return(generate_plot())
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.